The spring3.0 configuration date conversion can be implemented by configuring a date conversion class that implements a custom implementation Webbinginginitializer interface, as follows
Convert class: Public class DateConverter implements Webbindinginitializer {public void Initbinder (Webdatabinder binde R, WebRequest request) {SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd"); Binder.registercustomeditor (Date.class, New Customdateeditor (DF, false)); } }
Register in Spring-servlet.xml: <bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "> <!--date format conversion--> & Lt;property name= "Webbindinginitializer" > <bean class= "dateconverter"/> </property> </bean>
spring3.1.1 of the processing of the adjustment, so according to 3.0 of the writing in 3.1.1 is not valid, through the search data and testing, found a feasible method
Reason:
Annotation-driven The default registration class change
After using Annotation-driven in Spring 3.0.x, the default defaultannotationhandlermapping is used to register the mapping relationship between handler method and request. Annotationmethodhandleradapter to process its arguments before the actual call to Handlermethod.
In spring MVC 3.1, the strain is more
Defaultannotationhandlermapping-> requestmappinghandlermapping
Annotationmethodhandleradapter-> Requestmappinghandleradapter
Annotationmethodhandlerexceptionresolver-> Exceptionhandlerexceptionresolver
All of the above are automatically registered after using the Annotation-driven.
and the corresponding respectively provided abstracthandlermethodmapping, Abstracthandlermethodadapter and Abstracthandlermethodexceptionresolver to make it easier for users to implement custom implementation classes.
<mvc:annotation-driven/> is equivalent to registering two beans for defaultannotationhandlermapping and Annotationmethodhandleradapter, Configure some messageconverter. That is to solve the use of the @controller annotation of the premise configuration.
Spring MVC <mvc:annotation-driven/> Automatically starts the annotation feature for spring MVC, but what does it actually do?
Java code <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" />
As can be seen from the above configuration, my configuration should be covered by sping configuration, <mvc:annotation-driven/> Configuration already contains Webbindinginitializer configuration, it appears to use <MVC: Annotation-driven/> After the original configuration has been duplicated, in this case, regardless of <mvc:annotation-driven/> placed above or placed below will have problems.
Workaround:
Use Conversion-service to register a custom converter
DataBinder implements Propertyeditorregistry, TypeConverter these two interface, and when spring MVC is actually processed, return values are returned Binder.convertifnecessary (see the specific processing logic in Handlermethodinvoker). Therefore, you can use the customer Conversionservice to implement custom type conversions.
As you can see from the configuration of <mvc:annotation-driven/>, Annotationmethodhandleradapter has been configured with Webbindinginitializer, We can implement custom type conversions by setting their properties Conversionservice.
Java code <bean id= "Conversionservice" class= " Org.springframework.format.support.FormattingConversionServiceFactoryBean "> <property name=" Converters "& Gt <list> <bean class= "Com.doje.XXX.web.DateConverter"/> </list> </property> </bean>
You need to modify the Annotation-driven in the Spring service context XML configuration file to add properties Conversion-service point to the new Conversionservice bean.
Java code <mvc:annotation-driven conversion-service= "Conversionservice"/>
The actual custom converter is as follows. Java code 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; }