最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實(shí)例為大家分享了java實(shí)現(xiàn)省市區(qū)三級聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下

我搭建的是SSM 框架:

一、實(shí)現(xiàn)三級聯(lián)動

以省市區(qū)為例:
我的想法很簡單 ,可能想的有點(diǎn)少,首先遍歷省份,當(dāng)數(shù)據(jù)發(fā)生改變調(diào)用方法請求根據(jù)省的id去查詢市區(qū)的信息,當(dāng)市區(qū)信息發(fā)生改變調(diào)用另一個方法去查詢縣區(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層,(個人覺得兩個接口層公用一個也行,就像那種公用一個的改為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 {

?? ?//自動注入
?? ?@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ā)生變化的時候,響應(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ù)組中每一個json,都對應(yīng)一個省的信息,都應(yīng)該在省的下拉列表框下面添加一個<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ā)生變化的時候,相應(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ā)生變化的時候,相應(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>

因為就是測試可不可行直接寫的select下拉框,結(jié)果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java數(shù)據(jù)結(jié)構(gòu)專題解析之棧和隊列的實(shí)現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)專題解析之棧和隊列的實(shí)現(xiàn)

    從數(shù)據(jù)結(jié)構(gòu)的定義看,棧和隊列也是一種線性表。其不同之處在于棧和隊列的相關(guān)運(yùn)算具有特殊性,只是線性表相關(guān)運(yùn)算的一個子集。更準(zhǔn)確的說,一般線性表的插入、刪除運(yùn)算不受限制,而棧和隊列上的插入刪除運(yùn)算均受某種特殊限制。因此,棧和隊列也稱作操作受限的線性表
    2021-10-10
  • Mybatis基于注解與XML開發(fā)使用流程

    Mybatis基于注解與XML開發(fā)使用流程

    MyBatis是Java的持久化框架,目的是為了使操作數(shù)據(jù)庫更加方便、靈活、高效,可以通過Java注解和XML文件來映射Java對象和SQL語句,提供了非常靈活的SQL編寫方式和動態(tài)SQL語句的創(chuàng)建方式,這篇文章主要介紹了Mybatis基于注解與XML開發(fā),需要的朋友可以參考下
    2023-07-07
  • Java如何實(shí)現(xiàn)字符串每隔4位加空格

    Java如何實(shí)現(xiàn)字符串每隔4位加空格

    這篇文章主要介紹了Java如何實(shí)現(xiàn)字符串每隔4位加空格,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • java如何讀取某個文件夾中的全部文件(包括子文件夾)

    java如何讀取某個文件夾中的全部文件(包括子文件夾)

    這篇文章主要介紹了java如何讀取某個文件夾中的全部文件(包括子文件夾),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java中的Spring Security配置過濾器

    Java中的Spring Security配置過濾器

    這篇文章主要介紹了Java中的Spring Security配置過濾器,文章通過圍繞文章主題的相關(guān)資料展開詳細(xì)內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • 簡易版SpringBoot自定義模擬實(shí)現(xiàn)

    簡易版SpringBoot自定義模擬實(shí)現(xiàn)

    SpringBoot作為目前最流行的框架之一,極大地提高了開發(fā)效率和降低了學(xué)習(xí)成本,使得開發(fā)人員能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),而無需過多關(guān)注底層框架的配置和集成,本文模擬實(shí)現(xiàn)簡易版SpringBoot
    2024-01-01
  • Java實(shí)現(xiàn)獲取客戶端真實(shí)IP方法小結(jié)

    Java實(shí)現(xiàn)獲取客戶端真實(shí)IP方法小結(jié)

    本文給大家匯總介紹了2種使用java實(shí)現(xiàn)獲取客戶端真實(shí)IP的方法,主要用于獲取使用了代理訪問的來訪者的IP,有需要的小伙伴可以參考下。
    2016-03-03
  • Spring容器添加組件方式實(shí)現(xiàn)

    Spring容器添加組件方式實(shí)現(xiàn)

    這篇文章主要介紹了Spring容器添加組件方式實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Maven build 命令介紹的使用詳解

    Maven build 命令介紹的使用詳解

    這篇文章主要介紹了Maven build 命令介紹的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 在Java編程中使用正則表達(dá)式的基本方法

    在Java編程中使用正則表達(dá)式的基本方法

    這篇文章主要介紹了在Java編程中使用正則表達(dá)式的基本方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11

最新評論

漾濞| 乐山市| 临潭县| 建水县| 石楼县| 革吉县| 深泽县| 原平市| 安康市| 梧州市| 内乡县| 涡阳县| 福泉市| 平和县| 元氏县| 麦盖提县| 遵化市| 富平县| 思南县| 仁化县| 贵阳市| 达州市| 招远市| 兴安盟| 云龙县| 四会市| 新田县| 和田县| 全州县| 毕节市| 南和县| 改则县| 哈巴河县| 武隆县| 梅河口市| 惠来县| 大洼县| 孝昌县| 安新县| 盱眙县| 顺昌县|