PropertyEditor、Formatter、Converter的应用

@ResponseBody
@RequestMapping("date.do")
public String data(Date date) {
    return date.toString();
}

  访问路径:http://localhost:8080/date.do?date=1996-11-11

一、PropertyEditor的应用

  内置的可扩展性;局部使用webdatabinder

实例:

@InitBinder("date")
public void initDate(WebDataBinder binder) {
    String pattern = "yyyy-MM-dd";
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(pattern), true));
}

 

二、Formatter的应用

  内置的可扩展性;全局/局部;Source=String

实例一:

  编写DateFormatter.java类:

package com.tyk.service.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 pattern;

    public DateFormatter(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(text);
    }

    @Override
    public String print(Date object, Locale locale) {
        return object.toString();
    }
}

  配置文件spring-*.xml:

<mvc:annotation-driven conversion-service="dateFormatter"/>
<bean id="dateFormatter" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
        <set>
            <bean class="com.tyk.service.formatter.DateFormatter">
                <constructor-arg value="yyyy-MM-dd" type="java.lang.String"/>
            </bean>
        </set>
    </property>
</bean>

 实例二:

  编写DateRegistrar.java类:

package com.tyk.service.formatter;

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

public class DateRegistrar implements FormatterRegistrar {
    private String pattern;

    public DateRegistrar(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public void registerFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter(pattern));
    }
}

  配置文件spring-*.xml:

<mvc:annotation-driven conversion-service="dateRegistrar"/>
<bean id="dateRegistrar" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatterRegistrars">
        <set>
            <bean class="com.tyk.service.formatter.DateRegistrar">
                <constructor-arg value="yyyy-MM-dd" type="java.lang.String"/>
            </bean>
        </set>
    </property>
</bean>

 

三、Converter的应用

  内置的不可扩展性;全局/局部;Source=自定义

实例:

  编写DateConverter.java类

package com.tyk.service.converter;

import org.springframework.core.convert.converter.Converter;

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

public class DateConverter implements Converter<String, Date> {
    private String pattern;

    public DateConverter(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public Date convert(String source) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

  配置文件spring-*.xml:

<mvc:annotation-driven conversion-service="dateConverter"/>
<bean id="dateConverter" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="com.tyk.service.converter.DateConverter">
                <constructor-arg value="yyyy-MM-dd" type="java.lang.String"/>
            </bean>
        </list>
    </property>
</bean>

选择:

  局部:PropertyEditor

  全局: Web层:Formatter

      全部层:Converter

比较:

  Formatter的源类型必须是String,而Converter适用于任意的源类型

  Formatter只能将String转换成另一种Java类型,例如,将Spring转换为Date,但是他不能将Long转换成Date。因此,Formatter适用于Web层。为此,在Spring MVC应用程序中,选择Formatter比选择Converter更合适。

posted @ 2017-11-27 00:08  神秘的小岛岛  阅读(339)  评论(0编辑  收藏  举报