Spring MVC 的 Converter 和 Formatter
Converter 和 Formatter 都可用于将一种对象类型转换成另一种对象类型。
Converter 是通用元件,可以将一种类型转换成另一种类型,可以在应用程序中的任意层中使用;
Formatter 只能将 String 转换成另一种 Java 类型,是专门为 Web 层设计的;在 Spring MVC 应用程序中,选择 Formatter 比选择 Converter 更合适。
Converter
为了创建 Converter, 必须编写一个实现 org.springframework.core.coverter.converter.Converter 接口的 Java 类
public interface Converter<S, T> { // S 表示源类型,T 表示目标类型
public abstract T converter(S source);
}
为了使用 Spring MVC 应用程序中定制的 Converter,需要在 Spring MVC 配置文件中编写一个 conversionService bean。
Bean 的类名必须为 org.springframework.context.support.ConversionServiceFactoryBean。这个 bean 必须包含一个 converters 属性,它将列出要在应用中使用的所有定制 Converter。
Formatter
为了创建 Formatter,需要编写一个实现 org.springframework.format.Format 接口的 Java 类
public interface Formatter<T> extends Printer<t>, Parser<T>{ // T 是输入的字符串要转换成的目标类型
public abstract T parse(String text, Locale locale); // 利用指定的 Locale 将一个 String 解析成目标类型
public abstract String pring(T object, Locale locale); // 与 parse() 相反,返回目标类型的字符串表示法
}
为了在 Spring MVC 应用程序中使用 Formatter,需要利用 conversionService bean 对它进行注册。
bean 的类名称必须为 org.springframework.format.support.FormattingConversionServicefactoryBean。这个 bean 可以用一个 Formatters 属性注册 Formatter,用一个 converters 属性注册 converter。
注册 Formatter 的另一种方法是使用 Registrar。有了 Registrar,就不需要在 Spring MVC 配置文件中注册任何 Formatter 了,只在 Spring 配置文件中注册 Registrar。
Spring MVC 的 Converter 和 Formatter的更多相关文章
- Spring MVC No converter found for return value of type 解决方法
1.在pom中添加 jackson <properties> <jackson.version>2.8.5</jackson.version> </prope ...
- Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器
请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...
- 在Spring MVC 中配置自定义的类型转换器
方法一: 实现spring mvc 自带的 Formatter 接口 1.创建一个类来实现Formatter接口 import org.springframework.format.Formatter ...
- spring笔记2 spring MVC的基础知识2
2,spring MVC的注解驱动控制器,rest风格的支持 作为spring mvc的明星级别的功能,无疑是使得自己的code比较优雅的秘密武器: @RequestMapping处理用户的请求,下面 ...
- Java Spring MVC
Spring MVC的实现包括 实现Controller类和基于注解的Controller RequstMapping方式 依赖: <!-- https://mvnrepository.com/ ...
- spring MVC处理请求过程及配置详解
本文主要梳理下Spring MVC处理http请求的过程,以及配置servlet及业务application需要的常用标签,及其包含的意义. spring MVC处理请求过程 首先看一个整体图 简单说 ...
- Supported method argument types Spring MVC
Supported method argument types The following are the supported method arguments: Request or respons ...
- Spring MVC深入学习
一.MVC思想 MVC思想简介: MVC并不是java所特有的设计思想,也不是Web应用所特有的思想,它是所有面向对象程序设计语言都应该遵守的规范:MVC思想将一个应用部分分成三个基本部 ...
- Spring MVC 使用介绍(十三)数据验证 (一)基本介绍
一.消息处理功能 Spring提供MessageSource接口用于提供消息处理功能: public interface MessageSource { String getMessage(Strin ...
随机推荐
- 1、ZooKeeper 基本概念、使用方法、实践场景
ZooKeeper 基本概念 ZooKeeper 是面向分布式应用的协调服务,其实现了树形结构的数据模型(与文件系统类似),并且提供了简洁的编程原语.ZooKeeper 能够作为基础,用于构建更高层级 ...
- 使用虚拟化KVM安装RHEL7系统
使用虚拟化KVM安装RHEL7系统 作者:Eric 微信:loveoracle11g 检查真实物理机是否支持虚拟化功能 [root@server ~]# grep vmx /proc/cpuinfo ...
- 1DAY centos 7.4 u盘安装、网络安装
0xff01 重庆大学开源下载centos 1.下载地址 http://mirrors.cqu.edu.cn/CentOS/7.4.1708/isos/x86_64/ 选择 CentOS-7-x86 ...
- 论气机之"左升右降"
生命现象源于气机的出入升降运动. “出入废则神机化灭,升降息则气立孤危.故非出入,则无以生长壮老已:非升降,则无以生长化收藏”(<素问·六微旨大论>),升降是气机主要的运动形式之一,是 ...
- php解析excel文件
public static function getStaffByXlsx($path) { /*dirname(__file__): 当前代码所在的目录,$path: ”/文件名“ */ $PHPR ...
- ubuntu vsftp
转自:https://www.cnblogs.com/java-synchronized/p/6935711.html 12点多了,擦!做个码农真不容易呀! 系统:Ubuntu16.04 安装:FTP ...
- 26.python常用端口号
MySQL默认端口 3306 Redis默认端口 6379 MongoDB默认端口 27017 django端口 8000 flask端口 5000 pyspider服务端口 5000(由flask开 ...
- IDEA jrebet插件安装
破解.exe下载 https://github.com/ilanyu/ReverseProxy/releases/tag/v1.0 双击运行,exe 文件, 然后IDEA -> Help -&g ...
- shell脚本运行java程序jar
在UBuntu上部署项目的时候,我们往往通过一段shell来启动程序,甚至是通过crontab定时任务来定时的调用java程序,但是很奇怪的一个问题就是,比如我写了一个如下的shell脚本: #!/b ...
- 【原】The Linux Command Line - Workiing with commands
● type – Indicate how a command name is interpreted● which – Display which executable program will b ...