spring3.0配置日期轉換能夠經過配置自定義實現WebBingingInitializer接口的一個日期轉換類來實現,方法以下web
轉換類:spring
public class DateConverter implements WebBindingInitializer { mvc
public void initBinder(WebDataBinder binder, WebRequest request) { app
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); ide
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false)); 測試
} spa
} .net
在spring-servlet.xml當中的進行註冊:orm
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> xml
<!-- 日期格式轉換 -->
<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的註解功能,但實際它作了哪些工做呢?
Java代碼
<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來實現自定義類型轉換。
Java代碼
<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。
Java代碼
<mvc:annotation-driven conversion-service="conversionService" />
實際自定義的converter以下。
Java代碼
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;
}