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