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ū)為例:
我的想法很簡單 ,可能想的有點(diǎn)少,首先遍歷省份,當(dāng)數(shù)據(jù)發(fā)生改變調(diào)用方法請求根據(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;
?? ?//獲取省份映射到頁面
?? ?@RequestMapping("/getpervice")
?? ?public String privce(Model model) {
?? ??? ?List<Area> list = areadao.getProvince();
?? ??? ?model.addAttribute("province", list);
?? ??? ?return "arealist";
?? ?}
?? ?//根據(jù)省份id獲取對應(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、最后映射頁面: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="">-- 請選擇市 --</option>
?</select>
? 區(qū)(縣):
<select id="district" name="district" onchange="changehidden()">
? ? ? <option value="">-- 請選擇縣(區(qū)) --</option>
</select> -->
</body>
<script type="text/javascript">
? ? function changeCity(){
? ? ? ? //當(dāng)省的內(nèi)容發(fā)生變化的時(shí)候,響應(yīng)的改變省的隱藏域的值
? ? ? ? $("#phidden").val($("#province option:selected").html());
? ? ? ? //頁面加載完成,將省的信息加載完成
? ? ? ? //下拉列表框標(biāo)簽對象的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=''>-- 請選擇市 --</option>");
? ? ? ? ? ? ? ? $("#district").html("<option value=''>-- 請選擇區(qū)/縣 --</option>");
? ? ? ? ? ? ? ? //遍歷json,json數(shù)組中每一個(gè)json,都對應(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());
? ? ? ? //頁面加載完成,將省的信息加載完成
? ? ? ? //下拉列表框標(biāo)簽對象的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ū)/縣 --</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試可不可行直接寫的select下拉框,結(jié)果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)專題解析之棧和隊(duì)列的實(shí)現(xiàn)
從數(shù)據(jù)結(jié)構(gòu)的定義看,棧和隊(duì)列也是一種線性表。其不同之處在于棧和隊(duì)列的相關(guān)運(yùn)算具有特殊性,只是線性表相關(guān)運(yùn)算的一個(gè)子集。更準(zhǔn)確的說,一般線性表的插入、刪除運(yùn)算不受限制,而棧和隊(duì)列上的插入刪除運(yùn)算均受某種特殊限制。因此,棧和隊(duì)列也稱作操作受限的線性表2021-10-10
java如何讀取某個(gè)文件夾中的全部文件(包括子文件夾)
這篇文章主要介紹了java如何讀取某個(gè)文件夾中的全部文件(包括子文件夾),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
簡易版SpringBoot自定義模擬實(shí)現(xiàn)
SpringBoot作為目前最流行的框架之一,極大地提高了開發(fā)效率和降低了學(xué)習(xí)成本,使得開發(fā)人員能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),而無需過多關(guān)注底層框架的配置和集成,本文模擬實(shí)現(xiàn)簡易版SpringBoot2024-01-01
Java實(shí)現(xiàn)獲取客戶端真實(shí)IP方法小結(jié)
本文給大家匯總介紹了2種使用java實(shí)現(xiàn)獲取客戶端真實(shí)IP的方法,主要用于獲取使用了代理訪問的來訪者的IP,有需要的小伙伴可以參考下。2016-03-03

