SpringMVC-initBinder SpringMVC comment @initbinder resolve type conversion

Standing on the shoulders of giants

SpringMVC annotation type conversion problem to solve @initbinder

 

https://blog.csdn.net/c5113620/article/details/79023137

 

When using SpringMVC often encounter form a date string Date and type of conversion JavaBean, and SpringMVC default does not support this format conversion, so the need to manually configure, self-defined data binding to solve this problem.

Annotations using SpringMVC need of the Controller date conversion @initbinderand comes Spring WebDateBinderclass operation.
WebDataBinder request parameter is used to bind to a specific property editor. As reception is reached controller in the value of type String, when to Model Set this value in time, if this property is a set of objects, Spring will go find the corresponding editor to convert, then SET to go.
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)); }

SpringMVC need to add profiles

<!-- 解析器注册 -->  
<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"/> 

Put another 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:
the Spring MVC before a binding form, will register these editors, Spring provides a lot of their own implementation class, such as CustomDateEditor, CustomBooleanEditor, CustomNumberEditor and many, basically enough.
Use the time to call WebDataBinder registerCustomEditor method
registerCustomEditor Source:

public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {
    getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor); }

The first parameter is the type required requiredType conversion.
The second parameter is a property editor PropertyEditor, which is an interface, such as mentioned above are all inherited CustomDateEditor achieve this PropertyEditorSupport class interface.
We can not use their own editors these classes.
We can construct your own:

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(); } }

Annotations using SpringMVC need of the Controller date conversion @initbinderand comes Spring WebDateBinderclass operation.
WebDataBinder request parameter is used to bind to a specific property editor. As reception is reached controller in the value of type String, when to Model Set this value in time, if this property is a set of objects, Spring will go find the corresponding editor to convert, then SET to go.
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)); }

SpringMVC need to add profiles

<!-- 解析器注册 -->  
<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"/> 

Put another 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:
the Spring MVC before a binding form, will register these editors, Spring provides a lot of their own implementation class, such as CustomDateEditor, CustomBooleanEditor, CustomNumberEditor and many, basically enough.
Use the time to call WebDataBinder registerCustomEditor method
registerCustomEditor Source:

public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {
    getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor); }

The first parameter is the type required requiredType conversion.
The second parameter is a property editor PropertyEditor, which is an interface, such as mentioned above are all inherited CustomDateEditor achieve this PropertyEditorSupport class interface.
We can not use their own editors these classes.
We can construct your own:

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 www.cnblogs.com/longxok/p/10931692.html