Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,并且传值必须JSON.stringify转为json字符串,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合。以下列举接收List<String>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)几种较为复杂的集合参数示例:
一. 参数类型var param = {'list':List<String>,''name":"li","age":12,...}
- var list= new Array();
- list.push(“1”);
- list.push(“2”);
- list.push(“3”);
- $.ajax({
- type: "POST",
- url: "???",
- dataType: 'json',
- data: {"list":list,"name":"li","age":12},
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
List中是基本的数据类型,不是对象,Controller中方法:
public Object do(@RequestParam("list[]") List<String> list,String name,int age) { }
二 .var param = {'list':List<User>,''bossName":"ai","age":12,...}
- var list= new Array();
- list.push({"id":1,"address":"jinan","sex":"male","name":"li"});
- list.push({"id":2,"address":"jinan","sex":"male","name":"gi"});
- list.push({"id":3,"address":"jinan","sex":"male","name":"hi"});
- $.ajax({
- type: "POST",
- url: "???",
- dataType: 'json',
- data: {"list":JSON.stringify(list),"bossName":"li","age":"12"}, //list中是对象 在controller中 使用 //request.getParameter("list")获得这个对象列表
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
Controller中方法 在方法中也可以通过@RequestParam("list")获得String类型的list,或者通过request.getParameter("list")获得,然后再使用Json的工具类把list 转为对象列表 :
public Object do(@RequestParam(HttpServletRequest request, HttpServletResponse response,@RequestParam("list") String s) {
String bossName= request.getParameter("bossName");
String age= request.getParameter("age");
String list= request.getParameter("list");
String age= request.getParameter("age");
String list= request.getParameter("list");
}
三 。接收List<User>、User[]集合参数 参数类型 var param = List<User> ;
-
- var list= new Array();
- list.push({"id":1,"address":"jinan","sex":"male","name":"li"});
- list.push({"id":2,"address":"jinan","sex":"male","name":"gi"});
- list.push({"id":3,"address":"jinan","sex":"male","name":"hi"});
- $.ajax({
- type: "POST",
- url: "???",
- dataType: 'json',
- data: JSON.stringify(list),
- contentType:'application/json;charset=utf-8', // controller中使用@RequestBody接收数据,需要定义contentType,
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
Controller中方法 使用@RequestBody 可以直接得到解析后的对象列表, 如果想要接收User[]数组,只需要把do方法的参数类型改为@RequestBody User[] list就行了。:
public Object do(HttpServletRequest request, HttpServletResponse response,@RequestBody List<User> list) {
List<User> user = list;
User[] user = list; // @RequestBody User[] list
}
四 。
接收
List<Map<String,Object>>
集合参数和情况三类似:
- var list= new Array();
- list.push({"id":1,"address":"jinan","sex":"male","name":"li"});
- list.push({"id":2,"address":"jinan","sex":"male","name":"gi"});
- list.push({"id":3,"address":"jinan","sex":"male","name":"hi"});
- $.ajax({
- type: "POST",
- url: "???",
- dataType: 'json',
- data: JSON.stringify(list),
- contentType:'application/json;charset=utf-8', // controller中使用@RequestBody接收数据,需要定义contentType,
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
Controller中方法
:
public Object do(HttpServletRequest request, HttpServletResponse response,@RequestBodyList<Map<String,Object> list) {
List<Map<String,Object> map= list;
}
五 。 接收Boss (bean里面包含employee )集合参数:
- var employee = new Array();
- employee .push({"id":1,"address":"jinan","sex":"male","name":"li"});
- employee .push({"id":2,"address":"jinan","sex":"male","name":"gi"});
- employee .push({"id":3,"address":"jinan","sex":"male","name":"hi"});
- var boss = {};
- boss.bossName = "ai";
- boss.age = "28";
- boss.employee = list;
- $.ajax({
- type: "POST",
- url: "???",
- dataType: 'json',
- data: JSON.stringify(boss),
- contentType:'application/json;charset=utf-8', // controller中使用@RequestBody接收数据,需要定义contentType,
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
Controller中方法 能够直接获得员工的对象列表
:
public Object do(HttpServletRequest request, HttpServletResponse response,@RequestBody Boss boss) {
List<Employee> employee = boss.getEmployee();
}