Spring系列之字段格式化-13
字段格式化
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的更多相关文章
- Spring系列13:bean的生命周期
本文内容 bean的完整的生命周期 生命周期回调接口 Aware接口详解 Spring Bean的生命周期 面试热题:请描述下Spring的生命周期? 4大生命周期 从源码角度来说,简单分为4大阶段: ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级
Spring系列之事务的控制 注解实现+xml实现 在前面我写过一篇关于事务的文章,大家可以先去看看那一篇再看这一篇,学习起来会更加得心应手 链接:https://blog.csdn.net/pjh8 ...
- Spring系列之DI的原理及手动实现
目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 前言 在上一章中,我们介绍和简单实现了容器的部分功能,但是这里还留下了很多的问题.比如我们在构造bean实例的时 ...
- Spring系列之Spring常用注解总结 转载
Spring系列之Spring常用注解总结 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...
- LocalDateTime在spring boot中的格式化配置
在项目中日期格式化是最常见的问题,之前涉及的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,非线程安全,对日期的计算方式繁琐,而且容易出错,因为月份是 ...
- Spring系列(二):Spring IoC应用
一.Spring IoC的核心概念 IoC(Inversion of Control 控制反转),详细的概念见Spring系列(一):Spring核心概念 二.Spring IoC的应用 1.定义B ...
- Spring系列(五):Spring AOP源码解析
一.@EnableAspectJAutoProxy注解 在主配置类中添加@EnableAspectJAutoProxy注解,开启aop支持,那么@EnableAspectJAutoProxy到底做了什 ...
- 朱晔和你聊Spring系列S1E11:小测Spring Cloud Kubernetes @ 阿里云K8S
有关Spring Cloud Kubernates(以下简称SCK)详见https://github.com/spring-cloud/spring-cloud-kubernetes,在本文中我们主要 ...
- Spring系列 之数据源的配置 数据库 数据源 连接池的区别
Spring系列之数据源的配置 数据源,连接池,数据库三者的区别 连接池:这个应该都学习过,比如c3p0,druid等等,连接池的作用是为了提高程序的效率,因为频繁的去创建,关闭数据库连接,会对性能有 ...
随机推荐
- vue-seamless-scroll 组件用法-区域内容滚动效果
1.滚动效果 2.代码 <div class="page-container" style=""> <vue-seamless-scroll ...
- js 浮点数加、减、乘、除。
1.浮点数加法运算 function numAdd(arg1, arg2) { var r1, r2, m; try { r1 = arg1.toString().split("." ...
- Finance财务软件(如何运行该软件)
下载软件 首先,去gitHub下载该项目的release,尽量下载最新版本:https://github.com/edwardorchis/finance/releases.国内也可以在码云下载htt ...
- 简述47种Shader Map的渲染原理与制作方法
https://mp.weixin.qq.com/s/6EVpmgC53HqklIVkIpEssg
- CF319D 题解
题意 传送门 给你一个字符串 \(S\),要求你每次找到一个最短的并且最左边的形如 \(XX\)(即由两个相同的字符串拼接而成)的子串,然后把这个字符串从 \(XX\) 变成 \(X\).问无法操作后 ...
- android手机无线调试
1.手机与电脑先通过usb链接2.adb devices查看是否链接成功(链接成功会显示设备列表)3.adb tcpip 5555(0-65535之间取值,默认5555输进去)5:断开数据线,查看手机 ...
- Path Manipulation安全漏洞处理方法
1 public static String pathManipulation(String path) { 2 HashMap<String, String> map = new Has ...
- axios请求拦截封装理解
<p>全局处理请求.响应拦截的处理,常见处理请求动画,错误码</p> <p>import axios from 'axios'</p> <p> ...
- OSS管理文件(Java)
工具类 package com.panchina.util; import com.alibaba.druid.util.StringUtils; import com.aliyun.oss.Http ...
- Linux基础第八章:操作系统引导过程、运行级别及开机问题处理
一.操作系统引导过程 1.开机自检(bios) 2.MBR引导 3.grub菜单 4.加载内核(kernel) 5.init进程初始化 二.操作系统运行级别(init0-init6) 1.init 0 ...