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
- Import Org.springframework.beans.propertyeditors.PropertiesEditor;
- Public class Doubleeditor extends Propertieseditor {
- @Override
- public void Setastext (String text) throws illegalargumentexception {
- if (text = = Null | | text.equals ("")) {
- Text = "0";
- }
- SetValue (double.parsedouble (text));
- }
- @Override
- Public String Getastext () {
- return GetValue (). toString ();
- }
- }
[Java]View PlainCopy
- Import Org.springframework.beans.propertyeditors.PropertiesEditor;
- Public class Integereditor extends Propertieseditor {
- @Override
- public void Setastext (String text) throws illegalargumentexception {
- if (text = = Null | | text.equals ("")) {
- Text = "0";
- }
- SetValue (Integer.parseint (text));
- }
- @Override
- Public String Getastext () {
- return GetValue (). toString ();
- }
- }
[Java]View PlainCopy
- Import Org.springframework.beans.propertyeditors.PropertiesEditor;
- Public class Floateditor extends Propertieseditor {
- @Override
- public void Setastext (String text) throws illegalargumentexception {
- if (text = = Null | | text.equals ("")) {
- Text = "0";
- }
- SetValue (float.parsefloat (text));
- }
- @Override
- Public String Getastext () {
- return GetValue (). toString ();
- }
- }
[Java]View PlainCopy
- Import Org.springframework.beans.propertyeditors.PropertiesEditor;
- Public class Longeditor extends Propertieseditor {
- @Override
- public void Setastext (String text) throws illegalargumentexception {
- if (text = = Null | | text.equals ("")) {
- Text = "0";
- }
- SetValue (Long.parselong (text));
- }
- @Override
- Public String Getastext () {
- return GetValue (). toString ();
- }
- }
In the Basecontroller
[Java]View PlainCopy
- @InitBinder
- protected void Initbinder (Webdatabinder binder) {
- Binder.registercustomeditor (Date. Class, new Customdateeditor (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"), true);
- /binder.registercustomeditor (Int.class, new Customnumbereditor (Int.class, true));
- Binder.registercustomeditor (Int.class, new Integereditor ());
- /Binder.registercustomeditor (long. Class, new Customnumbereditor (long. class, true));
- Binder.registercustomeditor (long. Class, new Longeditor ());
- Binder.registercustomeditor (double. Class, new Doubleeditor ());
- Binder.registercustomeditor (float. Class, new Floateditor ());
- }
[Java]View PlainCopy
- Public class Org.springframework.beans.propertyeditors.PropertiesEditor extends Java.beans.PropertyEditorSupport {
Spring MVC uses @initbinder tags to bind form data