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 : 'application/json;charset=utf-8', //设置请求头信息
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.