SpringMVC annotation @ initbinder solves the type conversion problem,
SpringMVC is often used to convert the Date string in the form and the Date type of the JavaBean. SpringMVC does not support conversion of this format by default, so you need to manually configure it, this problem can be solved only when custom data is bound.
Use the SpringMVC annotation @ initbinder and the WebDateBinder class that comes with Spring in the Controller that requires date conversion.
WebDataBinder is used to bind request parameters to the specified Attribute Editor. since the value uploaded to the controller at the front end is of the String type, when the value is Set in the Model, if the set attribute is an object, spring will find the Corresponding editor for conversion, and then SET it in.
The Code is as follows:
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
You need to add
<! -- Parser registration --> <bean class = "org. springframework. web. servlet. mvc. method. annotation. requestMappingHandlerAdapter "> <property name =" messageConverters "> <list> <ref bean =" stringHttpMessageConverter "/> </list> </property> </bean> <! -- String parser, which allows direct return of String-type messages --> <bean id = "stringHttpMessageConverter" class = "org. springframework. http. converter. StringHttpMessageConverter"/>
Alternative writing
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> </mvc:message-converters></mvc:annotation-driven>
Expansion:
Before binding forms, spring mvc registers these editors. Spring provides many implementation classes, such as CustomDateEditor, CustomBooleanEditor, and CustomNumberEditor, which are basically enough.
Call the registerCustomEditor method of WebDataBinder.
RegisterCustomEditor source code:
public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) { getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor);}
The first parameter requiredType is the type to be converted.
The second parameter, PropertyEditor, is the property Editor, which is an interface. The aforementioned CustomDateEditor class inherits the PropertyEditorSupport class that implements this interface.
We can also not use these built-in editor classes.
We can construct it ourselves:
import org.springframework.beans.propertyeditors.PropertiesEditor;public class DoubleEditor extends PropertyEditorSupport { @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(); }}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.