spring-mvc的Conveter和Formatter

1. Converter

Spring的Converter可以将一种类型转换成另一种类型。
在使用时,必须编写一个实现org.springframework.core.convert.converter.Converter接口的java类。这个接口的声明如下

public interface Converter<S, T> {
    T convert(S var1);
}

这里的S表示源类型,T表示目标类型。
下面展示了一个将String类型转换成Date类型的Converter


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date>{
    private static final Log logger = LogFactory.getLog(StringToDateConverter.class);
    private String datePattern;

    public StringToDateConverter(String datePattern) {
        this.datePattern = datePattern;
    }

    public Date convert(String s) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
            dateFormat.setLenient(false);
            return dateFormat.parse(s);
        } catch (ParseException e) {
            throw new IllegalArgumentException("invalid date format. Please use this pattern\"" + datePattern + "\"");
        }
    }
}

为了使用Spring MVC应用程序定制的Converter,需要在配置文件中添加一个conversionService bean。Bean的类名称必须为org.springframework.context.support.ConversionServiceFactoryBean。这个bean必须包含一个converters属性,它列出要在应用程序中使用的所有定制Converter。下面bean声明注册了上面StringToDateConverter。

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="app06a.converter.StringToDateConverter">
                <constructor-arg name="datePattern" value="yyyy-MM-dd"/>
            </bean>
        </set>
    </property>
</bean>

之后,还需要给annotation-driven元素的conversion-service属性赋上bean名称,如下

<mvc:annotation-driven conversion-service="conversionService"/>

2. Formatter

Formatter和Converter一样,也是将一种类型转换成另一种类型。但是,Formatter的源类型必须是一个String。
在使用时,必须编写一个实现org.springframework.format.Formatter接口的java类。这个接口的声明如下

public interface Formatter<T> extends Printer<T>, Parser<T> {
}
public interface Printer<T> {
    String print(T var1, Locale var2);
}
public interface Parser<T> {
    T parse(String var1, Locale var2) throws ParseException;
}

这里的T表示输入字符串要转换的目标类型。
parse方法利用指定的Locale将一个String解析成目标类型。print方法相反,它是返回目标对象的字符串表示法。
下面展示了一个将String类型转换成Date类型的Formatter

import org.springframework.format.Formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatter implements Formatter<Date>{
    private String datePattern;
    private SimpleDateFormat dateFormat;

    public DateFormatter(String datePattern) {
        this.dateFormat = dateFormat;
        dateFormat = new SimpleDateFormat(datePattern);
        dateFormat.setLenient(false);
    }
    public Date parse(String s, Locale locale) throws ParseException {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
            dateFormat.setLenient(false);
            return dateFormat.parse(s);
        } catch (ParseException e) {
            throw new IllegalArgumentException("invalid date format. Please use this pattern\"" + datePattern + "\"");
        }
    }

    public String print(Date date, Locale locale) {
        return dateFormat.format(date);
    }
}

为了使用Spring MVC应用程序定制的Formatter,需要在配置文件中添加一个conversionService bean。Bean的类名称必须为org.springframework.format.support.FormattingConversionServiceFactoryBean。这个bean可以用一个formatters属性注册Formatter,用一个converters属性注册Converter。下面bean声明注册了上面DateFormatter。

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
        <set>
            <bean class="app06a.formatter.DateFormatter">
                <constructor-arg name="datePattern" value="yyyy-MM-dd"/>
            </bean>
        </set>
    </property>
</bean>

之后,还需要给annotation-driven元素的conversion-service属性赋上bean名称,如下

<mvc:annotation-driven conversion-service="conversionService"/>

3. 用Registrar注册Formatter

注册Formatter的另一种方法是使用Registrar。
下面就用Registrar来注册前面的DateFormatter。
先需要实现org.springframework.format.FormatterRegistrar接口,如下

import org.springframework.format.FormatterRegistrar;
import org.springframework.format.FormatterRegistry;

public class MyFormatterRegistrar implements FormatterRegistrar {
    private String datePattern;

    public MyFormatterRegistrar(String datePattern) {
        this.datePattern = datePattern;
    }

    public void registerFormatters(FormatterRegistry formatterRegistry) {
        formatterRegistry.addFormatter(new DateFormatter(datePattern));
        //register more formatters here
    }
}

有了Registrar,就不需要在Spring MVC配置文件中注册Formatter,只要在配置文件中注册Registrar就行,如下

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatterRegistrars">
        <set>
            <bean class="app06a.formatter.MyFormatterRegistrar">
                <constructor-arg name="datePattern" value="yyyy-MM-dd"/>
            </bean>
        </set>
    </property>
</bean>

4. 选择Converter还是Formatter

Converter是一般工具,可以将一种类型转换成另一种类型。例如,将String转换成Date,或者将Long转换成Date。Converter既可以用在web层,也可以用在其它层中。
Formatter只能将String转成成另一种java类型。例如,将String转换成Date,但它不能将Long转换成Date。所以,Formatter适用于web层。为此,在Spring MVC应用程序中,选择Formatter比选择Converter更合适。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值