SPRINGMVC Date format pass-through method of two (total two) @InitBinder use

Source: Internet
Author: User

About the resolution of the SPRINGMVC date problem in addition to this blog, "SPRINGMVC date format data transfer resolution @DateTimeFormat use and Configuration" article,

There are also the following ways:

Add this code to the controller:

1 @InitBinder2      Public voidInitbinder (Servletrequestdatabinder binder) {3         /**4 * field format for automatic conversion of date types5          */6SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd");7Binder.registercustomeditor (Date.class,NewCustomdateeditor (SDF,true));8 9         Ten}

Note that if the foreground has multiple date formats, There are no eggs to write in a manner similar to the following:

1 @InitBinder2      Public voidInitbinder (Servletrequestdatabinder binder) {3         /**4 * field format for automatic conversion of date types5          */6SimpleDateFormat SDF1 =NewSimpleDateFormat ("Yyyy-mm-dd");7SimpleDateFormat SDF2 =NewSimpleDateFormat ("yyyy mm month");8         Try {9Binder.registercustomeditor (Date.class,NewCustomdateeditor (SDF2,true));Ten}Catch(Exception e) { OneBinder.registercustomeditor (Date.class,NewCustomdateeditor (SDF1,true)); A         } -          -}

How to solve it?

Refer to this question: http://bbs.csdn.net/topics/380055180

The original content is as follows :

----------------------------------------------------------Dividing line begins-----------------------------------------------------------

Configuration file is

12345 <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">      <propertyname="webBindingInitializer">          <beanclass="xx.xxx.MyBindingInitializer"/>      </property>  </bean



In the Mybindinginitializer, the Initbinder method
Binder.registercustomeditor (Date.class, New Xxxeditor ());


In Xxxeditor's Setastext method, the system uses all possible format formats one by one to try, catch exceptions, and finally bind correctly.

-------------------------------------------------------end of the dividing line------------------------------------------------------------- ---

As for the Xxxeditor how to write, we can refer to the example of Customdateeditor, That is: Org.springframework.beans.propertyeditors.CustomDateEditor source code in how to write.


SPRINGMVC Date format pass-through method of two (total two) @InitBinder use

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.