Spring MVC uses @initbinder tags to bind form data

Source: Internet
Author: User

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
    1. 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

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.