springMVC 前后台日期格式传值解决方式之一(共二) @DateTimeFormat的使用和配置
无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用 org.springframework.format.support.FormattingConversionServiceFactoryBean
之后,只要在vo里加注解就行了
使用时要引入一个类库joda-time-n.n.jar
- @DateTimeFormat(pattern="yyyy-MM-dd")
- private Date dateRangeStart;// 通行日期范围开始
@DateTimeFormat(pattern="yyyy-MM-dd") 可将形如1980-0-01的字符串转换到Date类
@NumberFormat(pattern="#,###.##") 可将形如4,500.00的字符串转换成long类型
怎么注册呢?
- <bean id="conversionService"
- class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
- <mvc:annotation-driven validator="validator"
- conversion-service="conversionService" />
使用<mvc:annotation-driven /> 的话,默认就启用FormattingConversionServiceFactoryBean了,所以上面的配置也省了。
但是<mvc:annotation-driven />基本不用,因为总得做些个性化设置,那怎么注册FormattingConversionServiceFactoryBean给spring mvc呢?
起初我以为得从DefaultAnnotationHandlerMapping入手,后来通过看<mvc:annotation-
driven
/>的解析器AnnotationDrivenBeanDefinitionParser源码,才发现原来是
AnnotationMethodHandlerAdapter的属性
- RuntimeBeanReference conversionService = getConversionService(element, source, parserContext);
- RuntimeBeanReference validator = getValidator(element, source, parserContext);
- RootBeanDefinition bindingDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
- bindingDef.setSource(source);
- bindingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
- bindingDef.getPropertyValues().add("conversionService", conversionService);
- bindingDef.getPropertyValues().add("validator", validator);
- RootBeanDefinition annAdapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
- annAdapterDef.setSource(source);
- annAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
- annAdapterDef.getPropertyValues().add("webBindingInitializer", bindingDef);
- annAdapterDef.getPropertyValues().add("messageConverters", getMessageConverters(source));
getConversionService方法内部
- if (element.hasAttribute("conversion-service")) {
- return new RuntimeBeanReference(element.getAttribute("conversion-service"));
- }
- else {
- RootBeanDefinition conversionDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
- conversionDef.setSource(source);
- conversionDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
- String conversionName = parserContext.getReaderContext().registerWithGeneratedName(conversionDef);
- parserContext.registerComponent(new BeanComponentDefinition(conversionDef, conversionName));
- return new RuntimeBeanReference(conversionName);
- }
原来<mvc:annotation-driven />是这么注册FormattingConversionServiceFactoryBean的
如果不使用<mvc:annotation-driven />标签的话,只要配置AnnotationMethodHandlerAdapter的属性就可以了
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <!-- 支持@DateTimeFormat(pattern="yyyy-MM-dd")等注解 2015年7月31日11:07:03 liuyx--> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService"> <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean> </property> </bean> </property> </bean>