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.ConversionService
  a.实现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/>
原创粉丝点击