Home  >  Article  >  Web Front-end  >  js implements passing complex json parameters to controller

js implements passing complex json parameters to controller

小云云
小云云Original
2018-03-30 09:39:481581browse

When Spring MVC receives collection request parameters, it is necessary to add @RequestBody before the collection parameters of the Controller method. The enctype (MIME encoding) received by @RequestBody by default is application/json, so the request report needs to be set when sending a POST request. header information, otherwise Spring MVC will not automatically convert it into JSON data and then parse it into the corresponding collection when parsing the collection request parameters.

The following are some relatively complex collections that receive Listf7e83be87db5cd2d9a8a0b8117b38cd4, List4c8e0c17c3bd7e0081bb17cc795e1984, Lista06c5c2dfccfdea21bf0b155d9343663>, User[], and User (bean contains List) Parameter example:

Receive Listf7e83be87db5cd2d9a8a0b8117b38cd4 Collection parameter:

1. Page js code:

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: &#39;json&#39;, 
 data: {"idList":idList,"isBatch":isBatch}, 
 success: function(data){ 
  … 
 }, 
 error: function(res){ 
  … 
 } 
});

2. Controller method:

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=deleteCatalogSchemes") 
 @ResponseBody 
 public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") Listf7e83be87db5cd2d9a8a0b8117b38cd4 idList,Boolean isBatch) { 
   … 
 } 
}

Receive List< ;User>, User[] collection parameters:

1, User entity class:

public class User { 
  private String name; 
 private String pwd; 
 //省略getter/setter 
}

2, Page js code:

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 : &#39;application/json;charset=utf-8&#39;, //设置请求头信息 
 success: function(data){ 
  … 
 }, 
 error: function(res){ 
  … 
 } 
});

3, Controller method:

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=saveUsers") 
 @ResponseBody 
 public AjaxJson saveUsers(@RequestBody List<User> userList) { 
  … 
 } 
}

If you want to receive the User[] array, you only need to change the parameter type of saveUsers to @RequestBody User[] userArray.

Receive Lista06c5c2dfccfdea21bf0b155d9343663> collection parameters:

1. Page js code (User object is not needed):

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 : &#39;application/json;charset=utf-8&#39;, //设置请求头信息 
 success: function(data){ 
  … 
 }, 
 error: function(res){ 
  … 
 } 
});

2. Controller method :

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=saveUsers") 
 @ResponseBody 
 public AjaxJson saveUsers(@RequestBody Lista06c5c2dfccfdea21bf0b155d9343663> listMap) { 
  … 
 } 
}

Receive User (bean contains List) collection parameters:

1. User entity class:

public class User { 
 private String name; 
 private String pwd; 
 private List<User> customers;//属于用户的客户群 
 //省略getter/setter 
}

2. Page js code:

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 : &#39;application/json;charset=utf-8&#39;, //设置请求头信息 
 success: function(data){ 
  … 
 }, 
 error: function(res){ 
  … 
 } 
});

3. Controller method:

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=saveUsers") 
 @ResponseBody 
 public AjaxJson saveUsers(@RequestBody User user) { 
  List<User> customers = user.getCustomers(); 
  … 
 } 
}

Related recommendations:

Detailed explanation of the ASP.NET method of obtaining controller, URL and action in MVC

The above is the detailed content of js implements passing complex json parameters to controller. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn