java성 시급 연결 메뉴 실례 코드

본고의 실례는 모두에게 자바성 시급련의 구체적인 코드를 공유하여 참고하도록 하였으며, 구체적인 내용은 다음과 같다.
1.LoadAreaServlet.java

package com.scce.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import net.sf.json.JSONArray;
 
import com.scce.pojo.City;
import com.scce.pojo.Prov;
 
public class LoadAreaServlet extends HttpServlet {
 
   
   
  private static final long serialVersionUID = 1L;
 
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      this.doPost(request, response);
  }
 
   
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();
    ArrayList<Prov> provs = new ArrayList<Prov>();
    String provinceID = request.getParameter("provinceID");
    if (provinceID=="" || provinceID==null) {
      System.out.println(" ");
    Prov pro1 = new Prov();
    pro1.setId(0);
    pro1.setProvinceName(" ");
    Prov pro2 = new Prov();
    pro2.setId(1);
    pro2.setProvinceName(" ");
    Prov pro3 = new Prov();
    pro3.setId(2);
    pro3.setProvinceName(" ");
    Prov pro4 = new Prov();
    pro4.setId(3);
    pro4.setProvinceName(" ");
    provs.add(pro1);
    provs.add(pro2);
    provs.add(pro3);
    provs.add(pro4);
    System.out.println(JSONArray.fromObject(provs).toString()); //[{"id":0,"provinceName":" "},{}]
    out.print(JSONArray.fromObject(provs).toString());
    }else{
      ArrayList<City> citys = new ArrayList<City>();
      if (provinceID.equals("0")) {
        City city1 = new City();
        city1.setId(0);
        city1.setCityName(" ");
        City city2 = new City();
        city2.setId(1);
        city2.setCityName(" ");
        City city3 = new City();
        city3.setId(2);
        city3.setCityName(" ");
        citys.add(city1);
        citys.add(city2);
        citys.add(city3);
      }
      if (provinceID.equals("1")) {
        City city1 = new City();
        city1.setId(0);
        city1.setCityName(" ");
        City city2 = new City();
        city2.setId(1);
        city2.setCityName(" ");
        City city3 = new City();
        city3.setId(2);
        city3.setCityName(" ");
        citys.add(city1);
        citys.add(city2);
        citys.add(city3);
      }
      System.out.println(JSONArray.fromObject(citys).toString());
      out.print(JSONArray.fromObject(citys).toString());
    }  
  }
   
 
}
2.test1.html

<!DOCTYPE html>
<html>
 <head>
  <title>chapter4-test1</title>
   
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="this is my page">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
  
 
 <link rel="stylesheet" href="../UI/themes/icon.css" type="text/css"></link>
  
 <link rel="stylesheet" href="../UI/themes/default/easyui.css" type="text/css"></link>
  
 <!-- <script type="text/javascript" src="../js/jquery-1.8.2.min.js"></script> -->
 <script type="text/javascript" src="../UI/jquery.min.js"></script>
 <script type="text/javascript" src="../UI/jquery.easyui.min.js"></script>
  
 <script type="text/javascript" src="../UI/locale/easyui-lang-zh_CN.js"></script>
 <script type="text/javascript">
 //LoadAreaServlet
  /* var dataCombobox=[{"id":1,"provinceName":" "}, 
    {"id":2,"provinceName":" ","selected":true},
    {"id":3,"provinceName":" "},
    {"id":4,"provinceName":" "}
  ]; */
 $(function () {
       $("#province").combobox({
         width: 150,
         valueField: 'id', //1
         textField: 'provinceName', //                
         editable: false,
        // data:dataCombobox // 
        url:'../LoadAreaServlet',
        method:'post',
        onLoadSuccess:function(data){
           
          var provId = $("#province").combobox("getValue");
           
          loadCity(provId);
        },
        onSelect:function(recode){
          loadCity(recode.id);
        }
         
       });
     });
 
     function loadCity(provId){
       $("#city").combobox({
         width: 150,
         valueField: 'id', //1
         textField: 'cityName', //                
         editable: false,
        // data:dataCombobox // 
        url:'../LoadAreaServlet?provinceID='+provId,
        method:'post'
       });
     }
  
 </script>
 </head>
  
 <body> 
   :
  <select id="province" >
     <option value="0"> ..</option>
        
  </select>
  <br/>
   
   :
  <select id="city" >
     <option value=""> ..</option>
  </select>
  
  
 </body>
</html>
이상은 본고의 전체 내용입니다. 여러분이java프로그래밍을 배우는 데 도움이 되기를 바랍니다.

좋은 웹페이지 즐겨찾기