This time I will show you how to use spirngmvc js to transfer complex json parameters to the controller, and how to use spirngmvc js to transfer complex json parameters to the controller. What are the precautions?The following is a practical case, let’s take a look .
When Spring MVC receives the collection request parameters, you need to add @RequestBody before the collection parameters of the Controller method, and the enctype (MIME encoding) received by @RequestBody by default is application/json. Therefore, when sending a POST request, you need to 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
Receive List
##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: 'json', 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[]") List<String> idList,Boolean isBatch) { … } }
Receive List
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 : 'application/json;charset=utf-8', //设置请求头信息 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) { … } }
Receive List
##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 : 'application/json;charset=utf-8', //设置请求头信息
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 List<Map<String,Object>> listMap) {
…
}
}
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 : 'application/json;charset=utf-8', //设置请求头信息
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();
…
}
}
Recommended reading:
How to use the $http service Post method in AngularJS to pass json parameters
##How to use Npm in vue run build passes different parameters according to the environment
The above is the detailed content of How to use spirngmvc js to pass complex json parameters to the controller. For more information, please follow other related articles on the PHP Chinese website!