创建一个实体:里面包含需要接受的多种类型的参数。如实体、list集合、单个参数。(因为springMVC无法同时接受这几种参数,需要创建一个实体包含它们来接受)
如接收User(bean里面包含List)集合参数:
1、User实体类:
1.public class User { 2. private String name; 3. private String pwd; 4. private List<User> customers;//属于用户的客户群 5. //省略getter/setter 6.}
2、页面js代码:
1.var customerArray = new Array(); 2.customerArray.push({name: "李四",pwd: "123"}); 3.customerArray.push({name: "张三",pwd: "332"}); 4.var user = {}; 5.user.name = "李刚"; 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),//将对象序列化成JSON字符串 12. dataType:"json", 13. contentType : ‘application/json;charset=utf-8‘, //设置请求头信息 14. success: function(data){ 15. … 16. }, 17. error: function(res){ 18. … 19. } 20.});
3、Controller方法:
1[email protected] [email protected]("/catalog.do") 3.public class CatalogController { 4. 5. @RequestMapping(params = "fn=saveUsers") 6. @ResponseBody 7. public AjaxJson saveUsers(@RequestBody User user) { 8. List<User> customers = user.getCustomers(); 9. … 10. } 11.}