Spring MVC is receiving complex set parameters

Source: Internet
Author: User

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
  1. var idlist = new Array ();
  2. Idlist.push ("1");
  3. Idlist.push ("2");
  4. Idlist.push ("3");
  5. var isbatch = false;
  6. $.ajax ({
  7. Type: "POST",
  8. URL: "<%=path%>/catalog.do?fn=deletecatalogschemes",
  9. DataType: ' JSON ',
  10. Data: {"idlist": Idlist, "Isbatch": Isbatch},
  11. Success:function (data) {
  12. ...
  13. },
  14. Error:function (res) {
  15. ...
  16. }
  17. });

2, Controller method:

Java code
    1. @Controller
    2. @RequestMapping ("/catalog.do")
    3. public class Catalogcontroller {
    4. @RequestMapping (params = "fn=deletecatalogschemes")
    5. @ResponseBody
    6. Public Ajaxjson Deletecatalogschemes (@RequestParam ("idlist[]") list<string> Idlist,boolean Isbatch) {
    7. ...
    8. }
    9. }

    • Receive List<user>, user[] collection parameters:

1. User entity class:

Java code
    1. public class User {
    2. private String name;
    3. Private String pwd;
    4. Omit Getter/setter
    5. }

2, the page JS code:

JS Code
  1. var userlist = new Array ();
  2. Userlist.push ({name: "John Doe", PWD: "123"});
  3. Userlist.push ({name: "Zhang San", pwd: "332"});
  4. $.ajax ({
  5. Type: "POST",
  6. URL: "<%=path%>/catalog.do?fn=saveusers",
  7. Data:JSON.stringify (userlist),//Serializes an object into a JSON string
  8. DataType: "JSON",
  9. ContentType: ' Application/json;charset=utf-8 ',//Set request header information
  10. Success:function (data) {
  11. ...
  12. },
  13. Error:function (res) {
  14. ...
  15. }
  16. });

3, Controller method:

Java code
    1. @Controller
    2. @RequestMapping ("/catalog.do")
    3. public class Catalogcontroller {
    4. @RequestMapping (params = "fn=saveusers")
    5. @ResponseBody
    6. Public Ajaxjson saveusers (@RequestBody list<user> userlist) {
    7. ...
    8. }
    9. }

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
  1. var userlist = new Array ();
  2. Userlist.push ({name: "John Doe", PWD: "123"});
  3. Userlist.push ({name: "Zhang San", pwd: "332"});
  4. $.ajax ({
  5. Type: "POST",
  6. URL: "<%=path%>/catalog.do?fn=saveusers",
  7. Data:JSON.stringify (userlist),//Serializes an object into a JSON string
  8. DataType: "JSON",
  9. ContentType: ' Application/json;charset=utf-8 ',//Set request header information
  10. Success:function (data) {
  11. ...
  12. },
  13. Error:function (res) {
  14. ...
  15. }
  16. });

2, Controller method:

Java code
    1. @Controller
    2. @RequestMapping ("/catalog.do")
    3. public class Catalogcontroller {
    4. @RequestMapping (params = "fn=saveusers")
    5. @ResponseBody
    6. Public Ajaxjson saveusers (@RequestBody list<map<string,object>> listmap) {
    7. ...
    8. }
    9. }

    • Receive the user (the Bean contains list) collection parameters:

1. User entity class:

Java code
    1. public class User {
    2. private String name;
    3. Private String pwd;
    4. Private list<user> customers;//belongs to the user's customer base
    5. Omit Getter/setter
    6. }

2, the page JS code:

JS Code
  1. var customerarray = new Array ();
  2. Customerarray.push ({name: "John Doe", PWD: "123"});
  3. Customerarray.push ({name: "Zhang San", pwd: "332"});
  4. var user = {};
  5. User.Name = "Li Gang";
  6. USER.PWD = "888";
  7. User. Customers = Customerarray;
  8. $.ajax ({
  9. Type: "POST",
  10. URL: "<%=path%>/catalog.do?fn=saveusers",
  11. Data:JSON.stringify (user),//Serializes the object into a JSON string
  12. DataType: "JSON",
  13. ContentType: ' Application/json;charset=utf-8 ',//Set request header information
  14. Success:function (data) {
  15. ...
  16. },
  17. Error:function (res) {
  18. ...
  19. }
  20. });

3, Controller method:

Java code
    1. @Controller
    2. @RequestMapping ("/catalog.do")
    3. public class Catalogcontroller {
    4. @RequestMapping (params = "fn=saveusers")
    5. @ResponseBody
    6. Public Ajaxjson saveusers (@RequestBody user user) {
    7. List<user> customers = User.getcustomers ();
    8. ...
    9. }
    10. }

Spring MVC is receiving complex set parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.