SpringMVC支持多对象Form Data绑定:
对应的bean:
package com.netpf.test.bean;
public class Dept { private int id; private String name; private String leader; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLeader() { return leader; } public void setLeader(String leader) { this.leader = leader; } }
public class Role {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
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;
import javax.servlet.ServletRequest;
public class FormModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {
public FormModelAttributeMethodProcessor() {
super(false);
}
public FormModelAttributeMethodProcessor(boolean annotationNotRequired) {
super(annotationNotRequired);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (parameter.hasParameterAnnotation(FormModel.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);
}
}
package com.netpf.test;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FormModel {
String value() default "";
}
package com.netpf.test.Controller;
import com.netpf.common.APIResponse;
import com.netpf.test.FormModel;
import com.netpf.test.bean.Dept;
import com.netpf.test.bean.Role;
import com.netpf.test.bean.User;
import java.util.List;
public class TestController {
public APIResponse test(@FormModel("user")User user, @FormModel("dept")Dept dept, @FormModel("role") List<Role> roles){
return APIResponse.success();
}
}
对应Servlet xml如下:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<mvc:default-servlet-handler/>
<!-- 扫描自定义多实体对象处理类 -->
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.netpf.test.FormModelAttributeMethodProcessor"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.**.controller" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" />
<!-- 处理使用@responseBody注解返回json数据至前台 -->
<!-- start -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="mappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- end -->
<!--基于validate的载入配置-->
<mvc:annotation-driven validator="validator"/>
<!-- <bean id="accountValidator" class="dao.AccountValidator"></bean> -->
<bean id= "validator"
class= "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name= "providerClass" value= "org.hibernate.validator.HibernateValidator"/>
<!-- 假设不加默认到 使用classpath下的 ValidationMessages.properties -->
<property name= "validationMessageSource" ref= "messageSource"/>
</bean>
<bean id= "messageSource"
class= "org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name= "basename" value= "classpath:message"/>
<property name= "fileEncodings" value= "utf-8"/>
<property name= "cacheSeconds" value= "120"/>
</bean>
<!--<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">-->
<!--<property name="basename" value="/WEB-INF/message_info" />-->
<!--<property name="defaultEncoding" value="UTF-8"/>-->
<!--<property name="cacheSeconds" value="60"/>-->
<!--</bean>-->
<!-- 开启spring的@valid验证 -->
<mvc:annotation-driven/>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" >
<property name="paramName" value="sys_language"/>
</bean>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="sys_language"/>
<property name="cookieMaxAge" value="3600"/>
<property name="defaultLocale" value="zh_CN"/>
</bean>
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/" />
<property name="configLocation" value="classpath:velocity.properties"/>
</bean>
<bean class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"
p:viewClass="com.velocity.VelocityLayoutToolboxView"
p:order="0"
p:prefix=""
p:suffix=".vm"
p:cache="false"
p:layoutUrl="/WEB-INF/default_layout.vm"
p:contentType="text/html;charset=UTF-8"
p:exposeSpringMacroHelpers="true"
p:exposeRequestAttributes="true"
p:allowRequestOverride="true"
p:allowSessionOverride="true"
p:requestContextAttribute="rc"
p:toolboxConfigLocation="/WEB-INF/conf/velocity-tools.xml"
p:dateToolAttribute="date" />
</beans>