SpringMVC annotation @initbinder solves type conversion problems

SpringMVC annotation @initbinder solves type conversion problems

When using SpringMVC, you often encounter the conversion of the date string in the form and the Date type of JavaBean, and SpringMVC does not support this format conversion by default, so manual configuration and custom data binding can solve this problem.
Use SpringMVC annotations @initbinderand Spring's own WebDateBinderclasses to operate in Controllers that require date conversion.
WebDataBinder is used to bind request parameters to the specified property editor. Since the value passed to the controller in the foreground is of String type, when the value is set in the Model, if the property of the set is an object, Spring will go to Find the corresponding editor to convert, and then SET in.
code show as below:

@InitBinder  
public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }

Need to be added to the SpringMVC configuration file

<!-- 解析器注册 -->  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter"/> </list> </property> </bean> <!-- String类型解析器,允许直接返回String类型的消息 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/> 

change the spelling

<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>

Extension:
Spring mvc will register these editors before binding the form. Spring provides a large number of implementation classes, such as CustomDateEditor, CustomBooleanEditor, CustomNumberEditor, etc., which are basically sufficient.
When using, 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 above-mentioned CustomDateEditor, etc. all inherit the PropertyEditorSupport class that implements this interface.
We can also not use these editor classes that come with them.
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(); } }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325753695&siteId=291194637