欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spirngmvc js传递复杂json参数到controller的实例

程序员文章站 2023-11-01 18:52:34
spring mvc在接收集合请求参数时,需要在controller方法的集合参数里前添加@requestbody,而@requestbody默认接收的enctype (m...

spring mvc在接收集合请求参数时,需要在controller方法的集合参数里前添加@requestbody,而@requestbody默认接收的enctype (mime编码)是application/json,因此发送post请求时需要设置请求报文头信息,否则spring mvc在解析集合请求参数时不会自动的转换成json数据再解析成相应的集合。

以下列举接收list<string>、list<user>、list<map<string,object>>、user[]、user(bean里面包含list)几种较为复杂的集合参数示例:

接收list<string>集合参数:

1、页面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方法:

@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实体类:

public class user { 
  private string name; 
 private string pwd; 
 //省略getter/setter 
} 

2、页面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方法:

@controller 
@requestmapping("/catalog.do") 
public class catalogcontroller { 
 
 @requestmapping(params = "fn=saveusers") 
 @responsebody 
 public ajaxjson saveusers(@requestbody list<user> userlist) { 
  … 
 } 
} 

如果想要接收user[]数组,只需要把saveusers的参数类型改为@requestbody user[] userarray就行了。

接收list<map<string,object>>集合参数:

1、页面js代码(不需要user对象了):

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方法:

@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实体类:

public class user { 
 private string name; 
 private string pwd; 
 private list<user> customers;//属于用户的客户群 
 //省略getter/setter 
} 

2、页面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方法:

@controller 
@requestmapping("/catalog.do") 
public class catalogcontroller { 
 
 @requestmapping(params = "fn=saveusers") 
 @responsebody 
 public ajaxjson saveusers(@requestbody user user) { 
  list<user> customers = user.getcustomers(); 
  … 
 } 
} 

以上这篇spirngmvc js传递复杂json参数到controller的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。