Home > Web Front-end > JS Tutorial > Example of spirngmvc js passing complex json parameters to controller

Example of spirngmvc js passing complex json parameters to controller

亚连
Release: 2018-05-28 15:02:39
Original
1766 people have browsed it

Below I will share with you an example of spirngmvc js passing complex json parameters to the controller. It has a good reference value and I hope it will be helpful to everyone.

When Spring MVC receives collection request parameters, it needs to add @RequestBody before the collection parameters of the Controller method. The enctype (MIME encoding) received by @RequestBody by default is application/json, so it is required when sending a POST request. Set the 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 examples of more complex collection parameters that receive List, List, List>, User[], and User (bean contains List):

Receive List collection parameters:

##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){ 
  … 
 } 
});
Copy after login


2. Controller method:

##

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=deleteCatalogSchemes") 
 @ResponseBody 
 public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") List<String> idList,Boolean isBatch) { 
   … 
 } 
}
Copy after login


##Receive List, User[] collection parameters:

1, User entity class:

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



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){ 
  … 
 } 
});
Copy after login
Copy after login



3. Controller method:

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


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

Receive List> collection parameters:

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

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){ 
  … 
 } 
});
Copy after login
Copy after login


##2. Controller method:

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=saveUsers") 
 @ResponseBody 
 public AjaxJson saveUsers(@RequestBody List<Map<String,Object>> listMap) { 
  … 
 } 
}
Copy after login


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 
}
Copy after login


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){ 
  … 
 } 
});
Copy after login


##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(); 
  … 
 } 
}
Copy after login
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. Related articles:


How to create an ajax object and be compatible with multiple browsers

Ajax requests nested within Ajax requests Sample code


Use Ajax technology to complete the homepage login function through the XMLHttpRequest object


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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template