Spring mvc uses the @ InitBinder label to bind the form data. mvc @ initbinder
In SpringMVC, bean defines the Date, double, and Other types. 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, I add the method initBinder to BaseController and use annotation @ InitBinder for annotation. Then spring mvc will first register these editors before binding the form. Of course, if you don't bother, you can also write it in each of your controllers. All the remaining controllers inherit the class. Spring itself provides a large number of implementation classes, such as CustomDateEditor, CustomBooleanEditor, and CustomNumberEditor, which are basically enough.
Of course, we can also choose not to use the built-in editor class, then we will construct several
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(); } }
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(); } }
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(); } }
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 BaseController
@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()); }
Copy codeThe Code is as follows:
Public class org. springframework. beans. propertyeditors. PropertiesEditor extends java. beans. PropertyEditorSupport {
See? If your editor class inherits PropertyEditorSupport, you can also.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.