SpringMVC annotation @ initbinder solves the type conversion problem,

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.