SPRINGMVC Pass the parameter list object through Ajax or pass an array object to the background environment:
- Foreground pass parameters to background
- The front desk uses Ajax
- Backstage Use SPRINGMVC
- The passed parameter is n multiple objects
JSON objects and JSON strings
In the SpringMVC environment, @RequestBody receives a string of Json objects instead of a Json object.
However, in ajax requests, Json objects are often passed, and JSON.stringify(data) can be used to convert objects into strings.
At the same time, the ajax request should also specify the dataType: "json", contentType: "application/json" This can easily transfer an object or List to the Java side!
Content-type
Why does the server do special processing for form submission and file uploading, because the form submission data is a name-value pair, and the Content-Type is application/x-www-form-urlencoded,
The file upload server needs special processing. The normal post request (Content-Type is not application/x-www-form-urlencoded) is not fixed. The format is not necessarily the name-value pair.
Therefore, the server cannot know the specific processing method, so it can only be parsed by obtaining the original data stream. When jquery executes a post request, it sets the Content-Type to application/x-www-form-urlencoded, so the server can parse correctly, and when using the native ajax request,
If the Content-Type is not displayed, the default is text/plain. At this time, the server does not know how to parse the data, so the request data can only be parsed by obtaining the original data stream.
The first way to pass the parameter bean
Jsp file:
$("#saveuddd").click(function(){
Var saveDataAry=[];
Var data1={"name":"test","password":"gz"};
Var data2={"name":"ququ","password":"gr"};
saveDataAry.push(data1);
saveDataAry.push(data2);
$.ajax({
Type: "POST",
Url: basePath + "/user/saveUser",
dataType: "json",
contentType: "application/json", // Specifying this protocol is important
Data:JSON.stringify(saveDataAry), //This parameter is only available in json format, the background is parsed as an entity, and the background can be used directly.
Success:function(data){
}
});
})
Java file
@RequestMapping(value = "saveUser", method = {RequestMethod.POST })
@ResponseBody
Public void saveu( @RequestBody List<User> users ){ //The user at this time is the map data, and it is not parsed as a bean // not recommended
System.out.println(users) ;
}
Use the following methods to use the bean
@RequestMapping(value = "saveUser", method = {RequestMethod.POST })
@ResponseBody
Public void saveu( @RequestBody User[] users ){
System.out.println(users.getName()) ; // The user at this time is the entity bean
}
The second way to pass the key-value pair
Jsp as above
another kind:
@RequestMapping(value = "saveUser", method = {RequestMethod.POST })
@ResponseBody
Public void saveu( @RequestBody List<Map<String,String>> users ){ // spring MVC can only parse the outer json format, the internal bean is converted to a map-type key-value pair, and the map needs to be parsed.
List<User> userList = new ArrayList<User>();
For(Map<String,String> map : users){
User u = new User();
u.setName(map.get("name"));
u.setPassword(map.get("password"));
userList.add(u);
}
// Here you can use userList
}
The Third Way
Ability to pass multiple arguments, but each parameter is a string (the bean-type data is converted to a JSON-formatted string)
Jsp file
$("#saveuddd").click(function(){
Var saveDataAry=[];
Var data1={"name":"test","password":"gz"};
Var data2={"name":"ququ","password":"gr"};
saveDataAry.push(data1);
saveDataAry.push(data2);
$.ajax({
Type: "POST",
Url: basePath + "/user/saveUser",
dataType: "json",
Data:{users:JSON.stringify(saveDataAry),aa:"ddd"},
// can pass multiple parameters, but the user content at this time is passed along with the content of another parameter: aa, so the background is parsed into a string
// The contents of the users received in the background: [{"name":"test","password":"gz"},{"name":"ququ","password":"gr"}]
// aa content received in the background: "ddd"
Success:function(data){
}
});
})
Java file
@RequestMapping(value = "saveUser", method = {RequestMethod.POST })
@ResponseBody
Public void saveu( String users , String aa ){
System.out.println(users) ;
}
Reprinted from 73361104
Springmvc Pass the parameter list object through Ajax or pass an array object to the background