In Springmvc, the bean defines a type such as date,double, and if no processing is done, the date and double cannot be bound.
The solution is to use the @initbinder tag provided by spring MVC
In my project, adding method Initbinder to Basecontroller and using annotations @initbinder annotations, Spring MVC will register these editors before binding the form, of course, if you're not bothered, You can also write in each of your controllers individually. The rest of the controllers inherit the class. Spring itself provides a large number of implementation classes, such as Customdateeditor, Custombooleaneditor,customnumbereditor and many more, basically enough.
Of course, we can also not use his own to bring these editor classes, then we have to construct a few
[Java]View Plain copy
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 (); } }
In the Basecontroller
1 @InitBinder2 protected voidInitbinder (Webdatabinder binder) {3Binder.registercustomeditor (Date.class,NewCustomdateeditor (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"),true)); 4/Binder.registercustomeditor (int.class,NewCustomnumbereditor (int.class,true)); 5Binder.registercustomeditor (int.class,Newintegereditor ()); 6/Binder.registercustomeditor (Long.class,NewCustomnumbereditor (Long.class,true)); 7Binder.registercustomeditor (Long.class,Newlongeditor ()); 8Binder.registercustomeditor (Double.class,Newdoubleeditor ()); 9Binder.registercustomeditor (float.class,Newfloateditor ()); Ten}
[Java]View Plain copy
- Public class Org.springframework.beans.propertyeditors.PropertiesEditor extends Java.beans.PropertyEditorSupport {
If your editor class inherits PropertyEditorSupport directly, you can.
Spring MVC uses @initbinder tags to bind form data