【经典】springmvc controller接收复杂参数

Java 专栏收录该内容
79 篇文章 0 订阅

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合。以下列举接收List<String>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)几种较为复杂的集合参数示例:

  • 接收List<String>集合参数:

1、页面js代码:

var idList = new Array();  
idList.push("1");   
idList.push("2");   
idList.push("3");  
var isBatch = false;  
$.ajax({  
    type: "POST",  
    url: "<%=path%>/catalog.do?fn=deleteCatalogSchemes",  
    dataType: 'json',  
    data: {"idList[]":idList,"isBatch":isBatch},  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  

2、Controller方法:

@Controller  
@RequestMapping("/catalog.do")  
public class CatalogController {  
  
    @RequestMapping(params = "fn=deleteCatalogSchemes")  
    @ResponseBody  
    public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") List<String> idList,Boolean isBatch) {  
            …  
    }  
}  
  •  接收List<User>、User[]集合参数:

 

1、User实体类:

public class User {  
        private String name;   
    private String pwd;  
    //省略getter/setter  
}  

2、页面js代码:

var userList = new Array();  
userList.push({name: "李四",pwd: "123"});   
userList.push({name: "张三",pwd: "332"});   
$.ajax({  
    type: "POST",  
    url: "<%=path%>/catalog.do?fn=saveUsers",  
    data: JSON.stringify(userList),//将对象序列化成JSON字符串  
    dataType:"json",  
    contentType : 'application/json;charset=utf-8', //设置请求头信息  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  


3、Controller方法:
 

@Controller  
@RequestMapping("/catalog.do")  
public class CatalogController {  
  
    @RequestMapping(params = "fn=saveUsers")  
    @ResponseBody  
    public AjaxJson saveUsers(@RequestBody List<User> userList) {  
        …  
    }  
}  
如果想要接收User[]数组,只需要把saveUsers的参数类型改为@RequestBody User[] userArray就行了。

 

 

 

 

  • 接收List<Map<String,Object>>集合参数:

 

 1、页面js代码(不需要User对象了):

var userList = new Array();  
userList.push({name: "李四",pwd: "123"});   
userList.push({name: "张三",pwd: "332"});   
$.ajax({  
    type: "POST",  
    url: "<%=path%>/catalog.do?fn=saveUsers",  
    data: JSON.stringify(userList),//将对象序列化成JSON字符串  
    dataType:"json",  
    contentType : 'application/json;charset=utf-8', //设置请求头信息  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  
2、Controller方法:
@Controller  
@RequestMapping("/catalog.do")  
public class CatalogController {  
  
    @RequestMapping(params = "fn=saveUsers")  
    @ResponseBody  
    public AjaxJson saveUsers(@RequestBody List<Map<String,Object>> listMap) {  
        …  
    }  
}  
  •  接收User(bean里面包含List)集合参数:

 1、User实体类:

public class User {  
    private String name;   
    private String pwd;  
    private List<User> customers;//属于用户的客户群  
    //省略getter/setter  
}  


2、页面js代码:

var customerArray = new Array();  
customerArray.push({name: "李四",pwd: "123"});   
customerArray.push({name: "张三",pwd: "332"});   
var user = {};  
user.name = "李刚";  
user.pwd = "888";  
user. customers = customerArray;  
$.ajax({  
    type: "POST",  
    url: "<%=path%>/catalog.do?fn=saveUsers",  
    data: JSON.stringify(user),//将对象序列化成JSON字符串  
    dataType:"json",  
    contentType : 'application/json;charset=utf-8', //设置请求头信息  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  

3、Controller方法:

@Controller  
@RequestMapping("/catalog.do")  
public class CatalogController {  
  
    @RequestMapping(params = "fn=saveUsers")  
    @ResponseBody  
    public AjaxJson saveUsers(@RequestBody User user) {  
        List<User> customers = user.getCustomers();  
        …  
    }  
}  

 

转载地址: 

http://jxd-zxf.iteye.com/blog/2072300/

https://blog.csdn.net/huaieli1/article/details/77839043

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值