字段格式化

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. scrapy中发送post请求

    1.可以使用`yield scrapy.FormRequest(url,formdata,callback)`方法发送POST请求. 其中构造参数formdata可以是字典,也可以是可迭代的(key, ...

  2. Shiro 身份认证绕过漏洞 CVE-2022-32532

    前言 Apache Shiro 是一个强大且易用的 Java 安全框架,通过它可以执行身份验证.授权.密码和会话管理.使用 Shiro 的易用 API,您可以快速.轻松地保护任何应用程序 -- 从最小 ...

  3. 攻防世界-file_include(绕过base64)

    有简单过滤防护的文件包含  一.源码分析 易知$filename处可能会存在文件包含漏洞.但注意到include了一个./check.php文件,猜测可能有过滤.先不管,于是直接使用普通payload ...

  4. win10格式化U盘提示没有权限执行此操作

    解决办法参考:http://www.tpbz008.cn/post/766.html 1.gpedit.msc 2.展开计算机配置,管理模板.展开系统.选中可移动存储访问 3.所有可移动存储类:拒绝所 ...

  5. Error building Player because scripts had compiler errors

    报错信息如下: Error building Player because scripts had compiler errors Build completed with a result of ' ...

  6. yum无法安装nginx

    yum无法安装nginx,检查yum配置文件  

  7. cmake 设置属性INTERFACE_INCLUDE_DIRECTORIES,则其它库可以直接 target_link_libraries?

    rs项目改为cpm下载 项目  leveldb 和 basiccache, basiccache依赖 leveldb,下载都是在主项目中, 设置 INTERFACE_INCLUDE_DIRECTORI ...

  8. pat乙级1012数字分类

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int ...

  9. hutools密码算法库

    hutool密码算法库 一.开发背景 Hutool针对Bouncy Castle做了简化包装,用于实现国密算法中的SM2.SM3.SM4. 国密算法工具封装包括: 非对称加密和签名:SM2 摘要签名算 ...

  10. 86、linux离线安装nginx

    参考 nginx  离线安装https://blog.csdn.net/ywd1992/article/details/83095855