您的位置:首页 > 编程语言 > Java开发

Spring mvc接收前端日期类型字符串,AnnotationMethodHandlerAdapter配置webBindingInitializer失效解决方

2016-03-02 23:16 597 查看
场景:spring mvc3.1.1 上,Spring mvc接收前端日期类型字符串通过AnnotationMethodHandlerAdapter配置webBindingInitializer失效解决方案
问题:

spring vmc3.1.1 下,通过AnnotationMethodHandlerAdapter配置webBindingInitializer失效:

 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.xxx.ark.core.web.CustomerBindingInitializer" />
</property>
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>

<mvc:annotation-driven >
<mvc:argument-resolvers>
<bean class="com.xxx.ark.core.web.FormBeanArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>

 

 原因:

annotation-driven缺省注册类的改变 

Spring 3.0.x中使用了annotation-driven后,缺省使用DefaultAnnotationHandlerMapping 来注册handler method和request的mapping关系。 AnnotationMethodHandlerAdapter来在实际调用handlermethod前对其参数进行处理。 

 

在spring mvc 3.1中,对应变更为 
DefaultAnnotationHandlerMapping -> RequestMappingHandlerMapping 
AnnotationMethodHandlerAdapter -> RequestMappingHandlerAdapter 
AnnotationMethodHandlerExceptionResolver -> ExceptionHandlerExceptionResolver 

以上都在使用了annotation-driven后自动注册。 
  而且对应分别提供了AbstractHandlerMethodMapping , AbstractHandlerMethodAdapter和 AbstractHandlerMethodExceptionResolver以便于让用户更方便的实现自定义的实现类。 

 

<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。

 

spring mvc <mvc:annotation-driven />会自动启动Spring MVC的注解功能,但实际它做了哪些工作呢? 

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
</bean>
<bean id="conversionService" class="org.springframework.samples.petclinic.util.PetclinicConversionServiceFactory" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

 

 

从上面的配置可以看出,我的配置应该是被sping的配置覆盖了,<mvc:annotation-driven />配置中已经包含了webBindingInitializer的配置,看来使用<mvc:annotation-driven />后与原来的配置出现了重复,这种情况下不管<mvc:annotation-driven
/>放在上面还是放在下面都会出现问题。

 

解决方法:

使用conversion-service来注册自定义的converter 
DataBinder实现了PropertyEditorRegistry, TypeConverter这两个interface,而在spring mvc实际处理时,返回值都是return binder.convertIfNecessary(见HandlerMethodInvoker中的具体处理逻辑)。因此可以使用customer conversionService来实现自定义的类型转换。 

从<mvc:annotation-driven />中配置可以看出,AnnotationMethodHandlerAdapter已经配置了webBindingInitializer,我们可以通过设置其属性conversionService来实现自定义类型转换。

 

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.wan.util.convert.DateConverter" />
</list>
</property>
</bean>

 

 需要修改spring service context xml配置文件中的annotation-driven,增加属性conversion-service指向新增的conversionService bean。 

 <mvc:annotation-driven conversion-service="conversionService" />

 实际自定义的converter如下。 

public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
public static final String[] DATE_FORMATS = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" };
        @Override
        public Date convert(String source) {
            Date date = null;
            try {
                date = DateUtils.parseDate(source, DATE_FORMATS);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: