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<string>, list<user>, list<map<string,object>>, user[], User (bean contains List) Some examples of more complex set parameters:
- Receive list<string> Collection parameters:
1, the page JS code:
JS Code
- 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 method:
Java code
- @Controller
- @RequestMapping ("/catalog.do")
- public class Catalogcontroller {
- @RequestMapping (params = "fn=deletecatalogschemes")
- @ResponseBody
- Public Ajaxjson Deletecatalogschemes (@RequestParam ("idlist[]") list<string> Idlist,boolean Isbatch) {
- ...
- }
- }
- Receive List<user>, user[] collection parameters:
1. User entity class:
Java code
- public class User {
- private String name;
- Private String pwd;
- Omit Getter/setter
- }
2, the page JS code:
JS Code
- var userlist = new Array ();
- Userlist.push ({name: "John Doe", PWD: "123"});
- Userlist.push ({name: "Zhang San", pwd: "332"});
- $.ajax ({
- Type: "POST",
- URL: "<%=path%>/catalog.do?fn=saveusers",
- Data:JSON.stringify (userlist),//Serializes an object into a JSON string
- DataType: "JSON",
- ContentType: ' Application/json;charset=utf-8 ',//Set request header information
- Success:function (data) {
- ...
- },
- Error:function (res) {
- ...
- }
- });
3, Controller method:
Java code
- @Controller
- @RequestMapping ("/catalog.do")
- public class Catalogcontroller {
- @RequestMapping (params = "fn=saveusers")
- @ResponseBody
- Public Ajaxjson saveusers (@RequestBody list<user> userlist) {
- ...
- }
- }
If you want to receive user[] arrays, simply change the saveusers parameter type 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: "John Doe", PWD: "123"});
- Userlist.push ({name: "Zhang San", pwd: "332"});
- $.ajax ({
- Type: "POST",
- URL: "<%=path%>/catalog.do?fn=saveusers",
- Data:JSON.stringify (userlist),//Serializes an object into a JSON string
- DataType: "JSON",
- ContentType: ' Application/json;charset=utf-8 ',//Set request header information
- Success:function (data) {
- ...
- },
- Error:function (res) {
- ...
- }
- });
2, Controller method:
Java code
- @Controller
- @RequestMapping ("/catalog.do")
- public class Catalogcontroller {
- @RequestMapping (params = "fn=saveusers")
- @ResponseBody
- Public Ajaxjson saveusers (@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 String name;
- Private String pwd;
- Private list<user> customers;//belongs to the user's customer base
- Omit Getter/setter
- }
2, the page JS code:
JS Code
- var customerarray = new Array ();
- Customerarray.push ({name: "John Doe", PWD: "123"});
- Customerarray.push ({name: "Zhang San", pwd: "332"});
- var user = {};
- User.Name = "Li Gang";
- USER.PWD = "888";
- User. Customers = Customerarray;
- $.ajax ({
- Type: "POST",
- URL: "<%=path%>/catalog.do?fn=saveusers",
- Data:JSON.stringify (user),//Serializes the object into a JSON string
- DataType: "JSON",
- ContentType: ' Application/json;charset=utf-8 ',//Set request header information
- Success:function (data) {
- ...
- },
- Error:function (res) {
- ...
- }
- });
3, Controller method:
Java code
- @Controller
- @RequestMapping ("/catalog.do")
- public class Catalogcontroller {
- @RequestMapping (params = "fn=saveusers")
- @ResponseBody
- Public Ajaxjson saveusers (@RequestBody user user) {
- List<user> customers = User.getcustomers ();
- ...
- }
- }
Spring MVC is receiving complex set parameters