【Spring MVC】使用Formatter格式化数据

Formatter(格式化程序)是spring3.0开始引入的格式化转换框架,它位于org.spring.framework.format包中,其中比较重要的是Formatter接口。

Formatter可以完成任意Object与String之间的类型转换,但它只能将String转换成另一种Java类型。例如将String转换成Date,但不能将Long转换成Date。因此更使用于Web层的数据转换。

使用示例:使用Fromatter将String转换成Date

1、实现Formatter接口

package com.tcx.formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
/**
实现Formatter<T>接口, T是目标对象的类型
*/
public class DateFormatter implements Formatter<Date>{
	//日期格式化对象
	private SimpleDateFormat dateFormat;
	//通过依赖注入日期类型创建对象,datePattern="yyyy-MM-dd"
	public DateFormatter(String datePattern) {
		this.dateFormat = new SimpleDateFormat(datePattern);
	}
	//返回T类型的字符串表示形式
	@Override
	public String print(Date date, Locale locale) {
		return dateFormat.format(date); 
	}
	//通过指定的Locale将String解析成T类型对象
	@Override
	public Date parse(String source, Locale locale) throws ParseException {
		return dateFormat.parse(source);
	}
}

2、xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
						http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.3.xsd">
		
		<!-- 开启注解扫描 -->		
		<context:component-scan base-package="com.tcx.controller"/>
		<!-- 静态资源处理 -->
		<mvc:default-servlet-handler/>
		
		<!-- 自定义格式化转换器 -->
		<mvc:annotation-driven conversion-service="conversionService" />
		<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
			<property name="formatters">
				<list>
					<bean class="com.tcx.formatter.DateFormatter" c:_0="yyyy-MM-dd"/>
				</list>
			</property>
		</bean>
		
		<!-- 配置视图解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
			 <property name="prefix" value="/WEB-INF/content/" />
			 <property name="suffix" value=".jsp" />
		</bean>	
</beans>

3、测试
1)测试环境:浏览器-FireFox,Tomcat8.5,jdk1.8,Wins10
2)测试数据:姓名Mike和特定格式表示的生日,测试String to Date的数据转换过程。
在这里插入图片描述
3)测试结果:"2000-01-23"成功转换成日期类型。
在这里插入图片描述

Thanks♪(・ω・)ノ

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西西小道

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值