SpringMVC时间Date格式转换,400错误

转自:http://relive123-yahoo-com-cn.iteye.com/blog/1678376

spring mvc前台时间字符串到后台Date的转换

无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用org.springframework.format.support.FormattingConversionServiceFactoryBean 
之后,只要在vo里加注解就行了 
Java代码  收藏代码
  1. @DateTimeFormat(pattern="yyyy-MM-dd")  
  2. private Date dateRangeStart;// 通行日期范围开始  


@DateTimeFormat(pattern="yyyy-MM-dd") 可将形如1980-0-01的字符串转换到Date类 
@NumberFormat(pattern="#,###.##") 可将形如4,500.00的字符串转换成long类型 

怎么注册呢? 

Java代码  收藏代码
  1. <bean id="conversionService"    
  2. class="org.springframework.format.support.FormattingConversionServiceFactoryBean">   
  3. <mvc:annotation-driven validator="validator"    
  4.     conversion-service="conversionService" />    


使用<mvc:annotation-driven /> 的话,默认就启用FormattingConversionServiceFactoryBean了,所以上面的配置也省了。 
但是<mvc:annotation-driven />基本不用,因为总得做些个性化设置,那怎么注册FormattingConversionServiceFactoryBean给spring mvc呢? 
起初我以为得从DefaultAnnotationHandlerMapping入手,后来通过看<mvc:annotation-driven />的解析器AnnotationDrivenBeanDefinitionParser源码,才发现原来是AnnotationMethodHandlerAdapter的属性 
Java代码  收藏代码
  1. RuntimeBeanReference conversionService = getConversionService(element, source, parserContext);  
  2. RuntimeBeanReference validator = getValidator(element, source, parserContext);  
  3.   
  4. RootBeanDefinition bindingDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);  
  5. bindingDef.setSource(source);  
  6. bindingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);  
  7. bindingDef.getPropertyValues().add("conversionService", conversionService);  
  8. bindingDef.getPropertyValues().add("validator", validator);  
  9.   
  10. RootBeanDefinition annAdapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);  
  11. annAdapterDef.setSource(source);  
  12. annAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);  
  13. annAdapterDef.getPropertyValues().add("webBindingInitializer", bindingDef);  
  14. annAdapterDef.getPropertyValues().add("messageConverters", getMessageConverters(source));  

getConversionService方法内部 
Java代码  收藏代码
  1. if (element.hasAttribute("conversion-service")) {  
  2.     return new RuntimeBeanReference(element.getAttribute("conversion-service"));  
  3. }  
  4. else {  
  5.     RootBeanDefinition conversionDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);  
  6.     conversionDef.setSource(source);  
  7.     conversionDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);  
  8.     String conversionName = parserContext.getReaderContext().registerWithGeneratedName(conversionDef);  
  9.     parserContext.registerComponent(new BeanComponentDefinition(conversionDef, conversionName));  
  10.     return new RuntimeBeanReference(conversionName);  
  11. }  


原来<mvc:annotation-driven />是这么注册FormattingConversionServiceFactoryBean的 
如果不使用<mvc:annotation-driven />标签的话,只要配置AnnotationMethodHandlerAdapter的属性就可以了 
Java代码  收藏代码
  1. <property name="webBindingInitializer">  
  2.     <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">  
  3.         <property name="conversionService">  
  4.             <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>  
  5.         </property>  
  6.     </bean>  
  7. </property>  
posted @ 2016-04-25 16:06  thewindkee  阅读(423)  评论(0编辑  收藏  举报