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 PlainCopy
  1. Import Org.springframework.beans.propertyeditors.PropertiesEditor;
  2. Public class Doubleeditor extends Propertieseditor {
  3. @Override
  4. public void Setastext (String text) throws illegalargumentexception {
  5. if (text = = Null | | text.equals ("")) {
  6. Text = "0";
  7. }
  8. SetValue (double.parsedouble (text));
  9. }
  10. @Override
  11. Public String Getastext () {
  12. return GetValue (). toString ();
  13. }
  14. }

[Java]View PlainCopy
  1. Import Org.springframework.beans.propertyeditors.PropertiesEditor;
  2. Public class Integereditor extends Propertieseditor {
  3. @Override
  4. public void Setastext (String text) throws illegalargumentexception {
  5. if (text = = Null | | text.equals ("")) {
  6. Text = "0";
  7. }
  8. SetValue (Integer.parseint (text));
  9. }
  10. @Override
  11. Public String Getastext () {
  12. return GetValue (). toString ();
  13. }
  14. }

[Java]View PlainCopy
  1. Import Org.springframework.beans.propertyeditors.PropertiesEditor;
  2. Public class Floateditor extends Propertieseditor {
  3. @Override
  4. public void Setastext (String text) throws illegalargumentexception {
  5. if (text = = Null | | text.equals ("")) {
  6. Text = "0";
  7. }
  8. SetValue (float.parsefloat (text));
  9. }
  10. @Override
  11. Public String Getastext () {
  12. return GetValue (). toString ();
  13. }
  14. }

[Java]View PlainCopy
  1. Import Org.springframework.beans.propertyeditors.PropertiesEditor;
  2. Public class Longeditor extends Propertieseditor {
  3. @Override
  4. public void Setastext (String text) throws illegalargumentexception {
  5. if (text = = Null | | text.equals ("")) {
  6. Text = "0";
  7. }
  8. SetValue (Long.parselong (text));
  9. }
  10. @Override
  11. Public String Getastext () {
  12. return GetValue (). toString ();
  13. }
  14. }


In the Basecontroller

[Java]View PlainCopy
  1. @InitBinder
  2. protected void Initbinder (Webdatabinder binder) {
  3. Binder.registercustomeditor (Date.     Class, new Customdateeditor (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"), true);
  4. /binder.registercustomeditor (Int.class, new Customnumbereditor (Int.class, true));
  5. Binder.registercustomeditor (Int.class, new Integereditor ());
  6. /Binder.registercustomeditor (long. Class, new Customnumbereditor (long.   class, true));
  7. Binder.registercustomeditor (long.     Class, new Longeditor ());
  8. Binder.registercustomeditor (double.     Class, new Doubleeditor ());
  9. Binder.registercustomeditor (float.     Class, new Floateditor ());
  10. }

[Java]View PlainCopy
      1. Public class Org.springframework.beans.propertyeditors.PropertiesEditor extends Java.beans.PropertyEditorSupport {

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.