目录:
- 概述
- 实现方法及测试
[一]、概述
form表单中对应了多个对象而且不同对象间的属性名有相同的情况下,如何才能把页面数据准确绑定到各个对象中去呢?比如: form表单中对应了 Course (课程介绍,属性包含:name、description)和 Teacher (该课程的教师信息,属性包含:name、email、grade),两个对象中有同名的属性:name
[二]、实现方法及测试
如果多个对象之间没有同名的属性,springMVC可以实现对象的转化,如果是struts2 可以运用前缀的方式来解决,类似course.name,teacher.name,那么在这里我们可以从struts2中得到一定的启发,创建一个父级的对象CourseInfo.java 它包含了 Course 、Teacher 这两个对象:
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 |
package com.micmiu.demo.web.v2.demo.vo; import org.apache.commons.lang3.builder.ToStringBuilder; import com.micmiu.demo.web.v2.demo.entity.Course; 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; 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; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } } |
Course.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 |
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_COURSE") public class Course extends IdEntity { @Column(name = "NAME") private String name; @Column(name = "DESCRIPTION") private String description; public String getName() { return name; } public String getDescription() { return description; } public void setName(String name) { this.name = name; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } } |
Teacher.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_TEACHER") public class Teacher extends IdEntity { @Column(name = "NAME") private String name; @Column(name = "EMAIL") private String email; @Column(name = "GRADE") private String grade; public String getName() { return name; } public String getEmail() { return email; } public String getGrade() { return grade; } public void setName(String name) { this.name = name; } public void setEmail(String email) { this.email = email; } public void setGrade(String grade) { this.grade = grade; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } } |
view页面中 form表单内容如下:
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 |
<form:form id="input-form" modelAttribute="courseInfo" action="${ctx}/demo/course.do?method=save" method="post"> <input type="hidden" name="id" value="${course.id}" /> <fieldset class="prepend-top"> <legend>课程信息</legend> <div id="messageBox" class="error-msg" style="display: none">输入有误,请先更正。</div> <div> <label for="course.name" class="field">课程名称:</label> <input type="text" id="course.name" name="course.name" size="20" value="${courseInfo.course.name}" class="required" /> </div> <div> <label for="course.description" class="field">课程介绍:</label> <input type="text" id="course.description" name="course.description" size="20" value="${courseInfo.course.description}" class="required" /> </div> <div> <label for="teacher.name" class="field">老师姓名:</label> <input type="text" id="teacher.name" name="teacher.name" size="20" value="${courseInfo.teacher.name}" class="required" /> </div> <div> <label for="teacher.email" class="field">老师Email:</label> <input type="text" id="teacher.email" name="teacher.email" size="20" value="${courseInfo.teacher.email}" class="required" /> </div> </fieldset> </form:form> |
注意:加前缀来区分
controller 的实现CourseAction.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 |
package com.micmiu.demo.web.v2.demo.action; import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; 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; import com.micmiu.demo.web.v2.demo.vo.CourseInfo; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ @Controller @RequestMapping(value = "/demo/course.do") public class CourseAction { @RequestMapping(params = { "method=list" }) public String list(Model model) { return "course.list"; } @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()); String message = "save"; redirectAttributes.addFlashAttribute("message", message); return message; } } |
controller中的部分方法省略,这里主要测试下页面数据绑定的结果,测试过程如下:
表单输入内容如下图:
提交后,控制台输出日志如下:
1 2 |
course >> com.micmiu.demo.web.v2.demo.entity.Course@1fa1838[name=课程名称,description=课程介绍,id=<null>] teacher >> com.micmiu.demo.web.v2.demo.entity.Teacher@4c2c86[name=老师姓名,email=老师Email,grade=<null>,id=<null>] |
从日志中可以看出:创建父级对象后,利用前缀区分的方式可以实现多个对象的数据准确绑定。
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/j2ee/spring/springmvc-view-mulit-bean/
在VO直接访问PO真的好?
这个嘛。。。不讨论vo po bo ,适用最好 你说是吗
这种方法不灵活,更好的方式的实现HandlerMethodArgumentResolver接口。
http://www.cnblogs.com/fangjian0423/p/springMVC-request-param-analysis.html#demo
创建父级对象 貌似不是最好的办法, 感觉也很麻烦, 如果有N个累死的情况呢, 难道都要创建父级对象啊?
这个只是一种既要用spring mvc 又不想扩展源码的折中解决办法
😯
请问,就这样就ok了嘛?
不需要额外配置什么处理器啥的么。我奇怪的是他们怎么封装成对象的啊。。。
我是在3.2上实现的 好像2.x 不支持