目录:
- 概述
- 实现方法及测试
[一]、概述
继前面一篇 springMVC 页面中多个对象的数据绑定 ,本文主要介绍如果实现复杂类型对象的数据绑定,比如前文中的父级对象CourseInfo 中增加:String[] times , List<Student> studentList 这两个复杂类型属性,页面中数据如何才能准确绑定到对象上呢?
[二]、实现方法及测试
CourseInfo.java 修改成如下:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
vo
;
import
java
.
util
.
List
;
import
org
.
apache
.
commons
.
lang3
.
builder
.
ToStringBuilder
;
import
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Course
;
import
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Student
;
import
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Teacher
;
/**
* 课程信息(view层)
*
* @author <a href="http://www.micmiu.com">Michael Sun</a>
*/
public
class
CourseInfo
{
// 课程介绍
private
Course
course
;
// 老师信息
private
Teacher
teacher
;
// 上课时间
private
String
[
]
times
;
// 学生列表
private
List
&
lt
;
Student
&
gt
;
studentList
;
public
Course
getCourse
(
)
{
return
course
;
}
public
Teacher
getTeacher
(
)
{
return
teacher
;
}
public
void
setCourse
(
Course
course
)
{
this
.
course
=
course
;
}
public
void
setTeacher
(
Teacher
teacher
)
{
this
.
teacher
=
teacher
;
}
public
String
[
]
getTimes
(
)
{
return
times
;
}
public
List
&
lt
;
Student
&
gt
;
getStudentList
(
)
{
return
studentList
;
}
public
void
setTimes
(
String
[
]
times
)
{
this
.
times
=
times
;
}
public
void
setStudentList
(
List
&
lt
;
Student
&
gt
;
studentList
)
{
this
.
studentList
=
studentList
;
}
@
Override
public
String
toString
(
)
{
return
ToStringBuilder
.
reflectionToString
(
this
)
;
}
}
|
Student.java :
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
;
import
javax
.
persistence
.
Column
;
import
javax
.
persistence
.
Entity
;
import
javax
.
persistence
.
Table
;
import
org
.
apache
.
commons
.
lang3
.
builder
.
ToStringBuilder
;
import
com
.
micmiu
.
demo
.
web
.
v2
.
base
.
entity
.
IdEntity
;
/**
* 学生信息
*
* @author <a href="http://www.micmiu.com">Michael Sun</a>
*/
@
Entity
@
Table
(
name
=
"T_DEMO_STUDENT"
)
public
class
Student
extends
IdEntity
{
@
Column
(
name
=
"NAME"
)
private
String
name
;
@
Column
(
name
=
"EMAIL"
)
private
String
email
;
@
Column
(
name
=
"CLASS_NAME"
)
private
String
className
;
public
String
getName
(
)
{
return
name
;
}
public
String
getEmail
(
)
{
return
email
;
}
public
String
getClassName
(
)
{
return
className
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
void
setClassName
(
String
className
)
{
this
.
className
=
className
;
}
@
Override
public
String
toString
(
)
{
return
ToStringBuilder
.
reflectionToString
(
this
)
;
}
}
|
view 层页面修改成如下:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
&
lt
;
form
:
form
id
=
"input-form"
modelAttribute
=
"courseInfo"
action
=
"${ctx}/demo/course.do?method=save"
method
=
"post"
&
gt
;
&
lt
;
input
type
=
"hidden"
name
=
"id"
value
=
"${course.id}"
/
&
gt
;
&
lt
;
fieldset
class
=
"prepend-top"
&
gt
;
&
lt
;
legend
&
gt
;课程信息
&
lt
;
/
legend
&
gt
;
&
lt
;
div
id
=
"messageBox"
class
=
"error-msg"
style
=
"display: none"
&
gt
;输入有误,请先更正。
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"course.name"
class
=
"field"
&
gt
;课程名称
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"course.name"
name
=
"course.name"
size
=
"20"
value
=
"${courseInfo.course.name}"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"course.description"
class
=
"field"
&
gt
;课程介绍
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"course.description"
name
=
"course.description"
size
=
"20"
value
=
"${courseInfo.course.description}"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"teacher.name"
class
=
"field"
&
gt
;老师姓名
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"teacher.name"
name
=
"teacher.name"
size
=
"20"
value
=
"${courseInfo.teacher.name}"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"teacher.email"
class
=
"field"
&
gt
;老师
Email
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"teacher.email"
name
=
"teacher.email"
size
=
"20"
value
=
"${courseInfo.teacher.email}"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"times"
class
=
"field"
&
gt
;上课时间
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"times[0]"
name
=
"times"
size
=
"20"
value
=
"周一"
class
=
"required"
/
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"times[1]"
name
=
"times"
size
=
"20"
value
=
"周三"
class
=
"required"
/
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"times[2]"
name
=
"times"
size
=
"20"
value
=
"周五"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"studentList[0].name"
class
=
"field"
&
gt
;学生
1姓名
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"studentList[0].name"
name
=
"studentList[0].name"
size
=
"20"
value
=
"学生1姓名"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"studentList[0].email"
class
=
"field"
&
gt
;学生
1Email
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"studentList[0].email"
name
=
"studentList[0].email"
size
=
"20"
value
=
"学生1Email"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"studentList[1].name"
class
=
"field"
&
gt
;学生
2姓名
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"studentList[1].name"
name
=
"studentList[1].name"
size
=
"20"
value
=
"学生2姓名"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
div
&
gt
;
&
lt
;
label
for
=
"studentList[1].email"
class
=
"field"
&
gt
;学生
2Email
:
&
lt
;
/
label
&
gt
;
&
lt
;
input
type
=
"text"
id
=
"studentList[0].email"
name
=
"studentList[1].email"
size
=
"20"
value
=
"学生2Email"
class
=
"required"
/
&
gt
;
&
lt
;
/
div
&
gt
;
&
lt
;
/
fieldset
&
gt
;
|
注意:复杂对象在页面元素中各属性的定义规则。
controller 对应的 CourseAction.java 实现修改成如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@
RequestMapping
(
params
=
{
"method=save"
}
)
@
ResponseBody
public
String
save
(
Model
model
,
ModelAndView
mv
,
CourseInfo
courseInfo
,
RedirectAttributes
redirectAttributes
)
{
System
.
out
.
println
(
"course >> "
+
courseInfo
.
getCourse
(
)
)
;
System
.
out
.
println
(
"teacher >> "
+
courseInfo
.
getTeacher
(
)
)
;
for
(
String
time
:
courseInfo
.
getTimes
(
)
)
{
System
.
out
.
println
(
"time >> "
+
time
)
;
}
for
(
Student
student
:
courseInfo
.
getStudentList
(
)
)
{
System
.
out
.
println
(
"student >> "
+
student
)
;
}
System
.
out
.
println
(
"courseInfo >> "
+
courseInfo
)
;
String
message
=
"save"
;
redirectAttributes
.
addFlashAttribute
(
"message"
,
message
)
;
return
message
;
}
|
controller中的部分方法省略,这里主要测试下页面数据绑定的结果,测试过程如下:
表单输入内容如下图:
提交后,控制台输出日志如下:
1
2
3
4
5
6
7
8
|
course
&
gt
;
&
gt
;
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Course
@
e11326
[
name
=课程名称
,
description
=课程介绍
,
id
=&
lt
;
null
&
gt
;
]
teacher
&
gt
;
&
gt
;
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Teacher
@
b03bd5
[
name
=老师姓名
,
email
=老师
Email
,
grade
=&
lt
;
null
&
gt
;
,
id
=&
lt
;
null
&
gt
;
]
time
&
gt
;
&
gt
;
周一
time
&
gt
;
&
gt
;
周三
time
&
gt
;
&
gt
;
周五
student
&
gt
;
&
gt
;
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Student
@
13cd6aa
[
name
=学生
1姓名
,
email
=学生
1Email
,
className
=&
lt
;
null
&
gt
;
,
id
=&
lt
;
null
&
gt
;
]
student
&
gt
;
&
gt
;
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Student
@
13137a2
[
name
=学生
2姓名
,
email
=学生
2Email
,
className
=&
lt
;
null
&
gt
;
,
id
=&
lt
;
null
&
gt
;
]
courseInfo
&
gt
;
&
gt
;
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
vo
.
CourseInfo
@
ce4be6
[
course
=
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Course
@
e11326
[
name
=课程名称
,
description
=课程介绍
,
id
=&
lt
;
null
&
gt
;
]
,
teacher
=
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Teacher
@
b03bd5
[
name
=老师姓名
,
email
=老师
Email
,
grade
=&
lt
;
null
&
gt
;
,
id
=&
lt
;
null
&
gt
;
]
,
times
=
{周一
,周三
,周五
}
,
studentList
=
[
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Student
@
13cd6aa
[
name
=学生
1姓名
,
email
=学生
1Email
,
className
=&
lt
;
null
&
gt
;
,
id
=&
lt
;
null
&
gt
;
]
,
com
.
micmiu
.
demo
.
web
.
v2
.
demo
.
entity
.
Student
@
13137a2
[
name
=学生
2姓名
,
email
=学生
2Email
,
className
=&
lt
;
null
&
gt
;
,
id
=&
lt
;
null
&
gt
;
]
]
]
|
从日志中可以看出:不管是string数组对象times 还是复杂列表对象 studentList,数据绑定准确无误。
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/j2ee/spring/springmvc-view-complex/