Spring MVC needs to add @requestbody to the collection parameters of the Controller method when it receives the collection request parameters, whereas the Enctype (MIME encoding) @requestbody receives by default is Application/json. Therefore, the request header information needs to be set when the POST request is sent, otherwise spring MVC does not automatically convert the JSON data into the corresponding collection when parsing the collection request parameters. The following lists receive list<integer>, list<user>, list<map<string,object>>, user[], User (bean contains List) Some examples of more complex set parameters:
- First, receive list<integer> set parameters:
1, the page JS code:
JS Code
var arr = [1,2,3];$.jbox.confirm ("OK to delete data? "," Warning ", function () { $.ajax ({ type: ' Get ', URL: ' ${base.contextpath}/giving/index/del ', DataType: ' JSON ', data: {Ids:arr}, success:function (result) { ... }, error:function (Result) { (... }) })
2, Controller method:
Java code
@Controller @requestmapping ("/wxgiving") public class wxgivingcontroller{ @RequestMapping (value = "/index/del", method = Requestmethod.get) @ResponseBody public returnmsg del (@RequestParam (value = "ids[]") List < integer> IDs) { ... }}
- Receive List<user>, user[] collection parameters:
1. User entity class:
Java code
public class User { private int id; private String name; Private String pwd; Omit Getter/setter}
2, the page JS code:
JS Code
You can find the front end to spell this type var userlist = new Array (), Userlist.push ({name: "Zhang San", pwd: "123"}), Userlist.push ({name: "John Doe", pwd: "223"}); $.ajax ({type: "POST", url: "${base.contextpath}/user/index/add", Data:JSON.stringify (userlist),//Serializes the object into a JSON string DataType: "JSON", ContentType: ' Application/json;charset=utf-8 ',//Set request header information Success:function (result) {... }, Error:function (Result) {...} });
3, Controller method:
Java code
@Controller @requestmapping (value = "/user") public class Usercontroller () { @RequestMapping (value = "/index/add", method = requestmethod.post) @ResponseBody public returnmsg addoredit (@RequestBody list<user> userlist ) { ... }}
If you want to receive an user[] array, simply change the parameter type of add to @requestbody user[] Userarray.
- Receive list<map<string,object>> Collection parameters:
1, the page JS code (do not need the user object):
JS Code
var userlist = new Array (), Userlist.push ({name: "Zhang San", pwd: "123"}), Userlist.push ({name: "John Doe", pwd: "223"}); $.ajax ({type: "POST", url: "${base.contextpath}/user/index/add", Data:JSON.stringify (userlist),//Serializes the object into a JSON string dataType: "Jso N ", ContentType: ' Application/json;charset=utf-8 ',//Set request header information Success:function (Result) {...}, Error:fu Nction (Result) {...} });
2, Controller method:
Java code
@Controller @requestmapping (value = "/user") public class Usercontroller () { @RequestMapping (value = "/index/add", method = requestmethod.post) @ResponseBody public returnmsg addoredit (@RequestBody list<map<string, Object>> listmap) { ... }}
- Receive the user (the Bean contains list) collection parameters:
1. User entity class:
Java code
public class User { private int id; private String name; Private String pwd; Private list<user> userlist; Omit Getter/setter}
2, the page JS code:
JS Code
var userarray= new Array (); Userarray.push ({name: "Zhang San", pwd: "123"}); Userarray.push ({name: "John Doe", pwd: "223"}); var user = {} ; user.name = "Harry"; user.pwd = "888"; user.userlist= userarray;$.ajax ({type: "POST", url: "${base.contextpath}/user/in Dex/add ", data:JSON.stringify (user),//Serializes the object into a JSON string DataType:" JSON ", ContentType: ' Application/json;charset=ut F-8 ',//Set request header information Success:function (Result) {...}, error:function (Result) {...} });
3, Controller method:
Java code
@Controller @requestmapping (value = "/user") public class Usercontroller () { @RequestMapping (value = "/index/add", method = requestmethod.post) @ResponseBody public returnmsg addoredit (@RequestBody user user) { list< User> userlist= user.getuserlist ();
SPRINGMVC receive complex collection parameters, collection objects