制作一个省份的三级联动菜单

it2025-10-30  8

linkage.jsp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 <%@ page language= "java"  contentType= "text/html; charset=UTF-8"      pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type"  content= "text/html; charset=UTF-8" > <title>三级菜单联动</title> <script type= "text/javascript" >      getProvince();            function getProvince(){          var xmlHttp;          if (window.XMLHttpRequest){              xmlHttp= new  XMLHttpRequest();          } else {              xmlHttp= new  ActiveXObject( "Microsoft.XMLHTTP" ) ;          }                    document.getElementById( "province" ).length= 1 ;                    xmlHttp.open( "post" , "getProvince" );          xmlHttp.onreadystatechange=function(){              if (xmlHttp.readyState== 4  && xmlHttp.status== 200 ){                  var data=eval( "(" +xmlHttp.responseText+ ")" );                  alert(data.rows[ 0 ].areaName);                  for (var i= 0 ;i<data.rows.length;i++){                      document.getElementById( "province" ).innerHTML+= "<option value='" +data.rows[i].areaCode+ "'>" +data.rows[i].areaName+ "</option>" ;                   }              }          }          xmlHttp.send();      }        function getCity() {                }      function getCounty() {                }       </script> </head> <body> <span>选择省:</span> <select id= "province"  onchange= "getCity()" >      <option value= "-1" >-请选择省份-</option> </select>   <span>选择市:</span> <select id= "city"  onchange= "getCounty()" >      <option value= "-1" >-请选择城市-</option> </select>     <span>选择区县:</span> <select id= "" >      <option value= "-1" >-请选择区县-</option> </select> </body> </html>

   DbUtil.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package  com.oracle.util;   import  java.sql.Connection; import  java.sql.DriverManager;   public  class  DbUtil {      private  static  String jdbcName= "com.mysql.jdbc.Driver" ;      private  static  String url= "jdbc:mysql://127.0.0.1:3306/chinastates" ;      private  static  String user= "root" ;      private  static  String password= "zbsy201005" ;      public  Connection getConn()  throws  Exception{          Class.forName(jdbcName);          Connection conn=DriverManager.getConnection(url, user, password);          return  conn;      }      public  void  close(Connection conn)  throws  Exception{          if  (conn!= null ) {              conn.close();          }      }      public  static  void  main(String[] args)  throws  Exception{          DbUtil dbUtil= new  DbUtil();          Connection conn=dbUtil.getConn();          if (conn!= null ){              System.out.println( "数据库连接成功!" );          }          dbUtil.close(conn);      } }

  ResponceUtil.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package  com.oracle.util;   import  java.io.PrintWriter;   import  javax.servlet.http.HttpServletResponse;   import  net.sf.json.JSONObject;   public  class  ResponseUtil {        public  static  void  write(HttpServletResponse response,JSONObject jsonObject) throws  Exception{          response.setContentType( "text/html;charset=utf-8" );          PrintWriter out=response.getWriter();          out.println(jsonObject.toString());          out.flush();          out.close();      } }

 Chinastates.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 package  com.oracle.entity;   public  class  Chinastates {      private  String areaCode;      private  String areaName;      private  String parentAreaCode;      private  Integer root;      private  Integer zone;      private  String navigateURL;            public  Chinastates() {          super ();      }        public  String getAreaCode() {          return  areaCode;      }        public  void  setAreaCode(String areaCode) {          this .areaCode = areaCode;      }        public  String getAreaName() {          return  areaName;      }        public  void  setAreaName(String areaName) {          this .areaName = areaName;      }        public  String getParentAreaCode() {          return  parentAreaCode;      }        public  void  setParentAreaCode(String parentAreaCode) {          this .parentAreaCode = parentAreaCode;      }        public  Integer getRoot() {          return  root;      }        public  void  setRoot(Integer root) {          this .root = root;      }        public  Integer getZone() {          return  zone;      }        public  void  setZone(Integer zone) {          this .zone = zone;      }        public  String getNavigateURL() {          return  navigateURL;      }        public  void  setNavigateURL(String navigateURL) {          this .navigateURL = navigateURL;      } }

  

ChinastatesDao.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package  com.oracle.dao;   import  java.sql.Connection; import  java.sql.PreparedStatement; import  java.sql.ResultSet; import  java.util.ArrayList; import  java.util.List;   import  com.oracle.entity.Chinastates;   public  class  ChinastatesDao{      public  List<Chinastates> find(Connection conn)  throws  Exception {          List<Chinastates> list= new  ArrayList<Chinastates>();          String sql= "select AreaCode,AreaName from chinastates where ParentAreaCode=0001" ;          PreparedStatement pst=conn.prepareStatement(sql);          ResultSet rs=pst.executeQuery();          while  (rs.next()) {              Chinastates chinastates= new  Chinastates();              chinastates.setAreaCode(rs.getString( "AreaCode" ));              chinastates.setAreaName(rs.getString( "AreaName" ));              list.add(chinastates);          }          return  list;                } }

  GetProvinceServlet.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 package  com.oracle.controller;   import  java.io.IOException; import  java.sql.Connection; import  java.util.List;   import  javax.servlet.ServletException; import  javax.servlet.http.HttpServlet; import  javax.servlet.http.HttpServletRequest; import  javax.servlet.http.HttpServletResponse;   import  com.oracle.dao.ChinastatesDao; import  com.oracle.entity.Chinastates; import  com.oracle.util.DbUtil; import  com.oracle.util.ResponseUtil;   import  net.sf.json.JSONArray; import  net.sf.json.JSONObject;   public  class  GetProvinceServlet  extends  HttpServlet {      private  static  final  long  serialVersionUID = 1L;            private  DbUtil dbUtil= new  DbUtil();      private  ChinastatesDao chinastatesDao= new  ChinastatesDao();              @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 {          Connection conn= null ;          try  {                  conn=dbUtil.getConn();                  resp.setCharacterEncoding( "utf-8" );                  List<Chinastates> list=chinastatesDao.find(conn);                                    JSONArray jsonArray= new  JSONArray();                  for (Chinastates chinastates:list){                      JSONObject jsonObject= new  JSONObject();                      jsonObject.put( "areaCode" ,chinastates.getAreaCode());                      jsonObject.put( "areaName" ,chinastates.getAreaName());                      jsonArray.add(jsonObject);                  }                  JSONObject result= new  JSONObject();                  result.put( "rows" ,jsonArray);  //rows多行                                    ResponseUtil.write(resp, result);            }  catch  (Exception e) {              e.printStackTrace();          } finally {              try  {                  dbUtil.close(conn);              }  catch  (Exception e) {                  e.printStackTrace();              }          }   } }

转载于:https://www.cnblogs.com/gaowc/p/10701086.html

相关资源:关于国家省份城市的三级联动
最新回复(0)