本文实例为大家分享了java实现省市区三级联动的具体代码,供大家参考,具体内容如下
我搭建的是SSM 框架:
一、实现三级联动
以省市区为例:
我的想法很简单 ,可能想的有点少,首先遍历省份,当数据发生改变调用方法请求根据省的id去查询市区的信息,当市区信息发生改变调用另一个方法去查询县区的信息
1、实体类entity:area
package com.htzn.entity; public class Area { private String id ; private String name; private String pid; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } }
2、接口层 dao
package com.htzn.dao; import java.util.List; import com.htzn.entity.Area; public interface AreaDao { //获取省的信息 public List getProvince(); //获取市区信息 public List getCity(Integer pid); //获取所有县区信息 public List getArea(Integer pid); }
3、接口service层,(个人觉得两个接口层公用一个也行,就像那种公用一个的改为mapper接口层那种的也很方便,可能这样比较不规范吧)
package com.htzn.service; import java.util.List; import com.htzn.entity.Area; public interface AreaService { public List getProvince(); public List getCity(Integer pid); public List getArea(Integer pid); }
4、接口实现类serviceImpl
package com.htzn.serviceImpl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.htzn.dao.AreaDao; import com.htzn.entity.Area; import com.htzn.service.AreaService; @Service public class AreaServiceImpl implements AreaService { @Autowired AreaDao areadao; @Override public List getProvince() { // TODO Auto-generated method stub return areadao.getProvince(); } @Override public List getCity(Integer pid) { // TODO Auto-generated method stub return areadao.getCity(pid); } @Override public List getArea(Integer pid) { // TODO Auto-generated method stub return areadao.getArea(pid); } }
5、控制器:contoller
package com.htzn.controller; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.htzn.dao.AreaDao; import com.htzn.entity.Area; @Controller public class AreaController { //自动注入 @Autowired AreaDao areadao; //获取省份映射到页面 @RequestMapping("/getpervice") public String privce(Model model) { List list = areadao.getProvince(); model.addAttribute("province", list); return "arealist"; } //根据省份id获取对应市区 @ResponseBody @RequestMapping("/getcity") public List city(@RequestParam(value="pid",required=false) Integer id) { List city = areadao.getCity(id); return city; } //根据市区id获取相应的县区 @ResponseBody @RequestMapping("/getarea") public List area(@RequestParam(value="pid",required=false) Integer id) { List area = areadao.getArea(id); return area; } }
6、最后映射页面:jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fm"%>Insert title here 省: 市: 区(县): -->
7、mapper.xml文件:
id, name, pid
因为就是测试可不可行直接写的select下拉框,结果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。