springmvc formatter

以下,来自于Springmvc指南第二版,第93页。

Spring的Formatter是可以将一种类型转为另一种类型。

例如用户输入的date类型可能有多种格式。

下面是才用 registrar方式注册formatter

比如:在controller中接收一个LocalDate。

    @RequestMapping("/test")
    public String test(@RequestParam(required = false) LocalDate date){
        System.out.println("date = " + date);
        return null;
    }

  注意:LocalDate,比较特殊点,不能new,前面必须要用 required=false,不用的话,spring会试图去new一个LocalDate,然后就会引发异常。

自定义Formatter:

public class LocalDateFormatter implements Formatter<LocalDate>{
    private DateTimeFormatter formatter;
    private String datePattern;

    public LocalDateFormatter(String datePattern) {
        this.datePattern = datePattern;
        formatter = DateTimeFormatter.ofPattern(datePattern);
    }

    @Override
    public LocalDate parse(String s, Locale locale) throws ParseException {
        try {
            return LocalDate.parse(s, DateTimeFormatter.ofPattern(datePattern));
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public String print(LocalDate localDate, Locale locale) {
        return localDate.format(formatter);
    }
}

FormatterRegistrar:

public class MyFormatterRegistrar implements FormatterRegistrar{
    private String datePattern;

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

    @Override
    public void registerFormatters(FormatterRegistry formatterRegistry) {
        formatterRegistry.addFormatter(new LocalDateFormatter(datePattern));
    }
}

dispatcher-servlet.xml

  <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="formatterRegistrars">
            <set>
                <bean class="registrar.MyFormatterRegistrar">
                    <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
                </bean>
            </set>
        </property>
    </bean>

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

 直接注册formatter,不同registrar

dispatcher-servlet.xml

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="formatters">
            <set>
                <bean class="formatter.LocalDateFormatter">
                    <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
                </bean>
            </set>
        </property>
    </bean>

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

  

 

 

Formatter和Converter,都能转换类型

Converter是一种类型转换另一种,可以用在很多层中

Formatter是String转换另一种,适用于web层,springmvc程序中推荐使用

 

转载于:https://www.cnblogs.com/yanqin/p/7054164.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值