這篇文章主要介紹怎么利用Ajax實(shí)現(xiàn)地區(qū)三級聯(lián)動,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
站在用戶的角度思考問題,與客戶深入溝通,找到興國網(wǎng)站設(shè)計與興國網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋興國地區(qū)。前言:
利用Ajax來實(shí)現(xiàn)一個地區(qū)的三級聯(lián)動,用Java代碼來讀json文件,先eclipse做一個簡單的,最基礎(chǔ)的。(json我用的jackson來解析,也可用fastjson-阿里巴巴的等還有很多)提供代碼,思路之類的,注釋也沒有自己去想去琢磨出來的思路好
first:首先先要熟悉json文件,并要想好利用什么類型去解析,這是最難的,好找一個沒人的地方戴上耳機(jī)(對于初學(xué))我是用maven來做的用到的jar坐標(biāo) :
redis.clients jedis 2.9.0 compile javax.servlet javax.servlet-api 3.1.0 provided com.fasterxml.jackson.core jackson-databind 2.11.2
文件位置:
second:首先創(chuàng)建一個html文件 three.html
加了一個字體居中和大小的樣式以至于不會太難看,太原生
首先來實(shí)現(xiàn)–省--的局部刷新,利用Ajax
然后來寫對應(yīng)的ProvinceController.class的代碼(主要是邏輯,為什么我要用List
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核心類 String path = req.getServletContext().getRealPath("/WEB-INF/classes/city_code.json");//利用servletContext(也有叫appliaction)來拿到文件的真實(shí)路徑,也可以利用加載器拿都一樣 FileInputStream fi = new FileInputStream(path);//流 List
在three.html添加改變事件
代碼比較簡單,就是跟簡單的清空 賦值 取值
$("#province").on("change", function(){ let code = $(this).find(":selected").val(); $.post("city",{"code":code}, function(data){ $("#city").empty(); $("#city").append(""); $.each(data, function(){ $("#city").append(""); }) },"json"); });
再寫對應(yīng)的CityController.class(代碼類似)我為什么還會強(qiáng)轉(zhuǎn)List
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> province = om.readValue(fi, new TypeReference >>() {}); List
> list = new ArrayList<>(); Iterator > iterator = province.iterator(); while(iterator.hasNext()) { Map map2 = iterator.next(); if(map2.get("code").equals(code)) { map2.remove("code"); map2.remove("name"); list=(List >) map2.get("city"); break; } } List > list1 = new ArrayList<>(); Iterator > iterator2 = list.iterator(); while(iterator2.hasNext()) { Map next = iterator2.next(); next.remove("area"); list1.add(next); } om.writeValue(resp.getWriter(), list1); } }
不懂的話,可以先看一看city_code.json文件,多想一想
繼續(xù)來three.html
$("#city").on("change", function(){ let code = $(this).find(":selected").val(); $.post("village",{"code":code}, function(data){ $("#village").empty(); $("#village").append(""); $.each(data, function(){ $("#village").append(""); }) },"json");
代碼雷同以至于VillageController.class也是雷同,加了一些判斷而已多了一個循環(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> province = om.readValue(fi, new TypeReference >>() {}); List
> list = new ArrayList<>(); Iterator > iterator = province.iterator(); while(iterator.hasNext()) { Map map2 = iterator.next(); if(map2.get("code").equals(cityCode)) { map2.remove("code"); map2.remove("name"); list=(List >) map2.get("city"); break; } } List > list1 = new ArrayList<>(); Iterator > iterator2 = list.iterator(); while(iterator2.hasNext()) { Map next = iterator2.next(); if(next.get("code").equals(code)) { next.remove("code"); next.remove("name"); list1 = (List >)next.get("area"); } } om.writeValue(resp.getWriter(), list1); } }
以上是“怎么利用Ajax實(shí)現(xiàn)地區(qū)三級聯(lián)動”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!