JAVA Spring web mvc 学习 之 6:数据类型转换 -格式转换
来源:互联网 发布:施瓦辛格健身全书淘宝 编辑:程序博客网 时间:2024/06/10 19:13
格式转换器
3种转换方式
1.使用ConversionService转换,该指定是全局性的。
a.定义实现Formatter接口的类
b.在annotation-driven中装配conversion-service 指定转换类
2.使用FormatterRegistrar注册Formatter(适用日期格式)
a.实现DateFormatter接口
b.实现FormatterRegistrar接口
c.在annotation-driven中装配conversion-service 指定 转换注册类
3.使用注解直接转换
关键代码:
1.ConversionServicea.实现Formatter接口
// 实现Formatter<S,T>接口
public class DateFormatter implements Formatter<Date>{ //org.springframework.format.Formatter;
// 日期类型模板:如yyyy-MM-dd
private String datePattern;
// 日期格式化对象
private SimpleDateFormat dateFormat;
// 构造器,通过依赖注入的日期类型创建日期格式化对象
public DateFormatter(String datePattern) {
this.datePattern = datePattern;
this.dateFormat = new SimpleDateFormat(datePattern);
}
// 显示Formatter<T>的T类型对象
@Override
public String print(Date date, Locale locale) {
return dateFormat.format(date);
}
// 解析文本字符串返回一个Formatter<T>的T类型对象。
@Override
public Date parse(String source, Locale locale) throws ParseException {
try {
return dateFormat.parse(source);
} catch (Exception e) {
throw new IllegalArgumentException();
}
}
}
b.配置spring配置
<!-- 装配自定义格式化 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 格式化 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<list>
<bean class="org.fkit.formatter.DateFormatter" c:_0="yyyy-MM-dd"/>
</list>
</property>
</bean>
特别说明:c标识,xmlns:c="http://www.springframework.org/schema/c"
时间格式,可使用org.springframework.format.dateime.DateFormatterl类,只是采用p:pattern="yyyy-MM-dd"
2.使用FormatterRegistrar注册Formatter
a.实现DateFormatter接口
// Formatter<S,T>接口
public class DateFormatter implements Formatter<Date>{
// 日期类型模板:如yyyy-MM-dd
private String datePattern;
// 日期格式化对象
private SimpleDateFormat dateFormat;
// 构造器,通过依赖注入的日期类型创建日期格式化对象
public DateFormatter(String datePattern) {
this.datePattern = datePattern;
this.dateFormat = new SimpleDateFormat(datePattern);
}
// 显示Formatter<T>的T类型对象
@Override
public String print(Date date, Locale locale) {
return dateFormat.format(date);
}
// 解析文本字符串返回一个Formatter<T>的T类型对象。
@Override
public Date parse(String source, Locale locale) throws ParseException {
try {
return dateFormat.parse(source);
} catch (Exception e) {
throw new IllegalArgumentException();
}
}
}
b.实现FormatterRegistrar接口
public class MyFormatterRegistrar implements FormatterRegistrar{
private DateFormatter dateFormatter;
public void setDateFormatter(DateFormatter dateFormatter) {
this.dateFormatter = dateFormatter;
}
@Override
public void registerFormatters(FormatterRegistry registry) {
registry.addFormatter(dateFormatter);
}
}
c.配置spring配置
<!-- 装配自定义格式化 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- DateFormatter bean -->
<bean id="dateFormatter" class="org.fkit.formatter.DateFormatter"
c:_0="yyyy-MM-dd"/>
<!-- 格式化 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
<bean class="org.fkit.formatter.MyFormatterRegistrar"
p:dateFormatter-ref="dateFormatter"/>
</set>
</property>
</bean>
3.使用AnnotationFormatterFactory<A extents Annotation>格式化数据
a.在对象中引入格式注释
// 域对象,实现序列化接口
public class User implements Serializable{
// 日期类型
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
// 正常数字类型
@NumberFormat(style=Style.NUMBER, pattern="#,###")
private int total;
// 百分数类型
@NumberFormat(style=Style.PERCENT)
private double discount;
// 货币类型
@NumberFormat(style=Style.CURRENCY)
private double money;
public User() {
super();
// TODO Auto-generated constructor stub
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "User [birthday=" + birthday + ", total=" + total
+ ", discount=" + discount + ", money=" + money + "]";
}
}
b.配置spring默认装载
<!-- 默认装配 -->
<mvc:annotation-driven/>
阅读全文
0 0
- JAVA Spring web mvc 学习 之 6:数据类型转换 -格式转换
- JAVA Spring web mvc 学习 之 6:数据类型转换
- Spring MVC学习(七)-------SpringMVC数据类型转换
- Spring MVC学习(七)-------SpringMVC数据类型转换
- JAVA日期格式转换与数据类型转换
- 【Spring】详解Spring MVC中不同格式的POST请求参数的数据类型转换过程
- Java学习总结之数据类型转换
- Java之数据类型转换
- Spring3 Web MVC下的数据类型转换
- Spring3 Web MVC下的数据类型转换
- Spring MVC-自定义数据类型转换解决方案
- spring mvc学习心得(一)数据类型转换
- Java中日期数据类型的处理之格式转换
- JAVA Spring web mvc 学习 之 2
- JAVA Spring web mvc 学习 之 1
- JAVA Spring web mvc 学习 之 6:数据验证
- 第四章:数据类型转换——深入浅出学Spring Web MVC
- 第四章:数据类型转换——深入浅出学Spring Web MVC
- 异国之后女友就开始各种消息不回
- 第一课 了解SQL
- 汉诺塔问题及变形HDU 1207、2064、2077
- Android开发初体验之百度地图开发(3)
- Netty私有协议栈踩
- JAVA Spring web mvc 学习 之 6:数据类型转换 -格式转换
- hive mapjoin使用
- HYSBZ 1483 梦幻布丁
- 构造者模式
- 几种从stdin读入字符串的方式
- SVN回滚的正确姿势
- HDOJ1018(斯特林公式)
- 决策树详解(二)
- MATLAB图像处理基础知识1 imhist