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)