欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)

 更新時(shí)間:2022年06月23日 14:30:06   作者:MR_非凡  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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簡(jiǎn)單工廠模式入門

    java簡(jiǎn)單工廠模式入門

    下面小編就為大家?guī)?lái)一篇java工廠模式入門文章。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-07-07
  • 分析講解Java?Random類里的種子問(wèn)題

    分析講解Java?Random類里的種子問(wèn)題

    Random類中實(shí)現(xiàn)的隨機(jī)算法是偽隨機(jī),也就是有規(guī)則的隨機(jī)。在進(jìn)行隨機(jī)時(shí),隨機(jī)算法的起源數(shù)字稱為種子數(shù)(seed),在種子數(shù)的基礎(chǔ)上進(jìn)行一定的變換,從而產(chǎn)生需要的隨機(jī)數(shù)字
    2022-05-05
  • java方法重載和參數(shù)類型自動(dòng)提升方式

    java方法重載和參數(shù)類型自動(dòng)提升方式

    這篇文章主要介紹了java方法重載和參數(shù)類型自動(dòng)提升方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 聊聊SpringBoot自動(dòng)裝配的魔力

    聊聊SpringBoot自動(dòng)裝配的魔力

    這篇文章主要介紹了SpringBoot自動(dòng)裝配的魔力,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java核心教程之常見時(shí)間日期的處理方法

    Java核心教程之常見時(shí)間日期的處理方法

    這篇文章主要給大家介紹了關(guān)于Java核心教程之常見時(shí)間日期的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • java數(shù)字轉(zhuǎn)漢字工具類詳解

    java數(shù)字轉(zhuǎn)漢字工具類詳解

    這篇文章主要為大家詳細(xì)介紹了java數(shù)字轉(zhuǎn)漢字工具類的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Java利用蒙特卡洛方法求解圓周率π值

    Java利用蒙特卡洛方法求解圓周率π值

    蒙特·卡羅方法(Monte Carlo method),也稱統(tǒng)計(jì)模擬方法,是一種以概率統(tǒng)計(jì)理論為基礎(chǔ)的數(shù)值計(jì)算方法。本文將利用該方法實(shí)現(xiàn)圓周率的計(jì)算,需要的可以參考一下
    2022-08-08
  • 使用springboot配置和占位符獲取配置文件中的值

    使用springboot配置和占位符獲取配置文件中的值

    這篇文章主要介紹了使用springboot配置和占位符獲取配置文件中的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java FileInputStream與FileOutputStream使用詳解

    Java FileInputStream與FileOutputStream使用詳解

    這篇文章主要介紹了Java FileInputStream與FileOutputStream使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • MyBatis別名和settings設(shè)置方式

    MyBatis別名和settings設(shè)置方式

    這篇文章主要介紹了MyBatis別名和settings設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評(píng)論