SpringMVC配置全局日期轉換器,處理日期轉換異常
來自: http://blog.csdn.net//chenleixing/article/details/45156617
spring3.0配置日期轉換可以通過配置自定義實現WebBingingInitializer接口的一個日期轉換類來實現,方法如下
轉換類:
- public class DateConverter implements WebBindingInitializer {
- public void initBinder(WebDataBinder binder, WebRequest request) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
- }
- }
在spring-servlet.xml當中的進行注冊:
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <!-- 日期格式轉換 -->
- <property name="webBindingInitializer">
- <bean class="DateConverter" />
- </property>
- </bean>
spring3.1.1的處理進行調整,所以按照3.0的寫法在3.1.1里面是無效的,通過查找資料及測試,發現可行方法
原因:
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.doje.XXX.web.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) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- dateFormat.setLenient(false);
- try {
- return dateFormat.parse(source);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }