Spring MVC custom type conversion

Type conversion can convert the request parameter to the specified type, specified format (format of data), and then pass it to the parameter of the business method.

Spring MVC has built-in common type converters. If the built-in type converter cannot meet the requirements, you can use a custom type converter.

 

There are two ways to customize type conversion:

  • Using Converter
  • Using Formatter

 

 

 

 

Converter mode

(1) Create a new package, com.chy.converter, under which a new class, DateConverter, is created as a custom type converter

// Need to achieve Converter Interface,<Primitive type,Target type>
public class DateConverter implements Converter<String, Date> {

    //Parameter is the original data in the request parameter, and the return value is the converted data (target type)
    @Override
    public Date convert(String source) {
       // After some operations, convert the data of the original type to the data of the target type and return
       // ......
    }

}

 

 

(2) In the configuration of spring mvc, add:

    <!--To configure ConversionService-->
    <bean name="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <!--Use collection to pass in custom converter-->
        <property name="converters">
            <set>
                <!--One bean Corresponding to a type converter-->
                <bean class="com.chy.com.chy.converter.DateConverter" />
            </set>
        </property>
    </bean>

    <!--Specify the ConversionService-->
    <mvc:annotation-driven conversion-service="conversionService" />

 

 

 

 

Formatter mode

(1) Create a new package com.chy.converter, and create a new class DateFormatter under the package

// Need to achieve Converter Interface,<Target type>
public class DateConverter implements Formatter<Date> {
    //parse()Yes, convert the original type to the target type, and the parameter is the original type( String)
    @Override
    public Date parse(String source, Locale locale) throws ParseException {
        
    }

    //print()Is the formatted output, which converts the target type to a string of the specified format. The parameters are parse()Convert good target types
    @Override
    public String print(Date date, Locale locale) {
        
    }
}

The type conversion and data format of Formatter are separated and written in two methods.

 

 

(2) In the configuration of spring mvc, add:

    <!--To configure ConversionService-->
    <bean name="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!--Use collection to pass in custom formatter-->
        <property name="formatters">
            <set>
                <!--One bean Corresponding to one formatter-->
                <bean class="com.chy.com.chy.converter.DateConverter" />
            </set>
        </property>
    </bean>

    <!--Specify the ConversionService-->
    <mvc:annotation-driven conversion-service="conversionService" />

 

 

 

  

Stepped pits:

xmlns:mvc="http://www.springframework.org/schema/cache"

The constraint of IDEA automatic import is not correct. Change the cache to mvc, and the corresponding part of xsi should also be changed.

Tags: Java Spring

Posted on Thu, 30 Jan 2020 23:30:31 -0800 by noeledoran