Ajax 多級聯動市縣區(街道)基於Struts1.x與Strurts2.x實現

來源:互聯網
上載者:User
Ajax 多級聯動(Struts1.x與Strurts2.x)以市為例子上代碼:(如有問題可以加QQ:2250680083) 1、匯入包json-lib-2.2.3-jdk15.jar,引入jQuery如果連網建議使用Google的,地址為:<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> 2、頁面代碼如下:var url="/" +lemis.WEB_APP_NAME + "/monitor/comEditShi.do";       //初始化市區       function inni_city(){          jQuery.ajax({            url : url,            data:{method: 'ajaxGetCity'},            type : "post",            async: false,            cache : false,            dataType : "json",            success:onchangecallback            });             function onchangecallback(data){                var str="<option value=''>請選擇</option>" ;                for(var i=0;i<data.length;i++){                     if(s8==(data[i].key)){                          str+= "<option selected=true value='"+data[i].key+"'>"+data[i].value+ "</option>";//應對IE6的方法($("").val(值)會有問題)                     }else{                          str+= "<option value='"+data[i].key+"'>"+data[i].value+ "</option>";                     }                }               $( "#所放位子ID").html(str);            }       } 3、配置action(自己配置) 4、Java代碼public class ComEditShi extends ActionLeafSupport {

     /***
     *擷取市區     
     **/
     public ActionForward ajaxGetCity(ActionMapping mapping, ActionForm actionForm,
               HttpServletRequest request, HttpServletResponse response) throws AppException, IOException{
          List<Param> zihanTypes =new ArrayList<Param>();
          AjaxData msg1 = new AjaxData();//自己的方法

AjaxData.java
public class AjaxData {
     /**
     * 省廳監測,擷取市
     * @author 紫寒
     * @return
     */
     public LinkedHashMap getShi_st() {
          AjaxDataDAO rd = new AjaxDataDAO();
          try {
               return rd.getShi_st();//自己方法

     public LinkedHashMap getShi_st() throws SQLException {
          LinkedHashMap map = getData("ajax_shi",null); 

//市縣區 紫寒
     private LinkedHashMap getData(String string,String string2){
          LinkedHashMap map = new LinkedHashMap();
          Sc01 aSc01 = new Sc01();
          Sc01 aSc01_ = new Sc01();
          aSc01.setFileKey(string);
          if(string2!=null){
               aSc01.setAab003(string2);
          }
            Connection con = null;
     try {
               con = DBUtil. getConnection();
               List<?> list = (List<?>) find(con, aSc01, null, 0);
               for (int i = 0; i < list.size(); i++) {
                    ClassHelper.copyProperties(list.get(i), aSc01_);
                    map.put(aSc01_.getAab003(), aSc01_.getAab300());//主要的一步
               }
                  
          } catch (NoConnectionException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (AppException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }  
            return map;
          
     }
 

                 return map;
     }

 

          } catch (SQLException e) {
               e.printStackTrace();
          }
          return null;
     }
}

 

          LinkedHashMap mapaab061 = msg1.getShi_st();
          Set<Map.Entry<String, String>> entryseSet=mapaab061.entrySet();  
          for (Map.Entry<String, String> entry:entryseSet) { 
               Param param = new Param();

 Param.java
public class Param {           
    private Object entity;  //預留   
    private String value; 
    private String key;   
   
    public Param() {     
    }         
    public Param(String value, String key) { 
        this.value = value;   
        this.key = key;   
    }       
    public String getValue() {   
        return value;   
    }
       
    public void setValue(String value) {   
        this.value = value;   
    }   
   
       
    public String getkey() {   
        return key;   
    }   
   
       
    public void setkey(String key) {   
        this.key = key;   
    }   
   
       
    public Object getEntity() {   
        return entity;   
    }   
   
       
    public void setEntity(Object entity) {   
        this.entity = entity;   
    }   
   
 

               param.setkey(entry.getKey());
               param.setValue(entry.getValue());
               zihanTypes.add(param);     
               }          
          JSONArray jsonObj = JSONArray.fromObject(zihanTypes);
          sendMsg(jsonObj.toString(),response);  //Structs1.x         // sendMsg(jsonObj.toString());  //Structs2.x 

Structs1.x Structs2.x
private void sendMsg(String string, HttpServletResponse response) {
     // TODO Auto-generated method stub
             response.setCharacterEncoding("UTF-8");       
             try {
                    response.getWriter().write(string);
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }  
     
}
private void sendMsg(String string) {
 HttpServletResponse response = ServletActionContext.getResponse();       
             response.setCharacterEncoding("UTF-8");       
             response.getWriter().write(string);  
}
 

          return null;
     }

}5、相應的級聯方法用jQuery的onchange事件就OK了!6、祝技術分享愉快!

 

 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.