SpringMVC(十二)_数据绑定源程之数据格式化

SpringMVC(十二)_数据绑定流程之数据格式化

       前言:本篇主要介绍SpringMVC的数据绑定流程中数据格式化的相关概念与用法

本篇文章重点关注以下问题:

  • SpringMVC的数据格式化架构
  • Spring内建的格式化转换器

  • 自定义格式转换器

1. SpringMVC的数据格式化架构

1.1 格式化转换器

       提供格式化转换的实现支持,对属性对象的输入/输出进行格式化,从其本质上讲依然属于 “类型转换” 的范畴。

SpringMVC(十二)_数据绑定源程之数据格式化
       一共有如下两组四个接口:

 1. Printer接口

          格式化显示接口,将T 类型的对象根据Locale信息以某种格式进行打印显示(即返回字符串形式);

@FunctionalInterface
public interface Printer<T> {
	String print(T object, Locale locale);
}

 2. Parser接口

         解析接口,根据Locale信息解析字符串到T类型的对象,解析失败可以抛出java.text.ParseException或IllegalArgumentException异常即可。

@FunctionalInterface
public interface Parser<T> {
	T parse(String text, Locale locale) throws ParseException;
}

 3、Formatter接口

         格式化SPI接口,继承Printer 和Parser 接口,完成T类型对象的格式化和解析功能;

public interface Formatter<T> extends Printer<T>, Parser<T> {

}

 4、AnnotationFormatterFactory接口

         注解驱动的字段格式化工厂,用于创建带注解的对象字段的Printer 和Parser,即用于格式化和解析带注解的对象字段

public interface AnnotationFormatterFactory<A extends Annotation> { // 可以识别的注解类型
	Set<Class<?>> getFieldTypes();                           // 可以被A注解类型注解的字段类型集合
	Printer<?> getPrinter(A annotation, Class<?> fieldType); // 根据A注解类型和fieldType类型获取Printer
	Parser<?> getParser(A annotation, Class<?> fieldType);   // 根据A注解类型和fieldType类型获取Parser
}

        此接口返回用于格式化和解析被A注解类型注解的字段值的Printer 和Parser 。如JodaDateTimeFormatAnnotationFormatterFactory可以为带有@DateTimeFormat注解的java.util.Date字段类型创建相应的Printer 和Parser 进行格式化和解析。

 

1.2 格式化转换器注册器、格式化服务

       提供类型转换器注册支持,运行时类型转换API支持。

SpringMVC(十二)_数据绑定源程之数据格式化
 其有两种接口:

1.FormatterRegistry

        格式化转换器注册器,用于注册格式化转换器(Formatter、Printer 和Parser、AnnotationFormatterFactory);

public interface FormatterRegistry extends ConverterRegistry {

	// 添加格式化转换器(Spring3.1 新增API)
	void addFormatter(Formatter<?> formatter);

	// 为指定的字段类型添加格式化转换器
	void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter);

	// 为指定的字段类型添加Printer 和Parser
	void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser);

	// 添加注解驱动的字段格式化工厂AnnotationFormatterFactory
	void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory);

}

 

2. FormattingConversionService

         继承自ConversionService,运行时类型转换和格式化服务接口,提供运行期类型转换和格式化的支持。该实现类扩展了 GenericConversionService,因此它既具有类型转换的功能,又具有格式化的功能。FormattingConversionService 拥有一个FormattingConversionServiceFactroyBean 工厂类,后者用于在 Spring 上下文中构造前者。

         FormattingConversionService内部实现如下图所示:

SpringMVC(十二)_数据绑定源程之数据格式化
       FormattingConversionService内部实现如上所示,当调用convert方法时:

  1. 若是S类型----->String:调用私有的静态内部类PrinterConverter,其又调用相应的Printer 的实现进行格式化;
  2. 若是String----->T类型:调用私有的静态内部类ParserConverter,其又调用相应的Parser 的实现进行解析;
  3. 若是A 注解类型注解的S 类型----->String:调用私有的静态内部类AnnotationPrinterConverter,其又调用相应的AnnotationFormatterFactory的getPrinter 获取Printer 的实现进行格式化;

  4. 若是String----->A 注解类型注解的T 类型:调用私有的静态内部类AnnotationParserConverter,其又调用相应的AnnotationFormatterFactory的getParser 获取Parser 的实现进行解析。

        注:S类型表示源类型,T类型表示目标类型,A表示注解类型。

        此处可以可以看出之前的Converter SPI 完成任意Object 与Object 之间的类型转换,而Formatter SPI 完成任意Object与String之间的类型转换。

 

2. Spring内建的格式化转换器

类名

说明

DateFormatter

java.util.Date<---->String(实现日期的格式化/解析)

NumberFormatter

java.lang.Number<---->String(实现通用样式的格式化/解析)

CurrencyFormatter

java.lang.BigDecimal<---->String(实现货币样式的格式化/解析)

PercentFormatter

java.lang.Number<---->String(实现百分数样式的格式化/解析)

NumberFormatAnnotationFormatterFactory

@NumberFormat注解类型的数字字段类型<---->String

①通过@NumberFormat指定格式化/解析格式

②可以格式化/解析的数字类型:Short、Integer、Long、Float、Double、BigDecimal、BigInteger

JodaDateTimeFormatAnnotationFormatterFactory

@DateTimeFormat注解类型的日期字段类型<---->String

          FormattingConversionServiceFactroyBean内部已经注册了NumberFormatAnnotationFormatterFactroy,JodaDateTimeFormatAnnotationFormatterFactroy。

3. 自定义格式转换器

      此处以解析/格式化AddressVo为例。字符串"江苏-南京" 格式化为 AddressVo:

SpringMVC(十二)_数据绑定源程之数据格式化SpringMVC(十二)_数据绑定源程之数据格式化
 

1. 定义Formatter实现

public class AddressFormatter_ implements Formatter<AddressVo> {
    // 中文正则表达式
    Pattern pattern = Pattern.compile("^([\u4e00-\u9fa5]*)-([\u4e00-\u9fa5]*)$");
    
    @Override
    public String print(AddressVo address, Locale locale) {
        if(address == null) return "";
        return new StringBuilder().append(address.getProvince())
                                  .append("-")
                                  .append(address.getCity())
                                  .toString();
    }

    @Override
    public AddressVo parse(String text, Locale locale) throws ParseException {
        if(!StringUtils.hasLength(text)) return null;
        
        Matcher matcher = pattern.matcher(text);
        if(matcher.matches()) {
            String province = matcher.group(1); 
            String city     = matcher.group(2); 
            return new AddressVo(province, city);
        } else {
            throw new IllegalArgumentException();
        }
    }
}

 2. 定义解析/格式化字段的注解类型

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AddressFormatter {

}

 3. 实现AnnotationFormatterFactory注解格式化工厂

public class AddressFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<AddressFormatter> {

    private final AddressFormatter_ formatter;
    
    public AddressFormatAnnotationFormatterFactory() {
        this.formatter = new AddressFormatter_();
    }
    
    //②指定可以被解析/格式化的字段类型集合
    @Override
    public Set<Class<?>> getFieldTypes() {
        Set<Class<?>> set = new HashSet<Class<?>>();
        set.add(AddressVo.class);
        return set;
    }
    
    @Override
    public Parser<?> getParser(AddressFormatter annotation, Class<?> fieldType) {
        return formatter;
    }

    @Override   
    public Printer<?> getPrinter(AddressFormatter annotation, Class<?> fieldType) {
        return formatter;
    }
}

 4. 对实体类添加注解

@AddressFormatter
private AddressVo address;

 5. 注册自定义转换器

<!-- 配置 ConversionService -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
        <set>
            <bean class="com.wj.web.formatters.AddressFormatAnnotationFormatterFactory"></bean>
        </set>
    </property>
</bean>

 

4. 简述Spring內建常用格式化注解用法

1.@DateTimeFormat

        @DateTimeFormat 注解可对java.util.Date、java.util.Calendar、java.long.Long 时间类型进行标注:

  • pattern 属性:类型为字符串。指定解析/格式化字段数据的模式,如:”yyyy-MM-dd hh:mm:ss”;
  • iso 属性:类型为 DateTimeFormat.ISO。指定解析/格式化字段数据的ISO模式,包括四种:ISO.NONE(不使用)  默认、ISO.DATE(yyyy-MM-dd) 、ISO.TIME(hh:mm:ss.SSSZ)、ISO.DATE_TIME(yyyy-MM-dd hh:mm:ss.SSSZ);
  • style 属性:字符串类型。通过样式指定日期时间的格式,由两位字符组成,第一位表示日期的格式,第二位表示时间的格式:S:短日期/时间格式、M:中日期/时间格式、L:长日期/时间格式、F:完整日期/时间格式、-:忽略日期或时间格式;

2. @NumberFormat

        可对类似数字类型的属性进行标注,它拥有两个互斥的属性:

  • style:类型为 NumberFormat.Style。用于指定– 样式类型,包括三种:Style.NUMBER(正常数字类型)、Style.CURRENCY(货币类型)、 Style.PERCENT(百分数类型);
  • pattern:类型为 String,自定义样式, 如patter="#,###";

3. 示例

@NumberFormat(pattern="#,####,####.#")
private double salary;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date   birth;
 

 

代码下载来源:http://super-wangj.iteye.com/blog/2388430