• 地址的三级联动(jQuery+servlet)


    明人不说暗话,直接上代码。

    jsp页面代码:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>注册用户</title>
    </head>
    <script src="js/jquery-1.11.3.min.js"></script>
    <body>
    <form id="frm">
     
            住址:
            <select name="province" class="province">
                <option selected="selected" value="0">请选择</option>
            </select>
            <select name="city" class="city">
                <option selected="selected" value="0">请选择</option>
            </select>
            <select name="area" class="area">
                <option selected="selected" value="0">请选择</option>
            </select>
      
    </form>
    </body>
    <script>
       
            //初始化省级
            $.ajax({
                url:"AddressServlet",
                type:"post",
                dataType:"text",
                data:"",
                success:function (data) {
                    //将json字符串转化为json对象
                    var json=JSON.parse(data);
                    //遍历出省
                    for(var i=0;i<json.length;i++){
                        $(".province").append("<option value="+json[i].pId+">"+
                            json[i].pName+
                            "</option>");
                    }
                },
                error:function () {
                    alert("服务器内部错误!");
                }
            });
    
         
            //省改变时触发
            $(".province").change(function () {
                var pId=$(this).val();
                $.ajax({
                    url:"CitiesEchoServlet",
                    type:"post",
                    dataType:"text",
                    data:{pId:pId},
                    success:function (data) {
                        //将json字符串转化为json对象
                        var json=JSON.parse(data);
                        //清空市和区
                        $(".city").html("<option selected='selected' value='0'>请选择</option>");
                        $(".area").html("<option selected='selected' value='0'>请选择</option>");
                        //遍历出市
                        for(var i=0;i<json.length;i++){
                            $(".city").append("<option value="+json[i].cId+">"+
                                            json[i].cName+
                                            "</option>");
                        }
                    },
                    error:function () {
                        alert("服务器内部错误!");
                    }
                });
    
            });
    
            //市改变时触发
            $(".city").change(function () {
                var cId=$(this).val();
                $.ajax({
                    url:"AreasEchoServlet",
                    type:"post",
                    dataType:"text",
                    data:{cId:cId},
                    success:function (data) {
                        var json=JSON.parse(data);
                        $(".area").html("<option selected='selected' value='0'>请选择</option>");
                        for(var i=0;i<json.length;i++){
                            $(".area").append("<option value="+json[i].areaId+">"+
                                json[i].areaName+
                                "</option>");
                        }
                    },
                    error:function () {
                        alert("服务器内部错误!");
                    }
                });
            });
        })
    </script>
    </html>

    从后台获取省级数据的servlet

    package servlet;
    
    
    import bean.Province;
    import com.alibaba.fastjson.JSON;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import service.ProvinceService;
    
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    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 java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    @WebServlet(value = "/AddressServlet")
    public class AddressServlet extends HttpServlet {
    
        @Autowired
        private ProvinceService ps;
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            List<Province> provs=ps.provinceList();
            out.write(JSON.toJSONString(provs));//这里是使用了FastJson中的方法将list集合解析成json字符串
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
        //这里是重写httpservlet类中的init方法,从ioc容器中获得ProvinceServiceImpl对象
        @Override
        public void init(ServletConfig config) {
            ServletContext sc = config.getServletContext();
            ps=(ProvinceService) ((ClassPathXmlApplicationContext)sc.getAttribute("context")).getBean("provinceServiceImpl");
        }
    }

    获得市级数据的servlet:

    package servlet;
    
    import bean.City;
    import com.alibaba.fastjson.JSON;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import service.CityService;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    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 java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    @WebServlet(value = "/CitiesEchoServlet")
    public class CitiesEchoServlet extends HttpServlet {
    
        @Autowired
        private CityService cs;
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String pId=request.getParameter("pId");
            List<City> citiesById=cs.citiesById(Integer.parseInt(pId));
            out.write(JSON.toJSONString(citiesById));//将list集合转化为json字符串并返回
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
        @Override
        public void init(ServletConfig config) {
            ServletContext sc = config.getServletContext();
            cs=(CityService)((ClassPathXmlApplicationContext)sc.getAttribute("context")).getBean("cityServiceImpl");
        }
    }

    获得区级的servlet:

    package servlet;
    
    import bean.Area;
    import com.alibaba.fastjson.JSON;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import service.AreaService;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    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 java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    @WebServlet(value = "/AreasEchoServlet")
    public class AreasEchoServlet extends HttpServlet {
    
        @Autowired
        private AreaService as;
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String cId=request.getParameter("cId");
            List<Area> areasById=as.areasById(Integer.parseInt(cId));
            out.write(JSON.toJSONString(areasById));//将list集合转化为json字符串并返回
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
        @Override
        public void init(ServletConfig config) {
            ServletContext sc = config.getServletContext();
            as=(AreaService)((ClassPathXmlApplicationContext)sc.getAttribute("context")).getBean("areaServiceImpl");
        }
    }
  • 相关阅读:
    Python网络爬虫四大选择器(正则表达式、BS4、Xpath、CSS)总结
    如何利用CSS选择器抓取京东网商品信息
    如何利用Xpath抓取京东网商品信息
    如何利用BeautifulSoup选择器抓取京东网商品信息
    利用Python正则表达式抓取京东网商品信息
    jacoco查看覆盖率
    Centos7.X 搭建Grafana+Jmeter+Influxdb 性能实时监控平台(不使用docker)
    记录工作中遇到的BUG,经典的数据库时区问题和字段类型tinyint(1)问题
    jmeter分布式压测
    Linux性能优化思路
  • 原文地址:https://www.cnblogs.com/leafarmyarmy/p/10451310.html
Copyright © 2020-2023  润新知