显示目录

SpringMVC 数据绑定

SpringMVC 中提供了多种数据绑定,可以把请求中的数据绑定为简单类型,简单数组,对象,对象的数组等。

简单数组

1
2
3
4
5
@RequestMapping("/array")
@ResponseBody
public String[] array(@RequestParam("name") String[] names) {
return names;
}
1
2
3
4
5
@RequestMapping("/list")
@ResponseBody
public List<String> list(@RequestParam("name") List<String> names) {
return names;
}

显示:

[“Tom”,”Lucy”]

简单对象

1
2
3
4
5
@RequestMapping("/object")
@ResponseBody
public User object(User user) {
return user;
}

显示:

1
2
3
4
5
{
"username": "Tom",
"age": 10,
"address": null
}

复杂对象

1
2
3
4
5
@RequestMapping("/nested-object")
@ResponseBody
public User nestedObject(User user) {
return user;
}

显示:

1
2
3
4
5
6
7
8
{
"username": "Tom",
"age": 10,
"address": {
"city": "Beijing",
"street": "SCI"
}
}

同属性多对象

如果 URL 都使用 username 和 age 的话,会同时作用于 user 和 admin,使用 WebDataBinder 可对其加前缀,就能在映射的时候区分开是给谁使用的。

user.username and admin.username 分别映射到对象 user 和 admin,age 同时映射到他们的 age 属性上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RequestMapping("/intersect-object")
@ResponseBody
public Map intersectObject(User user, Admin admin) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", user);
map.put("admin", admin);
return map;
}
@InitBinder("user")
public void initUser(WebDataBinder binder) {
binder.setFieldDefaultPrefix("user.");
}
@InitBinder("admin")
public void initAdmin(WebDataBinder binder) {
binder.setFieldDefaultPrefix("admin.");
}

显示:

1
2
3
4
5
6
7
8
9
10
11
{
"admin": {
"username": "Jim",
"age": 10
},
"user": {
"username": "Tom",
"age": 10,
"address": null
}
}

对象数组

1
2
3
4
5
@RequestMapping("/object-list")
@ResponseBody
public UserList objectList(UserList userList) {
return userList;
}

显示:

1
2
3
4
5
6
7
8
9
10
11
{
"users": [{
"username": "Tom",
"age": 0,
"address": null
}, {
"username": "Lucy",
"age": 0,
"address": null
}]
}

对象的 Map

1
2
3
4
5
@RequestMapping("/object-map")
@ResponseBody
public UserMap objectMap(UserMap users) {
return users;
}

显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"users": {
"x": {
"username": "Tom",
"age": 0,
"address": null
},
"y": {
"username": "Lucy",
"age": 0,
"address": null
}
}
}

AJAX 数据绑定

如果是 AJAX 传递数据给服务器端,然后要绑定为对象,那么 AJAX 不能是 GET 操作,如果只是使用 AJAX 从服务器端获取数据,则可以是 GET,POST 等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var data = {username: 'Biao', age: 111};
$.ajax({
url: '/json-bind',
type: 'POST', // 1. 不能是 GET
dataType: 'json',
contentType: 'application/json', // 2. 少了就会报错
data: JSON.stringify(data) // 3. data 需要序列化一下
})
.done(function(result) {
console.log(result);
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
});
</script>
</head>
<body>
</body>
</html>

接受 AJAX 传递过来的数据,映射为对象时必须要有 @RequestBody 修饰方法的参数

1
2
3
4
5
6
@RequestMapping("/json-bind")
@ResponseBody
public User json(@RequestBody User user) {
System.out.println(user.getUsername());
return user;
}

浏览器控制台输出

1
2
3
4
5
{
username: "Biao",
age: 111,
address: null
}

需要的类

User

1
2
3
4
5
6
7
public class User {
private String username;
private int age;
private Address address;
// Setters and getters
}

Address

1
2
3
4
5
6
public class Address {
private String city;
private String street;
// Setters and getters
}

Admin

1
2
3
4
5
6
public class Admin {
private String username;
private int age;
// Setters and getters
}

UserList

1
2
3
4
5
public class UserList {
private List<User> users;
// Setters and getters
}

UserMap

1
2
3
4
5
public class UserMap {
private Map<String, User> users;
// Setters and getters
}