SpringMVC接收复杂集合对象(参数)代码示例
springmvc在接收集合请求参数时,需要在controller方法的集合参数里前添加@requestbody,而@requestbody默认接收的enctype(mime编码)是application/json,因此发送post请求时需要设置请求报文头信息,否则springmvc在解析集合请求参数时不会自动的转换成json数据再解析成相应的集合。以下列举接收list<string>、list<user>、list<map<string,object>>、user[]、user(bean里面包含list)几种较为复杂的集合参数示例:
接收list<string>集合参数:
1、页面js代码:
js代码
var idlist = new array(); idlist.push(“1”); idlist.push(“2”); idlist.push(“3”); var isbatch = false; $.ajax({ type: "post", url: "<%=path%>/catalog.do?fn=deletecatalogschemes", datatype: 'json', data: {"idlist":idlist,"isbatch":isbatch}, success: function(data){ … }, error: function(res){ … } });
2、controller方法:
java代码
@controller @requestmapping("/catalog.do") public class catalogcontroller { @requestmapping(params = "fn=deletecatalogschemes") @responsebody public ajaxjson deletecatalogschemes(@requestparam("idlist[]") list<string> idlist,boolean isbatch) { … } }
接收list<user>、user[]集合参数:
1、user实体类:
java代码
public class user { private string name; private string pwd; //省略getter/setter }
2、页面js代码:
js代码
var userlist = new array(); userlist.push({name: "李四",pwd: "123"}); userlist.push({name: "张三",pwd: "332"}); $.ajax({ type: "post", url: "<%=path%>/catalog.do?fn=saveusers", data: json.stringify(userlist),//将对象序列化成json字符串 datatype:"json", contenttype : 'application/json;charset=utf-8', //设置请求头信息 success: function(data){ … }, error: function(res){ … } });
3、controller方法:
java代码
@controller @requestmapping("/catalog.do") public class catalogcontroller { @requestmapping(params = "fn=saveusers") @responsebody public ajaxjson saveusers(@requestbody list<user> userlist) { … } }
如果想要接收user[]数组,只需要把saveusers的参数类型改为@requestbodyuser[]userarray就行了。
接收list<map<string,object>>集合参数:
1、页面js代码(不需要user对象了):
js代码
var userlist = new array(); userlist.push({name: "李四",pwd: "123"}); userlist.push({name: "张三",pwd: "332"}); $.ajax({ type: "post", url: "<%=path%>/catalog.do?fn=saveusers", data: json.stringify(userlist),//将对象序列化成json字符串 datatype:"json", contenttype : 'application/json;charset=utf-8', //设置请求头信息 success: function(data){ … }, error: function(res){ … } });
2、controller方法:
java代码
@controller @requestmapping("/catalog.do") public class catalogcontroller { @requestmapping(params = "fn=saveusers") @responsebody public ajaxjson saveusers(@requestbody list<map<string,object>> listmap) { … } }
接收user(bean里面包含list)集合参数:
1、user实体类:
java代码
public class user { private string name; private string pwd; private list<user> customers;//属于用户的客户群 //省略getter/setter }
2、页面js代码:
js代码
var customerarray = new array(); customerarray.push({name: "李四",pwd: "123"}); customerarray.push({name: "张三",pwd: "332"}); var user = {}; user.name = "李刚"; user.pwd = "888"; user. customers = customerarray; $.ajax({ type: "post", url: "<%=path%>/catalog.do?fn=saveusers", data: json.stringify(user),//将对象序列化成json字符串 datatype:"json", contenttype : 'application/json;charset=utf-8', //设置请求头信息 success: function(data){ … }, error: function(res){ … } });
3、controller方法:
java代码
@controller @requestmapping("/catalog.do") public class catalogcontroller { @requestmapping(params = "fn=saveusers") @responsebody public ajaxjson saveusers(@requestbody user user) { list<user> customers = user.getcustomers(); … } }
总结
以上就是本文关于springmvc接收复杂集合对象(参数)代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
springmvc开发restful api之用户查询代码详解
springmvc使用multipartfile 实现异步上传方法介绍
如有不足之处,欢迎留言指出。