SPRINGMVC receive complex collection parameters, collection objects

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<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

    1. 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

    1. @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

    1. 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

    1. 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

    1. @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

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.