SpringMVC在对应绑定不同实体,但具有相同属性名的解决方案....

时间:2024-03-25 10:07:38

在springmvc中,可以对前台传递过来的参数进行与后台实体绑定(第二种方式相对较好).

比如:

前台页面:

 <form action="${pageContext.request.contextPath}/test/test" method="POST">
用户名:<input type="text" name="name"><br/>
<input type="submit" value="提交">
</form>

实体类:

 package com.yemaozi.rms.domain;

 public class Student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

对应的Controller:

 @Controller
@Scope(value="prototype")
@RequestMapping("/test")
public class TestController {
@RequestMapping("/test")
public String test(Student stu){
System.out.println(stu.getName());
return "success";
}
}

这样,在Controller是可以进行绑定的....

但是,若是,要对多个实体数据进行绑定,而且这些实体有同名的属性....

前台页面:

<form action="${pageContext.request.contextPath}/test/test" method="POST">
学生姓名:<input type="text" name="name"><br/>
老师姓名:<input type="text" name="name"><br/>
<input type="submit" value="提交">
</form>

实体类:

 public class Teacher {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Controller:

 @RequestMapping("/test")
public String test(Student stu, Teacher teacher){
System.out.println(stu.getName() + teacher.getName());
return "success";
5 }

这样,就会明白,name并不是唯一标识了,所以,在后台不能精确的绑定,其实,若是将该表单进行提交,则会将这两个name属性分别都添加到stu 和teacher这两个对象中..

因为springmvc中,是根据属性来进行数据绑定的,不像struts2是基于ognl的数据绑定机制.

要解决现在这样问题的方案一:

复合实体:  

即:

 public class StuTeacher {
private Student stu;
private Teacher teacher;
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}

创建一个拥有stu和teacher这两个实体对象的类StuTeacher.....

这样我们就可以再前台这样书写.

 <form action="${pageContext.request.contextPath}/test/test1" method="POST">
学生姓名:<input type="text" name="stu.name"><br/>
老师姓名:<input type="text" name="teacher.name"><br/>
<input type="submit" value="提交">
</form>

就可以根据复合实体中的属性通过.进行导航绑定数据

在Controller中的代码:

 @RequestMapping("/test1")
public String test1(StuTeacher stuTeacher){
System.out.println(stuTeacher);
return "success";
}

这种方法可以简单的处理这种数据绑定问题,好处是不需要添加任何插件代码,缺点是 扩展性不好,后期可能使得代码臃肿.

所以可以在springmvc中可以进行自定义ModelAttributeProcessor来进行数据绑定的扩展.

1,自定义注解:

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExtModelAttribute {
String value() default "";
}

2,继承ServletModelAttributeMethodProcessor类,实现自己的数据绑定模式.

 import javax.servlet.ServletRequest;

 import org.springframework.core.MethodParameter;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor; public class ExtServletModelAttributeMethodProcessor extends
ServletModelAttributeMethodProcessor { public ExtServletModelAttributeMethodProcessor() {
super(false);
} public ExtServletModelAttributeMethodProcessor(boolean annotationNotRequired) {
super(annotationNotRequired);
} @Override
public boolean supportsParameter(MethodParameter parameter) {
if (parameter.hasParameterAnnotation(ExtModelAttribute.class)) {
return true;
} else {
return false;
}
} @Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
servletBinder.setFieldDefaultPrefix(servletBinder.getObjectName() + ".");
servletBinder.bind(servletRequest);
}
}

3,在springmvc配置文件中添加相应的加载驱动配置

 <mvc:annotation-driven>
<!--添加在此处-->
<mvc:argument-resolvers>
<bean class="com.yemaozi.springmvc.ext.databind.ExtServletModelAttributeMethodProcessor"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>

4,应用

在前台页面中:

<form action="${pageContext.request.contextPath}/test/test1" method="POST">
学生姓名:<input type="text" name="stu.name"><br/>
老师姓名:<input type="text" name="teacher.name"><br/>
<input type="submit" value="提交">
</form>

在Controller中使用方式:

 @RequestMapping("/test2")
public String test2(@ExtModelAttribute("stu") Student stu, @ExtModelAttribute("teacher")Teacher teacher){
System.out.println(stu.getName() + teacher.getName());
return "success";
}

使用刚才自定义的注解来标注对应的属性.