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