structs2注解+jsp+ajax实现post异步加载select

104 篇文章 1 订阅

转载自:http://www.2cto.com/kf/201405/299980.html

流程:

1.点击加载btn发起异步请求post

2.后台处理请求返回数据

3.前端获取数据成功,对数据进行处理


前端:

html:首先要导入jq包,不然怎么用ajax呢。

?
1
<script type= "text/javascript" src= "js/jquery.js" ></script>

?
1
2
3
<select name= "warId" id= "warId" style= "height:100%;width:107px" >
     <option value= "" >所有仓库 </option>
</select>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script type= "text/javascript" >
$(document).ready(function(){    //这里实现的是: 点击class为add的按钮时加载仓库列表
     $( ".add" ).click(function(){
             $.post( "xxxx.do" ,function(data){      //要异步加载的action,和成功后的回调
                 var objs = data.warehouses;     //对应action里的属性
                 var selObj = $( "#wareId" );  
                 selObj.empty();
                 for (var i in objs) {
                     var obj = objs[i];
                     addOption(selObj,obj.id,obj.warehouseName); //为Select追加一个Option(下拉项)          
                 }
             }, "json" );   //返回类型
      });
});
function addOption(selObj,value,text){
     var optionstring = "<option value=" \"" " +=" " " \ "=" ">" + text + "</option>" ;
     selObj.append(optionstring); //为Select追加一个Option(下拉项)          
}
</script>

后台structs:

使用注解实现,

要注意的是声明类的时候要加注解:@ParentPackage("json-default")

方法声明注解: @Action(value = "xxxx", results = @Result(name = "success", type="json"))

需要跳转页面就把type = "json" 换成 location = "/mobile/auction_detailed.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
@ParentPackage ( "json-default" )
public class WarHouseAction extends ActionSupport {
     @Autowired
     private WarehouseService warehouseService;
     
     private List<warehouse> warehouses;
     
     /**
      * 后台ajax
      * @return
      * 需要跳转页面就把type = "json" 换成  location = "/mobile/auction_detailed.jsp" 要跳转的页面
      */
     @Action (value = "addGood_before" , results = @Result (name = "success" , type= "json" )) 
     public String addGoodBefore(){
         warehouses = warehouseService.findAllEnableWarehouses();   //这个是返回到页面的
         return "success" ;
     }
 
     public WarehouseService getWarehouseService() {
         return warehouseService;
     }
 
     public void setWarehouseService(WarehouseService warehouseService) {
         this .warehouseService = warehouseService;
     }
 
     public List<warehouse> getWarehouses() {
         return warehouses;
     }
 
     public void setWarehouses(List<warehouse> warehouses) {
         this .warehouses = warehouses;
     }
}
</warehouse></warehouse></warehouse>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值