springMVC中复杂嵌套对象、List等集合类型数据绑定



springMVC中复杂嵌套对象、List等集合类型数据绑定

[一]、概述

继前面一篇 springMVC 页面中多个对象的数据绑定 ,本文主要介绍如果实现复杂类型对象的数据绑定,比如前文中的父级对象CourseInfo 中增加:String[] times , List studentList 这两个复杂类型属性,页面中数据如何才能准确绑定到对象上呢?

[二]、实现方法及测试

CourseInfo.java 修改成如下:

查看源代码
打印 帮助
1 package com.micmiu.demo.web.v2.demo.vo;
2  
3 import java.util.List;
4  
5 import org.apache.commons.lang3.builder.ToStringBuilder;
6  
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;
10  
11 /**
12  * 课程信息(view层)
13  *
14  * @author Michael Sun
15  */
16 public class CourseInfo {
17  
18     // 课程介绍
19     private Course course;
20  
21     // 老师信息
22     private Teacher teacher;
23  
24     // 上课时间
25     private String[] times;
26  
27     // 学生列表
28     private List studentList;
29  
30     public Course getCourse() {
31         return course;
32     }
33  
34     public Teacher getTeacher() {
35         return teacher;
36     }
37  
38     public void setCourse(Course course) {
39         this.course = course;
40     }
41  
42     public void setTeacher(Teacher teacher) {
43         this.teacher = teacher;
44     }
45  
46     public String[] getTimes() {
47         return times;
48     }
49  
50     public List getStudentList() {
51         return studentList;
52     }
53  
54     public void setTimes(String[] times) {
55         this.times = times;
56     }
57  
58     public void setStudentList(List studentList) {
59         this.studentList = studentList;
60     }
61  
62     @Override
63     public String toString() {
64         return ToStringBuilder.reflectionToString(this);
65     }
66 }

Student.java :

查看源代码
打印 帮助
1 package com.micmiu.demo.web.v2.demo.entity;
2  
3 import javax.persistence.Column;
4 import javax.persistence.Entity;
5 import javax.persistence.Table;
6  
7 import org.apache.commons.lang3.builder.ToStringBuilder;
8  
9 import com.micmiu.demo.web.v2.base.entity.IdEntity;
10  
11 /**
12  * 学生信息
13  *
14  * @author Michael Sun
15  */
16 @Entity
17 @Table(name = "T_DEMO_STUDENT")
18 public class Student extends IdEntity {
19  
20     @Column(name = "NAME")
21     private String name;
22  
23     @Column(name = "EMAIL")
24     private String email;
25  
26     @Column(name = "CLASS_NAME")
27     private String className;
28  
29     public String getName() {
30         return name;
31     }
32  
33     public String getEmail() {
34         return email;
35     }
36  
37     public String getClassName() {
38         return className;
39     }
40  
41     public void setName(String name) {
42         this.name = name;
43     }
44  
45     public void setEmail(String email) {
46         this.email = email;
47     }
48  
49     public void setClassName(String className) {
50         this.className = className;
51     }
52  
53     @Override
54     public String toString() {
55         return ToStringBuilder.reflectionToString(this);
56     }
57 }

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">
5         <legend>课程信息legend>
6         <div id="messageBox" class="error-msg" style="display: none">输入有误,请先更正。div>
7         <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" />
11         div>
12         <div>
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" />
16         div>
17  
18         <div>
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" />
22         div>
23         <div>
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" />
27         div>
28         <div>
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" />
34         div>
35  
36         <div>
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" />
40         div>
41         <div>
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" />
45         div>
46             <div>
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" />
50         div>
51         <div>
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" />
55         div>
56     fieldset>

注意:复杂对象在页面元素中各属性的定义规则

controller 对应的 CourseAction.java  实现修改成如下:

查看源代码
打印 帮助
1 @RequestMapping(params = { "method=save" })
2 @ResponseBody
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());
7  
8     for (String time : courseInfo.getTimes()) {
9         System.out.println("time >> " + time);
10     }
11     for (Student student : courseInfo.getStudentList()) {
12         System.out.println("student >> " + student);
13     }
14  
15     System.out.println("courseInfo >> " + courseInfo);
16     String message = "save";
17     redirectAttributes.addFlashAttribute("message", message);
18     return message;
19 }

controller中的部分方法省略,这里主要测试下页面数据绑定的结果,测试过程如下:

表单输入内容如下图:

springMVC中复杂嵌套对象、List等集合类型数据绑定_第1张图片

提交后,控制台输出日志如下:

course >> com.micmiu.demo.web.v2.demo.entity.Course@e11326[name=课程名称,description=课程介绍,id=]
teacher >> com.micmiu.demo.web.v2.demo.entity.Teacher@b03bd5[name=老师姓名,email=老师Email,grade=,id=]
time >> 周一
time >> 周三
time >> 周五
student >> com.micmiu.demo.web.v2.demo.entity.Student@13cd6aa[name=学生1姓名,email=学生1Email,className=,id=]
student >> com.micmiu.demo.web.v2.demo.entity.Student@13137a2[name=学生2姓名,email=学生2Email,className=,id=]
courseInfo >> com.micmiu.demo.web.v2.demo.vo.CourseInfo@ce4be6[course=com.micmiu.demo.web.v2.demo.entity.Course@e11326[name=课程名称,description=课程介绍,id=],teacher=com.micmiu.demo.web.v2.demo.entity.Teacher@b03bd5[name=老师姓名,email=老师Email,grade=,id=],times={周一,周三,周五},studentList=[com.micmiu.demo.web.v2.demo.entity.Student@13cd6aa[name=学生1姓名,email=学生1Email,className=,id=], com.micmiu.demo.web.v2.demo.entity.Student@13137a2[name=学生2姓名,email=学生2Email,className=,id=]]]

从日志中可以看出:不管是string数组对象times 还是复杂列表对象 studentList,数据绑定准确无误。

你可能感兴趣的:(spring,mvc)