SpringMVC在接收集合請求參數(shù)時,需要在Controller方法的集合參數(shù)里前添加@RequestBody,而@RequestBody默認(rèn)接收的enctype(MIME編碼)是application/json,因此發(fā)送POST請求時需要設(shè)置請求報文頭信息,否則SpringMVC在解析集合請求參數(shù)時不會自動的轉(zhuǎn)換成JSON數(shù)據(jù)再解析成相應(yīng)的集合。以下列舉接收List<String>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)幾種較為復(fù)雜的集合參數(shù)示例:
接收List<String>集合參數(shù):
1、頁面js代碼:
Js代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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代碼
1
2
3
4
5
6
7
8
9
10
|
@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[]集合參數(shù):
1、User實體類:
Java代碼
1
2
3
4
5
|
public class User { private String name; private String pwd; //省略getter/setter } |
2、頁面js代碼:
Js代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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' , //設(shè)置請求頭信息 success: function (data){ … }, error: function (res){ … } }); |
3、Controller方法:
Java代碼
1
2
3
4
5
6
7
8
9
10
|
@Controller @RequestMapping( "/catalog.do" ) public class CatalogController { @RequestMapping(params = "fn=saveUsers" ) @ResponseBody public AjaxJson saveUsers(@RequestBody List<User> userList) { … } } |
如果想要接收User[]數(shù)組,只需要把saveUsers的參數(shù)類型改為@RequestBodyUser[]userArray就行了。
接收List<Map<String,Object>>集合參數(shù):
1、頁面js代碼(不需要User對象了):
Js代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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' , //設(shè)置請求頭信息 success: function (data){ … }, error: function (res){ … } }); |
2、Controller方法:
Java代碼
1
2
3
4
5
6
7
8
9
10
|
@Controller @RequestMapping ( "/catalog.do" ) public class CatalogController { @RequestMapping (params = "fn=saveUsers" ) @ResponseBody public AjaxJson saveUsers( @RequestBody List<Map<String,Object>> listMap) { … } } |
接收User(bean里面包含List)集合參數(shù):
1、User實體類:
Java代碼
1
2
3
4
5
6
|
public class User { private String name; private String pwd; private List<User> customers; //屬于用戶的客戶群 //省略getter/setter } |
2、頁面js代碼:
Js代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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' , //設(shè)置請求頭信息 success: function (data){ … }, error: function (res){ … } }); |
3、Controller方法:
Java代碼
1
2
3
4
5
6
7
8
9
10
11
|
@Controller @RequestMapping ( "/catalog.do" ) public class CatalogController { @RequestMapping (params = "fn=saveUsers" ) @ResponseBody public AjaxJson saveUsers( @RequestBody User user) { List<User> customers = user.getCustomers(); … } } |
總結(jié)
以上就是本文關(guān)于SpringMVC接收復(fù)雜集合對象(參數(shù))代碼示例的全部內(nèi)容,希望對大家有所幫助。如有不足之處,歡迎留言指出。
原文鏈接:http://jxd-zxf.iteye.com/blog/2072300