Inadvertently found that for the time string to date class, do not have to write their own conversion class, Spring MVC has implemented this feature, or annotation-based, easy and convenient, using Org.springframework.format.support.FormattingConversionServiceFactoryBean
After that, just add a note to VO.
To introduce a class library when using Joda-time-n.n.jar
Java code
- @DateTimeFormat (pattern="Yyyy-mm-dd")
- Private Date Daterangestart; //Date range start
@DateTimeFormat (pattern= "YYYY-MM-DD") converts a string of a shape like 1980-0-01 to a date class
@NumberFormat (pattern= "#,###.##") converts a string of the form 4,500.00 to a long type
How to register it?
Java code
- <bean id="Conversionservice"
- class="Org.springframework.format.support.FormattingConversionServiceFactoryBean" >
- <mvc:annotation-driven validator="Validator"
- conversion-service="Conversionservice"/>
With <mvc:annotation-driven/>, Formattingconversionservicefactorybean is enabled by default, so the above configuration is also saved.
But <mvc:annotation-driven/> Basic not, because must do some personalization, then how to register Formattingconversionservicefactorybean to spring MVC?
At first I thought I had to start from defaultannotationhandlermapping, and then by looking at <mvc:annotation-driven/> Parser Annotationdrivenbeandefinitionparser Source, only to find that the original is the Annotationmethodhandleradapter attribute
Java code
- 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 Method Interior
Java code
- 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);
- }
The original <mvc:annotation-driven/> is so registered Formattingconversionservicefactorybean
If you do not use the <mvc:annotation-driven/> tag, just configure the properties of the Annotationmethodhandleradapter.
<Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <!--Support @datetimeformat (pattern= "Yyyy-mm-dd") and other annotations July 31, 2015 11:07:03 Liuyx - < Propertyname= "Webbindinginitializer"> <Beanclass= "Org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> < Propertyname= "Conversionservice"> <Beanclass= "Org.springframework.format.support.FormattingConversionServiceFactoryBean"></Bean> </ Property> </Bean> </ Property> </Bean>
Original: http://relive123-yahoo-com-cn.iteye.com/blog/1678376
Springmvc one of the date format value resolution methods (total two) @DateTimeFormat usage and configuration