Springmvc How to accept various types of parameters transmitted by the front desk? (Collection, entity, single parameter)

Source: Internet
Author: User

Create an entity that contains several types of parameters that need to be accepted. such as entity, List collection, single parameter. (Because SPRINGMVC cannot accept these types of parameters at the same time, you need to create an entity that contains them to accept)

If you receive the user (the Bean contains list) set parameters:

1. User entity class:

1.publicclass  User {  2.    Private String name;    3.    private  String pwd;   4.    private list<user> customers;   customer base  5 that belongs to the user .    // Omit Getter/setter   6.}  

2, the page JS code:

1.varCustomerarray =NewArray (); 2.customerarray.push ({name: "John Doe", PWD: "123"}); 3.customerarray.push ({name: "Zhang San", pwd: "332"}); 4.varuser = {}; 5.user.name = "Li Gang"; 6.USER.PWD = "888"; 7.user. Customers =Customerarray; 8. $.ajax ({9. Type: "POST",  URL: "<%=path%>/catalog.do?fn=saveusers",  Data:JSON.stringify (user),//serializes an object into a JSON stringDataType: "JSON",  ContentType: ' Application/json;charset=utf-8 ',//Set Request header informationSuccess:function(data) {15. ... ..16. },  . Error:function(res) {18. ... ..19. }  20.});

3, Controller method:

1[email protected]  [email protected] ("/catalog.do")  3.  publicclass  catalogcontroller {  4.   5.    @RequestMapping (params = "fn=saveusers")  6.    @ResponseBody  7.     Public Ajaxjson saveusers (@RequestBody user user) {  8.        List<user> customers = user.getcustomers ();   9.        ...   .. Ten.    }   11.}  

Springmvc How to accept various types of parameters transmitted by the front desk? (Collection, entity, single parameter)

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.