springMVC中复杂嵌套对象、List等集合类型数据绑定
2014-11-15 10:38
232 查看
继前面一篇springMVC页面中多个对象的数据绑定,本文主要介绍如果实现复杂类型对象的数据绑定,比如前文中的父级对象CourseInfo中增加:String[]times,List<Student>
studentList这两个复杂类型属性,页面中数据如何才能准确绑定到对象上呢?
[二]、实现方法及测试
CourseInfo.java修改成如下:
查看源代码
打印帮助
Student.java:
查看源代码
打印帮助
view层页面修改成如下:
查看源代码
打印帮助
注意:复杂对象在页面元素中各属性的定义规则。
controller对应的CourseAction.java实现修改成如下:
查看源代码
打印帮助
controller中的部分方法省略,这里主要测试下页面数据绑定的结果,测试过程如下:
表单输入内容如下图:
提交后,控制台输出日志如下:
从日志中可以看出:不管是string数组对象times还是复杂列表对象studentList,数据绑定准确无误。
springMVC中复杂嵌套对象、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<ahref=" |
15 | */ |
16 | public class CourseInfo{ |
17 |
18 | //课程介绍 |
19 | private Coursecourse; |
20 |
21 | //老师信息 |
22 | private Teacherteacher; |
23 |
24 | //上课时间 |
25 | private String[]times; |
26 |
27 | //学生列表 |
28 | private List<Student>studentList; |
29 |
30 | public CoursegetCourse(){ |
31 | return course; |
32 | } |
33 |
34 | public TeachergetTeacher(){ |
35 | return teacher; |
36 | } |
37 |
38 | public void setCourse(Coursecourse){ |
39 | this .course=course; |
40 | } |
41 |
42 | public void setTeacher(Teacherteacher){ |
43 | this .teacher=teacher; |
44 | } |
45 |
46 | public String[]getTimes(){ |
47 | return times; |
48 | } |
49 |
50 | public List<Student>getStudentList(){ |
51 | return studentList; |
52 | } |
53 |
54 | public void setTimes(String[]times){ |
55 | this .times=times; |
56 | } |
57 |
58 | public void setStudentList(List<Student>studentList){ |
59 | this .studentList=studentList; |
60 | } |
61 |
62 | @Override |
63 | public StringtoString(){ |
64 | return ToStringBuilder.reflectionToString( this ); |
65 | } |
66 | } |
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<ahref=" |
15 | */ |
16 | @Entity |
17 | @Table (name= "T_DEMO_STUDENT" ) |
18 | public class Student extends IdEntity{ |
19 |
20 | @Column (name= "NAME" ) |
21 | private Stringname; |
22 |
23 | @Column (name= "EMAIL" ) |
24 | private Stringemail; |
25 |
26 | @Column (name= "CLASS_NAME" ) |
27 | private StringclassName; |
28 |
29 | public StringgetName(){ |
30 | return name; |
31 | } |
32 |
33 | public StringgetEmail(){ |
34 | return email; |
35 | } |
36 |
37 | public StringgetClassName(){ |
38 | return className; |
39 | } |
40 |
41 | public void setName(Stringname){ |
42 | this .name=name; |
43 | } |
44 |
45 | public void setEmail(Stringemail){ |
46 | this .email=email; |
47 | } |
48 |
49 | public void setClassName(StringclassName){ |
50 | this .className=className; |
51 | } |
52 |
53 | @Override |
54 | public StringtoString(){ |
55 | return ToStringBuilder.reflectionToString( this ); |
56 | } |
57 | } |
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
|
4 | RedirectAttributesredirectAttributes){ |
5 | System.out.println( "course>>" +courseInfo.getCourse()); |
6 | System.out.println( "teacher>>" +courseInfo.getTeacher()); |
7 |
8 | for (Stringtime:courseInfo.getTimes()){ |
9 | System.out.println( "time>>" +time); |
10 | } |
11 | for (Studentstudent:courseInfo.getStudentList()){ |
12 | System.out.println( "student>>" +student); |
13 | } |
14 |
15 | System.out.println( "courseInfo>>" +courseInfo); |
16 | Stringmessage= "save" ; |
17 | redirectAttributes.addFlashAttribute( "message" ,message); |
18 | return message; |
19 | } |
表单输入内容如下图:
提交后,控制台输出日志如下:
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,数据绑定准确无误。
相关文章推荐
- springMVC中复杂嵌套对象、List等集合类型数据绑定
- springMVC4复杂对象和集合类型入参绑定
- SpringMVC数据绑定二(自定义数据类型与复杂数据类型对象)
- springMVC对简单对象,Set,List,Map的数据绑定及类型转换
- list集合对于基本数据类型与引用数据类型(对象)去重操作
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- springmvc中的对象、数组、集合类型的参数绑定
- springMVC对简单对象、Set、List、Map的数据绑定和常见问题.
- springMVC对简单对象、Set、List、Map的数据绑定和常见问题(一)
- SpringMVC数据绑定1 之 基本对象、数组、包装类与集合的绑定
- iOS Json和对象互转(反射支持对象嵌套的复杂对象类型,支持基本数据类型和NSDate属性转化)
- springMVC对简单对象、Set、List、Map的数据绑定和常见问题(一)
- springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解
- JAVA之旅(十八)——基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用
- DevExpress之TreeList绑定复杂数据类型
- springMVC对简单对象、Set、List、Map的数据绑定和常见问题(二)
- springMVC对简单对象、Set、List、Map的数据绑定和常见问题(二)
- springMVC对简单对象、Set、List、Map的数据绑定和常见问题
- JAVA之旅(十八)——基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用
- springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解