ssm框架下实现省市县三级联动

运行效果:省份信息从数据库中自动遍历,单击省份下的江西,城市信息便自动从数据库中读出,单击城市中的九江,县城信息便从数据库中读出


ssm框架下实现省市县三级联动_第1张图片

数据库

ssm框架下实现省市县三级联动_第2张图片

jsp层:comovement.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 


三级联动





    
        
       
       


控制层:ComovementController.java

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.kebo.po.Comovement;
import com.kebo.service.ComovementService;

@Controller
public class ComovementController {
@Autowired
private ComovementService comovementService;
@RequestMapping("findProvince")
public String findProvince(Model model) {
	List province=comovementService.findProvince();
	model.addAttribute("province",province);
	return "comovement";
}
@RequestMapping(value="/testjson1/{pr_name}",method=RequestMethod.GET)
@ResponseBody
public Object testJson1( @PathVariable("pr_name") String pr_name,Model model) throws IOException{
	List city=comovementService.findCityByName(pr_name);
 return city;
}
@RequestMapping(value="/testjson2/{ci_name}",method=RequestMethod.GET)
@ResponseBody
public Object testJson2(@PathVariable("ci_name") String ci_name,Model model) throws IOException{
	List county=comovementService.findCountyByName(ci_name);
 return county;
}
}

Dao层:ComovementDao.java

import java.util.List;

import com.kebo.po.Comovement;

public interface ComovementDao {
public List findProvince();
public List findCityByName(String pr_name);
public List findCountyByName(String ci_name);
}

ComovementDao.xml




    
    
    
    
    
    

Service层:ComovementService.java

import java.util.List;

import com.kebo.po.Comovement;

public interface ComovementService {
	public List findProvince();
	public List findCityByName(String pr_name);
	public List findCountyByName(String ci_name);
}

实现层:ComovementServiceImpl.java

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kebo.dao.ComovementDao;
import com.kebo.po.Comovement;
import com.kebo.service.ComovementService;
@Service
@Transactional
public class ComovementServiceImpl implements ComovementService {
	@Autowired
private ComovementDao comovementDao;
	@Override
	public List findProvince() {
		return this.comovementDao.findProvince();
	}

	@Override
	public List findCityByName(String pr_name) {
		// TODO Auto-generated method stub
		return this.comovementDao.findCityByName(pr_name);
	}

	@Override
	public List findCountyByName(String ci_name) {
		// TODO Auto-generated method stub
		return this.comovementDao.findCountyByName(ci_name);
	}

}

po层:Comovement.java

public class Comovement {
private String pr_name;
private String ci_name;
private String co_name;
public String getPr_name() {
	return pr_name;
}
public void setPr_name(String pr_name) {
	this.pr_name = pr_name;
}
public String getCi_name() {
	return ci_name;
}
public void setCi_name(String ci_name) {
	this.ci_name = ci_name;
}
public String getCo_name() {
	return co_name;
}
public void setCo_name(String co_name) {
	this.co_name = co_name;
}

}

注意:ssm框架下springmvc的拦截器会把jq的配置文件给拦截掉,需要在spring-config.xml文件中

使静态文件不被拦截(例如),除此之外,ssm框架下实现此功能除了需要ssm框架本身的jar包之外,还需要导入json支持包,以及jstl.jar和standard.jar包用于遍历输出省级信息

这是整体的项目图:

ssm框架下实现省市县三级联动_第3张图片


你可能感兴趣的:(ssm框架下实现省市县三级联动)