• Ajax 多级联动市县区(街道)基于Struts1.x与Strurts2.x实现


    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、祝技术分享愉快!
     
     
  • 相关阅读:
    leetcode[145]Binary Tree Postorder Traversal
    leetcode[146]LRU Cache
    leetcode[147]Insertion Sort List
    leetcode[148]Sort List
    Intro to WebGL with Three.js
    Demo: Camera and Video Control with HTML5
    js ar
    Jingwei Huang
    Tinghui Zhou
    MODS: Fast and Robust Method for Two-View Matching
  • 原文地址:https://www.cnblogs.com/zihan1120/p/3079316.html
Copyright © 2020-2023  润新知