Spring MVC-数据绑定
1. 基本类型绑定
服务器代码
@RequestMapping(
"/mvc")
public class TestController{
@RequestMapping(value=
"baseType.do")
@ResponseBody
public String
baseType(
int age){
return "age = " + age;
}
}
浏览器访问
http://localhost:8080/web/mvc?age=
12
如果没有传递参数age,则服务器报错:
status:
500
org.springframework.web.util.NestedServletException: Request processing failed; nested exception
is java.lang.IllegalStateException:
Optional
int parameter
'age' is present but cannot be translated
into a
null
解决办法
改age类型为包装类型Integer 或者 加上 @RequestParam(“xage”) 表示必须传递age参数,如果不传该参数则报400错误。xage是age的别名,所以访问的URL:
http://localhost:8080/web/mvc?xage=
12
@RequestMapping(value=
"baseType.do")
@ResponseBody
public String
baseType(@
RequestParam("xage")
int age){
return "age = " + age;
}
2. 数组类型的绑定
Controller代码
@RequestMapping(value =
"/array.do")
@ResponseBody
public String
array(String[] name) {
StringBuilder sb =
new StringBuilder();
for(String str : name){
sb.append(str);
sb.append(
" ");
}
return sb.toString();
}
访问地址
http:/
/localhost:8082/bind/mvc/array.
do?name=
Lucy&name=
Tom&name=
Dog
ajax请求访问
$.ajax({
"url":
"mvc/array.do",
"type":
"get",
"data":{
"name":[
"Lucy",
"Tom",
"Dog"
]
},
"contentType":
"x-www-form-urlencoded",
"dataType":
"json"
});
3. 简单对象的数据绑定
定义User类
public class User {
private String name;
private int age;
@Override
public String
toString() {
return "User [name=" + name +
", age=" + age +
"]";
}
}
TestController
public class TestController{
@RequestMapping(value =
"/object.do")
@ResponseBody
public String
object(User user) {
return user.toString();
}
}
访问链接
http:/
/localhost:8082/bind/mvc/object.
do?name=
Lucy&age=
39
打印结果
User [name=
Lucy, age=39]
4. 多层级对象的数据绑定
定义对象
public class ContactInfo {
private String address;
private String phone;
@Override
public String
toString() {
return "ContactInfo [address=" + address +
", phone=" + phone +
"]";
}
}
public class User {
private String name;
private int age;
private ContactInfo contactInfo;
@Override
public String
toString() {
return "User [name=" + name +
", age=" + age +
", contactInfo="
+ contactInfo +
"]";
}
}
TestController
public class TestController {
@RequestMapping(value =
"/object.do")
@ResponseBody
public String
object(User user) {
return user.toString();
}
}
访问链接
http://localhost:
8082/bind/mvc/object
.do?name=Lucy&age=
39&contactInfo
.address=BeiJing&contactInfo
.phone=
10086
打印结果
User [name=
Lucy, age=39, contactInfo=ContactInfo [address=BeiJing, phone=10086]]
5. 拥有同属性的多对象的数据绑定
定义类
public class User {
private String name;
private int age;
private ContactInfo contactInfo;
getter method and setter method...
@Override
public String
toString() {
return "User [name=" + name +
", age=" + age +
", contactInfo="
+ contactInfo +
"]";
}
}
public class Admin {
private String name;
private int age;
@Override
public String
toString() {
return "Admin [name=" + name +
", age=" + age +
"]";
}
}
TestController
@RequestMapping(value =
"/mutiObject.do")
@ResponseBody
public String
MutiObject(User user,Admin admin) {
return user.toString() +
" " + admin.toString();
}
访问链接
http://localhost:
8082/bind/mvc/mutiObject
.do?name=Lucy&age=
39&contactInfo
.address=BeiJing&contactInfo
.phone=
10086
打印结果
User [name=Lucy, age=39, contactInfo=ContactInfo [address=BeiJing, phone=10086]]
Admin [name=Lucy, age=39]
发现Admin和User的name,age都是相同的
尝试修改访问链接
http://localhost:
8082/bind/mvc/mutiObject
.do?user
.name=Lucy&admin
.name=Tom&age=
39
打印结果
User [
name=null, age=
39, contactInfo=null] Admin [
name=null, age=
39]
我们发现user的
name和admin的
name均未被赋值
解决办法
修改 TestController,添加下面两个方法
@InitBinder(
"user")
public void initUser(WebDataBinder binder) {
binder.setFieldDefaultPrefix(
"user.");
}
@InitBinder(
"admin")
public void initAdmin(WebDataBinder binder) {
binder.setFieldDefaultPrefix(
"admin.");
}
访问链接
http://localhost:
8082/bind/mvc/mutiObject
.do?user
.name=Lucy&admin
.name=Tom&age=
39
打印结果
User [name=Lucy, age=39, contactInfo=null] Admin [name=Tom, age=39]
6. List的数据绑定
定义类
public class User{
private String name;
private Integer age;
private String contactInfo;
}
public class ListUserForm {
private List<User> users;
@Override
public String
toString() {
return "ListUserForm [users=" + users +
"]";
}
}
TestController
@RequestMapping(value =
"/list.do")
@ResponseBody
public String
list(ListUserForm listUserForm) {
return "list.size = " + listUserForm.getUsers().size() +
" " +listUserForm.toString();
}
访问链接
http://localhost:8082/bind/mvc/list.do?users[
0].name=Lucy&users[1].name=Tom&users[10].name=Jim
打印结果
list.size =
11 ListUserForm [users=[User [name=Lucy, age=
0, contactInfo=
null], User [name=Tom, age=
0, contactInfo
=
null], User [name=
null, age=
0, contactInfo=
null], User [name=
null, age=
0, contactInfo=
null], User [name=
null, ag
e=
0, contactInfo=
null], User [name=
null, age=
0, contactInfo=
null], User [name=
null, age=
0, contactInfo=
null], User
[name=
null, age=
0, contactInfo=
null], User [name=
null, age=
0, contactInfo=
null], User [name=
null, age=
0, contactInfo
=
null], User [name=Jim, age=
0, contactInfo=
null]]]
结论
不能直接在TestController中参数传递List users,必须封装成ListUserForm这样的对象才能传递要注意List中的下标要连续,否则容易造成性能损耗
7. Set的数据绑定
Set用得不那么多,而且容易出错Set的数据绑定与List类似UserSetForm中,需要事先对users进行实例化
8. Map的数据绑定
Java代码
public class User{
private String name;
}
@Controller
@RequestMapping
public class TestController{
@RequestMapping(
"map.do")
public String
map(Map<String,User> map){
return map.toString();
}
}
测试链接
http://localhost:8080/map.do?users[
'X'].name=Tom&users[
'Y'].name=Jim
9. Json的数据绑定
User类
public class User{
private Integer id;
private String name;
private Integer age;
private List<ContactInfo> contactInfos;
}
public class ContactInfo{
private String name;
private String phoneNumber;
}
Controller代码
@Controller
@RequestMapping(
"/test")
public class TestController
@RequestMapping(value="json.do")
@ResponseBody
public String
json(@RequestBody User user){
return user.toString();
}
}
请求地址
http:/
/localhost:8080/web/test/json.
do
JSON 数据
{
"id":
"1",
"name":
"iuie",
"age":
22,
"contactInfos":[
{
"name":
"zhang",
"phoneNumber":
12345656"
}
]
}