In SpringMVC, types such as Date, double are defined in beans. 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 in BaseController and use the annotation @InitBinder annotation. Then spring mvc will register these editors before binding the form. Of course, if you don't bother, you can also write them separately in each of your controllers. The remaining controllers inherit this class. spring itself provides a large number of implementation classes, such as CustomDateEditor, CustomBooleanEditor, CustomNumberEditor, etc., which are basically enough.
Of course, we can also not use these editor classes that come with our own. Let's construct a few of them ourselves.
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()); } The code copy is as follows:
public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {
See? It is also OK if your editor class directly inherits PropertyEditorSupport.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.