springMVC接收复杂集参数版权声明
原创spring MVC接收收集请求参数时,需要Controller方法的集合参数已在前面添加。@RequestBody,而@RequestBody默认接收enctype (MIME 编码 ) 是 application/json ,因此发送 POST 请求时 需要设置请求标头信息,否则Spring MVC解析收集请求参数时,它不会自动转换。JSON数据被重新解析为相应的集合。以下列表接收List
- 接收List
设置参数:
1、页面js代码:
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 方法:
Java代码
-
@Controller
-
@RequestMapping ( "/catalog.do" )
-
public class CatalogController {
-
@RequestMapping (params = "fn=deleteCatalogSchemes" )
-
@ResponseBody
-
public AjaxJson deleteCatalogSchemes( @RequestParam ( "idList[]" ) List
idList,Boolean isBatch) { -
…
-
}
-
}
- 接收List
、User[]设置参数:
1、User实体类:
Java代码
- public class User {
- private String name;
- private String pwd;
- //省略getter/setter
- }
2 、页面 js 代码:
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 方法:
Java代码
-
@Controller
-
@RequestMapping ( "/catalog.do" )
-
public class CatalogController {
-
@RequestMapping (params = "fn=saveUsers" )
-
@ResponseBody
-
public AjaxJson saveUsers( @RequestBody List
userList) { -
…
-
}
-
}
如果您想接收 User[] 阵列,放好 saveUsers 的参数类型 @RequestBody User[] userArray 就行了。
- 接收 List<Map<String,Object>>设置参数:
1 、页面 js 代码(不需要 User 对象):
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){
- …
- }
- });
2 、 Controller 方法:
Java代码
-
@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实体类:
Java代码
- public class User {
- private String name;
- private String pwd;
- private List
customers; //属于用户的客户群 - //省略getter/setter
- }
2 、页面 js 代码:
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 方法:
Java代码
-
@Controller
-
@RequestMapping ( "/catalog.do" )
-
public class CatalogController {
-
@RequestMapping (params = "fn=saveUsers" )
-
@ResponseBody
-
public AjaxJson saveUsers( @RequestBody User user) {
-
List
customers = user.getCustomers(); -
…
-
}
-
}
原始地址: https://lihaiming.iteye.com/blog/2293840
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除