Springmvc How to accept special types of fields

Source: Internet
Author: User
Tags dateformat

Springmvc If the field type cannot be encapsulated (for example, date) when receiving incoming data from the foreground, a " Request for Error" will appear with the following workaround.

1. Add annotations before the fields that need to be processed:

@DateTimeFormat (pattern = "YYYY-MM-DD")

The Joda-time.jar package is then introduced into the project and finally added to the configuration in the SPRINGMVC configuration XML file: <mvc:annotation-driven/> . This sentence configuration is a shorthand, in fact, the spring container is injected with two Bena, respectively: Defaultannotationhandlermapping and Annotationmethodhandleradapter. The inside of the @DateTimeFormat annotation also needs to be processed using the two beans injected earlier, so without this configuration, there is no corresponding bean in the Spring container to handle the annotations also error. At this point, all the steps are complete and you can run. 、

2. Use the @initbinder label provided by SPRINGMVC:

Add an editor to the control layer

 /**   * Set Control layer Special field conversion rules (such as Date), overloading *  @param   binder  */  @InitBinder  protected  void   Initbinder (Webdatabinder binder) {SimpleDateFormat DateFormat           = new  SimpleDateFormat ("Yyyy-mm-dd" );          Dateformat.setlenient ( false  ); Binder.registercustomeditor (Date.  class , true  

Spring provides a large number of implementation classes, such as customdateeditor, custombooleaneditor,customnumbereditor, etc.

@InitBinderprotected voidInitbinder (Webdatabinder binder) {binder.registercustomeditor (Date.class,NewCustomdateeditor (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"),true)); /Binder.registercustomeditor (int.class,NewCustomnumbereditor (int.class,true)); Binder.registercustomeditor (int.class,Newintegereditor ()); /Binder.registercustomeditor (Long.class,NewCustomnumbereditor (Long.class,true)); Binder.registercustomeditor (Long.class,Newlongeditor ()); Binder.registercustomeditor (Double.class,Newdoubleeditor ()); Binder.registercustomeditor (float.class,Newfloateditor ()); }   

You can also implement custom editing classes by inheriting Propertieseditor without using the own editor classes

ImportOrg.springframework.beans.propertyeditors.PropertiesEditor; Public classDoubleeditorextendsPropertieseditor {@Override Public voidSetastext (String text)throwsIllegalArgumentException {if(Text = =NULL|| Text.equals ("") ) {text= "0";      } setValue (Double.parsedouble (text)); } @Override PublicString Getastext () {returnGetValue (). toString (); }  } 

ImportOrg.springframework.beans.propertyeditors.PropertiesEditor; Public classIntegereditorextendsPropertieseditor {@Override Public voidSetastext (String text)throwsIllegalArgumentException {if(Text = =NULL|| Text.equals ("") ) {text= "0";      } setValue (Integer.parseint (text)); } @Override PublicString Getastext () {returnGetValue (). toString (); }  } 
ImportOrg.springframework.beans.propertyeditors.PropertiesEditor; Public classFloateditorextendsPropertieseditor {@Override Public voidSetastext (String text)throwsIllegalArgumentException {if(Text = =NULL|| Text.equals ("") ) {text= "0";      } setValue (Float.parsefloat (text)); } @Override PublicString Getastext () {returnGetValue (). toString (); }  }  
ImportOrg.springframework.beans.propertyeditors.PropertiesEditor; Public classLongeditorextendsPropertieseditor {@Override Public voidSetastext (String text)throwsIllegalArgumentException {if(Text = =NULL|| Text.equals ("") ) {text= "0";      } setValue (Long.parselong (text)); } @Override PublicString Getastext () {returnGetValue (). toString (); }  }  

3: Configure the Global date converter.

http://blog.csdn.net/chenleixing/article/details/45156617

Springmvc How to accept special types of fields

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.