字段格式化

Spring 3 引入了一个方便的SPI,它为客户端环境的实现Formatter提供了一个简单而健壮的替代方

Formatter

Formatter实现字段格式化逻辑的SPI 。

package org.springframework.format.datetime;

public final class DateFormatter implements Formatter<Date> {

    private String pattern;

    public DateFormatter(String pattern) {
this.pattern = pattern;
} public String print(Date date, Locale locale) {
if (date == null) {
return "";
}
return getDateFormat(locale).format(date);
} public Date parse(String formatted, Locale locale) throws ParseException {
if (formatted.length() == 0) {
return null;
}
return getDateFormat(locale).parse(formatted);
} protected DateFormat getDateFormat(Locale locale) {
DateFormat dateFormat = new SimpleDateFormat(this.pattern, locale);
dateFormat.setLenient(false);
return dateFormat;
}
}

注释驱动的格式

字段格式可以通过字段类型或注释进行配置。

要触发格式化,您可以使用@NumberFormat 注释字段,如以下示例所示:

public class MyModel {

    @NumberFormat(style=Style.CURRENCY)
private BigDecimal decimal;
}

以下示例用于@DateTimeFormat将 格式化java.util.Date为 ISO 日期 (yyyy-MM-dd):

public class MyModel {

    @DateTimeFormat(iso=ISO.DATE)
private Date date;
}

FormatterRegistry

FormatterRegistry是一个用于注册格式化程序和转换器的 SPI。

FormatterRegistrar

FormatterRegistrar是一个 SPI,用于通过 FormatterRegistry 注册格式化程序和转换器。下面的清单显示了它的接口定义:

package org.springframework.format;

public interface FormatterRegistrar {

    void registerFormatters(FormatterRegistry registry);
}

FormatterRegistrar在为给定的格式类别(例如日期格式)注册多个相关转换器和格式器时很有用。在声明式注册不足的情况下,它也很有用——例如,当格式化程序需要在与其自身不同的特定字段类型下进行索引时,``或者在注册Printer/Parser对时。下一节提供有关转换器和格式化程序注册的更多信息。

在 Spring MVC 中配置格式化

请参阅Spring MVC 章节中的转换和格式化

Spring系列之字段格式化-13的更多相关文章

  1. Spring系列13:bean的生命周期

    本文内容 bean的完整的生命周期 生命周期回调接口 Aware接口详解 Spring Bean的生命周期 面试热题:请描述下Spring的生命周期? 4大生命周期 从源码角度来说,简单分为4大阶段: ...

  2. Spring系列之新注解配置+Spring集成junit+注解注入

    Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...

  3. Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级

    Spring系列之事务的控制 注解实现+xml实现 在前面我写过一篇关于事务的文章,大家可以先去看看那一篇再看这一篇,学习起来会更加得心应手 链接:https://blog.csdn.net/pjh8 ...

  4. Spring系列之DI的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 前言 在上一章中,我们介绍和简单实现了容器的部分功能,但是这里还留下了很多的问题.比如我们在构造bean实例的时 ...

  5. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  6. LocalDateTime在spring boot中的格式化配置

    在项目中日期格式化是最常见的问题,之前涉及的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,非线程安全,对日期的计算方式繁琐,而且容易出错,因为月份是 ...

  7. Spring系列(二):Spring IoC应用

    一.Spring IoC的核心概念 IoC(Inversion of Control  控制反转),详细的概念见Spring系列(一):Spring核心概念 二.Spring IoC的应用 1.定义B ...

  8. Spring系列(五):Spring AOP源码解析

    一.@EnableAspectJAutoProxy注解 在主配置类中添加@EnableAspectJAutoProxy注解,开启aop支持,那么@EnableAspectJAutoProxy到底做了什 ...

  9. 朱晔和你聊Spring系列S1E11:小测Spring Cloud Kubernetes @ 阿里云K8S

    有关Spring Cloud Kubernates(以下简称SCK)详见https://github.com/spring-cloud/spring-cloud-kubernetes,在本文中我们主要 ...

  10. Spring系列 之数据源的配置 数据库 数据源 连接池的区别

    Spring系列之数据源的配置 数据源,连接池,数据库三者的区别 连接池:这个应该都学习过,比如c3p0,druid等等,连接池的作用是为了提高程序的效率,因为频繁的去创建,关闭数据库连接,会对性能有 ...

随机推荐

  1. webapi fromurl frombody

    https://blog.csdn.net/QiGary/article/details/113979877 在做后台api接口时,常常涉及到Http方法访问问题,其中最基础也是最核心的就是传参问题. ...

  2. https://www.cnblogs.com/DKSoft/category/608549.html

    https://www.cnblogs.com/DKSoft/category/608549.html

  3. excel数字转日期

    import datetime delta = datetime.timedelta() today = datetime.datetime.strptime('1899/12/30', '%Y/%m ...

  4. revit卸载工具,完全彻底卸载删除干净revit各种残留注册表和文件的方法和步骤。

    revit卸载工具,完全彻底卸载删除干净revit各种残留注册表和文件的方法和步骤.如何卸载revit呢?有很多同学想把revit卸载后重新安装,但是发现revit安装到一半就失败了或者显示revit ...

  5. Codeforces Round #843 (Div. 2) C【思维】

    https://codeforces.com/contest/1775/problem/C 题意 题意是说,给你n和x,你要求出最小的满足要求的m,使得 \(n\)&\((n+1)\)& ...

  6. Unity 读取Json文件、创建Json文件

    using System.IO; using UnityEngine; public class ReadJson:MonoBehaviour { public static TestSetting ...

  7. oracle学习之索引的介绍

    在关系型数据库中,用户查找数据与行的物理位置无关紧要.为了能够找到数据,表中的每一行均用一个rowid来标识,rowid能够标识数据库中某一行的具体位置.当Oracle数据库中存储海量的记录时,就意味 ...

  8. python获取当前运行函数名

    两种方式: #!/usr/bin/env python3#coding:utf-8 import sys, inspectdef test_a(): print('func name: ', sys. ...

  9. sqlalchemy 数据类型

  10. vue 祖孙传值