java實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
本文實(shí)例為大家分享了java實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
我搭建的是SSM 框架:
一、實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
以省市區(qū)為例:
我的想法很簡(jiǎn)單 ,可能想的有點(diǎn)少,首先遍歷省份,當(dāng)數(shù)據(jù)發(fā)生改變調(diào)用方法請(qǐng)求根據(jù)省的id去查詢市區(qū)的信息,當(dāng)市區(qū)信息發(fā)生改變調(diào)用另一個(gè)方法去查詢縣區(qū)的信息
1、實(shí)體類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<Area> getProvince(); ?? ? ?? ?//獲取市區(qū)信息 ?? ?public List<Area> getCity(Integer pid); ?? ? ?? ?//獲取所有縣區(qū)信息 ?? ?public List<Area> getArea(Integer pid); ?? ? }
3、接口service層,(個(gè)人覺得兩個(gè)接口層公用一個(gè)也行,就像那種公用一個(gè)的改為mapper接口層那種的也很方便,可能這樣比較不規(guī)范吧)
package com.htzn.service; import java.util.List; import com.htzn.entity.Area; public interface AreaService { ?? ? ?? ?public List<Area> getProvince(); ?? ?public List<Area> getCity(Integer pid); ?? ?public List<Area> getArea(Integer pid); ?? ? }
4、接口實(shí)現(xiàn)類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<Area> getProvince() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?return areadao.getProvince(); ?? ?} ?? ?@Override ?? ?public List<Area> getCity(Integer pid) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?return areadao.getCity(pid); ?? ?} ?? ?@Override ?? ?public List<Area> 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 { ?? ?//自動(dòng)注入 ?? ?@Autowired ?? ?AreaDao areadao; ?? ?//獲取省份映射到頁(yè)面 ?? ?@RequestMapping("/getpervice") ?? ?public String privce(Model model) { ?? ??? ?List<Area> list = areadao.getProvince(); ?? ??? ?model.addAttribute("province", list); ?? ??? ?return "arealist"; ?? ?} ?? ?//根據(jù)省份id獲取對(duì)應(yīng)市區(qū) ?? ?@ResponseBody ?? ?@RequestMapping("/getcity") ?? ?public List<Area> city(@RequestParam(value="pid",required=false) Integer id) { ?? ??? ?List<Area> city = areadao.getCity(id); ?? ??? ?return city; ?? ?} ?? ?//根據(jù)市區(qū)id獲取相應(yīng)的縣區(qū) ?? ?@ResponseBody ?? ?@RequestMapping("/getarea") ?? ?public List<Area> area(@RequestParam(value="pid",required=false) Integer id) { ?? ??? ?List<Area> area = areadao.getArea(id); ?? ??? ?return area; ?? ?} }
6、最后映射頁(yè)面: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"%> ? ? <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> </head> <body> ??。? ?<select name="province" id="province" ?onchange="changeCity()"> <c:forEach items="${province }" var="list"> ?? ?<option value="${list.id }" >${list.name }</option> ?</c:forEach> ? ? ? ?</select>? ? 市: ?<select id="city" name="city" onchange="changeDistrict()"> ? ? ? <option value="">-- 請(qǐng)選擇市 --</option> ?</select> ? 區(qū)(縣): <select id="district" name="district" onchange="changehidden()"> ? ? ? <option value="">-- 請(qǐng)選擇縣(區(qū)) --</option> </select> --> </body> <script type="text/javascript"> ? ? function changeCity(){ ? ? ? ? //當(dāng)省的內(nèi)容發(fā)生變化的時(shí)候,響應(yīng)的改變省的隱藏域的值 ? ? ? ? $("#phidden").val($("#province option:selected").html()); ? ? ? ? //頁(yè)面加載完成,將省的信息加載完成 ? ? ? ? //下拉列表框標(biāo)簽對(duì)象的val()方法就是選中的option標(biāo)簽的value的屬性值 ? ? ? ? var pid = $("#province").val(); ? ? ? ? alert(pid); ? ? ? ? $.ajax({ ? ? ? ? ? ? url:"/sky-ssm/getcity", ? ? ? ? ? ? type:'post', ? ? ? ? ? ? data:{"pid":pid}, ? ? ? ? ? ? dataType: "json", ? ? ? ? ? ? success:function(data){ ? ? ? ? ? ? ? ? //清空城市下拉列表框的內(nèi)容 ? ? ? ? ? ? ? ? $("#city").html("<option value=''>-- 請(qǐng)選擇市 --</option>"); ? ? ? ? ? ? ? ? $("#district").html("<option value=''>-- 請(qǐng)選擇區(qū)/縣 --</option>"); ? ? ? ? ? ? ? ? //遍歷json,json數(shù)組中每一個(gè)json,都對(duì)應(yīng)一個(gè)省的信息,都應(yīng)該在省的下拉列表框下面添加一個(gè)<option> ? ? ? ? ? ? ? ? for(var i=0;i<data.length;i++){ ? ? ? ? ? ? ? ? ? ? var $option = $("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); ? ? ? ? ? ? ? ? ? ? $("#city").append($option); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }, ? ? ? ? ? ? error:function(data){ ? ? ? ? ? ? ?? ?alert("失敗了"); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? function changeDistrict(){ ? ? ? ? //當(dāng)城市的內(nèi)容發(fā)生變化的時(shí)候,相應(yīng)的改變城市的隱藏域的值 ? ? ? ? $("#chidden").val($("#city option:selected").html()); ? ? ? ? //頁(yè)面加載完成,將省的信息加載完成 ? ? ? ? //下拉列表框標(biāo)簽對(duì)象的val()方法就是選中的option標(biāo)簽的value的屬性值 ? ? ? ? var pid = $("#city").val(); ? ? ? ? $.ajax({ ? ? ? ? ? ? url:"/sky-ssm/getarea", ? ? ? ? ? ? data:{"pid":pid}, ? ? ? ? ? ? dataType:"json", ? ? ? ? ? ? success:function(data){ ? ? ? ? ? ? ? ? //清空城市下拉列表框的內(nèi)容 ? ? ? ? ? ? ? ? $("#district").html("<option value=''>-- 請(qǐng)選擇區(qū)/縣 --</option>"); ? ? ? ? ? ? ? ? for(var i=0;i<data.length;i++){ ? ? ? ? ? ? ? ? ? ? var $option = $("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); ? ? ? ? ? ? ? ? ? ? $("#district").append($option); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? function changehidden(){ ? ? ? ? //當(dāng)城市的內(nèi)容發(fā)生變化的時(shí)候,相應(yīng)的改變城市的隱藏域的值 ? ? ? ? $("#dhidden").val($("#district option:selected").html()); ? ? } </script> </html>
7、mapper.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.htzn.dao.AreaDao"> ? <resultMap id="BaseResultMap" type="com.htzn.entity.Area"> ? ? <!-- ? ? ? WARNING - @mbg.generated ? ? ? This element is automatically generated by MyBatis Generator, do not modify. ? ? ? This element was generated on Thu Jan 09 17:01:48 CST 2020. ? ? --> ? ? <id column="id" jdbcType="VARCHAR" property="id" /> ? ? <result column="name" jdbcType="VARCHAR" property="name" /> ? ? <result column="pid" jdbcType="VARCHAR" property="pid" /> ? </resultMap> ? <sql id="Base_Column_List"> ? ? <!-- ? ? ? WARNING - @mbg.generated ? ? ? This element is automatically generated by MyBatis Generator, do not modify. ? ? ? This element was generated on Thu Jan 09 17:01:48 CST 2020. ? ? --> ? ? id, name, pid ? </sql> ?? ? ? <select id="getProvince" ?resultMap="BaseResultMap"> ?? ? ? select? ? ? <include refid="Base_Column_List" /> ? ? from area? ? ? where pid = 0 ? ?? ? </select> ?? ? ? ? <select id="getCity" ?resultMap="BaseResultMap"> ?? ? ? select? ? ? <include refid="Base_Column_List" /> ? ? from area? ? ? where pid = #{pid} ? ?? ? </select> ?? ? <select id="getArea" ?resultMap="BaseResultMap"> ?? ? ? select? ? ? <include refid="Base_Column_List" /> ? ? from area? ? ? where pid = #{pid} ? ?? ? </select> ? ? <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> ? ? <!-- ? ? ? WARNING - @mbg.generated ? ? ? This element is automatically generated by MyBatis Generator, do not modify. ? ? ? This element was generated on Thu Jan 09 17:01:48 CST 2020. ? ? --> ? ? select? ? ? <include refid="Base_Column_List" /> ? ? from dept ? ? where id = #{id,jdbcType=INTEGER} ? </select> </mapper>
因?yàn)榫褪菧y(cè)試可不可行直接寫的select下拉框,結(jié)果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java方法重載和參數(shù)類型自動(dòng)提升方式
這篇文章主要介紹了java方法重載和參數(shù)類型自動(dòng)提升方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java FileInputStream與FileOutputStream使用詳解
這篇文章主要介紹了Java FileInputStream與FileOutputStream使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08