黄色电影一区二区,韩国少妇自慰A片免费看,精品人妻少妇一级毛片免费蜜桃AV按摩师 ,超碰 香蕉

如何利用Ajax實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)詳解

前言:

利用ajax來(lái)實(shí)現(xiàn)一個(gè)地區(qū)的三級(jí)聯(lián)動(dòng),用java代碼來(lái)讀json文件,先eclipse做一個(gè)簡(jiǎn)單的,最基礎(chǔ)的。(json我用的jackson來(lái)解析,也可用fastjson-阿里巴巴的等還有很多)提供代碼,思路之類(lèi)的,注釋也沒(méi)有自己去想去琢磨出來(lái)的思路好

first:首先先要熟悉json文件,并要想好利用什么類(lèi)型去解析,這是最難的,最好找一個(gè)沒(méi)人的地方戴上耳機(jī)(對(duì)于初學(xué))我是用maven來(lái)做的用到的jar坐標(biāo) :

<dependency>
			<groupid>redis.clients</groupid>
			<artifactid>jedis</artifactid>
			<version>2.9.0</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupid>javax.servlet</groupid>
			<artifactid>javax.servlet-api</artifactid>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupid>com.fasterxml.jackson.core</groupid>
			<artifactid>jackson-databind</artifactid>
			<version>2.11.2</version>
		</dependency>

文件位置:

second:首先創(chuàng)建一個(gè)html文件 three.html

加了一個(gè)字體居中和大小的樣式以至于不會(huì)太難看,太原生

首先來(lái)實(shí)現(xiàn)–省--的局部刷新,利用ajax

<script type="text/javascript">
	$(function(){
		$.post("province",function(data){
			$.each(data,function(){
				$("#province").append("<option value="+this.code+">"+this.name+"</option>");
			})
		},"json")
	})
</script>

然后來(lái)寫(xiě)對(duì)應(yīng)的provincecontroller.class的代碼(主要是邏輯,為什么我要用list<map<string,object>>類(lèi)型)

package com.daben.controller;

import java.io.fileinputstream;
import java.io.ioexception;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import java.util.map;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.fasterxml.jackson.core.type.typereference;
import com.fasterxml.jackson.databind.objectmapper;

@webservlet("/province")
public class provincecontroller extends httpservlet{

	private static final long serialversionuid = -6513954606070061277l;

	@override
	protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		this.dopost(req, resp);
	}

	@override
	protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		resp.setcontenttype("appliaction/json;charset=utf-8");//可加可不加,json可在前端標(biāo)注也可在后端 看自己習(xí)慣 我前后都加了
		objectmapper om = new objectmapper(); //jackson核心類(lèi)
		string path = req.getservletcontext().getrealpath("/web-inf/classes/city_code.json");//利用servletcontext(也有叫appliaction)來(lái)拿到文件的真實(shí)路徑,也可以利用加載器拿都一樣
		fileinputstream fi = new fileinputstream(path);//流
		list<map<string, object>> province = om.readvalue(fi, new typereference<list<map<string,object>>>() {});//jackson解析的方法,為什么是這個(gè)方法,百度學(xué)的 利用typereference可解析你想要得到的類(lèi)型
		list<map<string,object>> list = new arraylist<>();
		iterator<map<string, object>> iterator = province.iterator();//我用的迭代器遍歷的 foreach等 也可以 
		while(iterator.hasnext()) {
			map<string, object> map2 = iterator.next();
			map2.remove("city");//可寫(xiě)也可不寫(xiě)
			list.add(map2);
		}
		om.writevalue(resp.getwriter(), list);
	}
	
}

在three.html添加改變事件

代碼比較簡(jiǎn)單,就是跟簡(jiǎn)單的清空 賦值 取值

$("#province").on("change", function(){
			let code = $(this).find(":selected").val();
			$.post("city",{"code":code}, function(data){
				$("#city").empty();
				$("#city").append("<option>---市---</option>");
				$.each(data, function(){
					$("#city").append("<option value="+this.code+">"+this.name+"</option>");
				})
			},"json");
		});

再寫(xiě)對(duì)應(yīng)的citycontroller.class(代碼類(lèi)似)我為什么還會(huì)強(qiáng)轉(zhuǎn)list<map<string,object>>類(lèi)型

package com.daben.controller;

import java.io.fileinputstream;
import java.io.ioexception;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import java.util.map;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.fasterxml.jackson.core.type.typereference;
import com.fasterxml.jackson.databind.objectmapper;
@webservlet("/city")
public class citycontroller extends httpservlet{

	private static final long serialversionuid = -6513954606070061277l;

	@override
	protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		this.dopost(req, resp);
	}

	@suppresswarnings("unchecked")
	@override
	protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		resp.setcontenttype("appliaction/json;charset=utf-8");
		string code = req.getparameter("code");
		req.getsession().setattribute("citycode", code);
		objectmapper om = new objectmapper();
		string path = req.getservletcontext().getrealpath("/web-inf/classes/city_code.json");
		fileinputstream fi = new fileinputstream(path);
		list<map<string, object>> province = om.readvalue(fi, new typereference<list<map<string,object>>>() {});
		list<map<string,object>> list = new arraylist<>();
		iterator<map<string, object>> iterator = province.iterator();
		while(iterator.hasnext()) {
			map<string, object> map2 = iterator.next();
			if(map2.get("code").equals(code)) {
				map2.remove("code");
				map2.remove("name");
				list=(list<map<string,object>>) map2.get("city");
				break;
			}
			
		}
		list<map<string,object>> list1 = new arraylist<>();
		iterator<map<string,object>> iterator2 = list.iterator();
		while(iterator2.hasnext()) {
			map<string,object> next = iterator2.next();
			next.remove("area");
			list1.add(next);
			
		}
		om.writevalue(resp.getwriter(), list1);
	}
	
}

不懂的話(huà),可以先看一看city_code.json文件,多想一想

繼續(xù)來(lái)three.html

$("#city").on("change", function(){
			let code = $(this).find(":selected").val();
			$.post("village",{"code":code}, function(data){
				$("#village").empty();
				$("#village").append("<option>---縣---</option>");
				$.each(data, function(){
					$("#village").append("<option value="+this.code+">"+this.name+"</option>");
				})
			},"json");

代碼雷同以至于villagecontroller.class也是雷同,加了一些判斷而已多了一個(gè)循環(huán),

package com.daben.controller;
import java.io.fileinputstream;
import java.io.ioexception;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import java.util.map;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.fasterxml.jackson.core.type.typereference;
import com.fasterxml.jackson.databind.objectmapper;
@webservlet("/village")
public class villagecontroller extends httpservlet{

	private static final long serialversionuid = -6513954606070061277l;

	@override
	protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		this.dopost(req, resp);
	}

	@suppresswarnings("unchecked")
	@override
	protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		resp.setcontenttype("appliaction/json;charset=utf-8");
		string code = req.getparameter("code");
		string citycode = (string)req.getsession().getattribute("citycode");
		objectmapper om = new objectmapper();
		string path = req.getservletcontext().getrealpath("/web-inf/classes/city_code.json");
		fileinputstream fi = new fileinputstream(path);
		list<map<string, object>> province = om.readvalue(fi, new typereference<list<map<string,object>>>() {});
		list<map<string,object>> list = new arraylist<>();
		iterator<map<string, object>> iterator = province.iterator();
		while(iterator.hasnext()) {
			map<string, object> map2 = iterator.next();
			if(map2.get("code").equals(citycode)) {
				map2.remove("code");
				map2.remove("name");
				list=(list<map<string,object>>) map2.get("city");
				break;
			}
			
		}
		list<map<string,object>> list1 = new arraylist<>();
		iterator<map<string,object>> iterator2 = list.iterator();
		while(iterator2.hasnext()) {
			map<string,object> next = iterator2.next();
				if(next.get("code").equals(code)) {
					next.remove("code");
					next.remove("name");
					list1 = (list<map<string,object>>)next.get("area");
				}
		}
		om.writevalue(resp.getwriter(), list1);
		
	}
	
}

完結(jié):只提供了代碼,但是為什么這樣何不自己去想一想?

json地區(qū)文件下載:

關(guān)注公眾號(hào) “程序員零距離” 回復(fù) “201231” 即可下載

↑關(guān)注上方公眾號(hào)回復(fù) “201231” 即可下載↑

思考?可不可以用xml文件來(lái)代替json文件?將jackson換成jsoup來(lái)解析?

到此這篇關(guān)于如何利用ajax實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)詳解的文章就介紹到這了,更多相關(guān)ajax實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)內(nèi)容請(qǐng)搜索碩編程以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持碩編程!

相關(guān)文章