Springmvc one of the date format value resolution methods (total two) @DateTimeFormat usage and configuration

Source: Internet
Author: User

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
    1. @DateTimeFormat (pattern="Yyyy-mm-dd")
    2. 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
    1. <bean id="Conversionservice"
    2. class="Org.springframework.format.support.FormattingConversionServiceFactoryBean" >
    3. <mvc:annotation-driven validator="Validator"
    4. 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
  1. Runtimebeanreference Conversionservice = Getconversionservice (element, source, parsercontext);
  2. Runtimebeanreference validator = Getvalidator (element, source, parsercontext);
  3. Rootbeandefinition bindingdef = new Rootbeandefinition (Configurablewebbindinginitializer.   Class);
  4. Bindingdef.setsource (source);
  5. Bindingdef.setrole (beandefinition.role_infrastructure);
  6. Bindingdef.getpropertyvalues (). Add ("Conversionservice", Conversionservice);
  7. Bindingdef.getpropertyvalues (). Add ("validator", validator);
  8. Rootbeandefinition annadapterdef = new Rootbeandefinition (Annotationmethodhandleradapter.   Class);
  9. Annadapterdef.setsource (source);
  10. Annadapterdef.setrole (beandefinition.role_infrastructure);
  11. Annadapterdef.getpropertyvalues (). Add ("Webbindinginitializer", bindingdef);
  12. Annadapterdef.getpropertyvalues (). Add ("Messageconverters", getmessageconverters (source));


Getconversionservice Method Interior

Java code
  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. }



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.