struts2+json+jquery做联动

在网上找了些资料,调试出来。总结如下:

1. jquery包导入:jquery-1.2.6.min.js

2.在页面写上这句:<script src="jquery-1.2.6.min.js" type="text/javascript"></script> 

3.自己写的JavaScript如下:

 

function getMDepartment(){  
                var citycode = $("select[name='citycode']").val();  
                if(citycode){  
                    $.getJSON("getdepartment.action",{citycode:citycode},function(json){  
                        var mdepartment = $("select[name='mdepartment']");  
                        mdepartment.empty();  
                        mdepartment.append('<option value="null">--部门名称--</option>');  
                        $.each( json, function(index, entry){  
                         mdepartment.append('<option value="' + entry['id'] + '">' + entry['name'] + '</option>');  
                        });   
                    });  
                }  
            };


            function getSubDepartment(){  
                var mdepart = $("select[name='mdepartment']").val();  
                if(mdepart){  
                    $.getJSON("getsubdepartment.action",{mdepartment:mdepart},function(json){  
                        var sdepartment = $("select[name='sdepartment']");  
                        sdepartment.empty();  
                        sdepartment.append('<option value="null">--子部门名称--</option>');  
                        $.each( json, function(index, entry){  
                         sdepartment.append('<option value="' + entry['id'] + '">' + entry['name'] + '</option>');  
                        });   
                    });  
                }  
            };

 

 

4.页面的代码如下:

 

<select name="citycode" onChange="getMDepartment()">
       <option value="null" selected="selected">请选择分公司</option>
        <option  value ="0127" style="height: 16px;">武汉</option>
        <option  value ="0713" style="height: 16px;">黄冈</option>
        <option  value ="0710" style="height: 16px;">襄樊</option>
        <option  value ="0716" style="height: 16px;">荆州</option>
        <option  value ="0717" style="height: 16px;">宜昌</option>
        <option  value ="0712" style="height: 16px;">孝感</option>
        <option  value ="0719" style="height: 16px;">十堰</option>
        <option  value ="0714" style="height: 16px;">黄石</option>
        <option  value ="0711" style="height: 16px;">鄂州</option>
        <option  value ="0724" style="height: 16px;">荆门</option>
        <option  value ="0715" style="height: 16px;">咸宁</option>
        <option  value ="0703" style="height: 16px;">潜江</option>
        <option  value ="0718" style="height: 16px;">恩施</option>
        <option  value ="0722" style="height: 16px;">随州</option>
        <option  value ="0728" style="height: 16px;">仙桃</option>
        <option  value ="0702" style="height: 16px;">天门</option>
        <option  value ="0701" style="height: 16px;">神农架林区</option>
         </select>
       
       <select name="mdepartment" onChange="getSubDepartment()"></select>
       <select name="sdepartment" ></select>

 

5. 在两个js方法调用的action配置如下:

   <action name="getdepartment" method="getDepartment" class="com.huang.json.JsonAction"> 
          
       </action>
       <action name="getsubdepartment" method="getSubDepartment" class="com.huang.json.JsonAction"> 
          
       </action>

没有返回的

6.action中的方法如下:

 

public void getDepartment() throws IOException{
      
       HttpServletResponse response = ServletActionContext.getResponse();        
          response.setCharacterEncoding("UTF-8");        
          int cicode = Integer.parseInt(citycode);
          System.out.println(citycode);
          List<Department> departlist=new ArrayList<Department>();
          Department de=new Department();
          de.setId("1");
          de.setName("huang");
          Department de2=new Department();
          de2.setId("1");
          de2.setName("huang");
          departlist.add(de);
          departlist.add(de2);
         
         
          StringBuffer json = new StringBuffer();  
          json.append("["); 
          for(int i=0;i<departlist.size();i++){
           json.append("{id:/"" + departlist.get(i).getId() + "/",name:/"" + departlist.get(i).getName() + "/"},"); 
          }
           
          json.deleteCharAt(json.length()-1);  
          json.append("]");  
            System.out.println(json.toString());
          response.getWriter().write(json.toString());  

     }
    
    
     public void getSubDepartment() throws IOException{
      
       HttpServletResponse response = ServletActionContext.getResponse();        
          response.setCharacterEncoding("UTF-8");        
          int mdepart = Integer.parseInt(mdepartment);  
          List<Department> departlist=new ArrayList<Department>();
          Department de=new Department();
          de.setId("1");
          de.setName("huang");
          Department de2=new Department();
          de2.setId("1");
          de2.setName("huang");
          departlist.add(de);
          departlist.add(de2);
         
         
          StringBuffer json = new StringBuffer();  
          json.append("["); 
          for(int i=0;i<departlist.size();i++){
           json.append("{id:/"" + departlist.get(i).getId() + "/",name:/"" + departlist.get(i).getName() + "/"},"); 
          }
           
          json.deleteCharAt(json.length()-1);  
          json.append("]");  
            System.out.println(json.toString());
          response.getWriter().write(json.toString());  

     }

 

 

这里struts2提供action 做处理而已,返回一个json形式的字符串,然后jquery 得到这个字符串后解析出来得到相应的值。jquery目前还不是清楚,需要好好的研究研究。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值