java培训之数据的格式化

     1 数据格式化概述

  • 对属性对象的输入/输出进行格式化,从其本质上讲依然属于 “类型转换” 的范畴。
  • Spring 在格式化模块中定义了一个实现 ConversionService 接口的

         FormattingConversionService 实现类,该实现类扩展了 GenericConversionService,         因此它既具有类型转换的功能,又具有格式化的功能

  • FormattingConversionService 拥有一个 FormattingConversionServiceFactoryBean 工厂类,后者用于在 Spring 上下文中构造前者,FormattingConversionServiceFactoryBean 内部已经注册了 :
    • NumberFormatAnnotationFormatterFactory:支持对数字类型的属性使用

        @NumberFormat 注解

  • JodaDateTimeFormatAnnotationFormatterFactory:支持对日期类型的属性使用

        @DateTimeFormat 注解

  • 装配了 FormattingConversionServiceFactroyBean 后,就可以在 Spring MVC 入参绑定及模型数据输出时使用注解驱动了。
    • <mvc:annotation-driven/> 默认创建的 ConversionService 实例即为

          DefaultFormattingConversionService

     2日期格式化概述

  • @DateTimeFormat 注解可对util.Date、java.util.Calendar、java.long.Long 时间类型进行标注:
  • pattern 属性:类型为字符串。指定解析/格式化字段数据的模式,如:”yyyy-MM-dd hh:mm:ss”
  • iso 属性:类型为ISO。指定解析/格式化字段数据的ISO模式,包括四种:ISO.NONE(不使用) -- 默认、ISO.DATE(yyyy-MM-dd) 、ISO.TIME(hh:mm:ss.SSSZ)、  ISO.DATE_TIME(yyyy-MM-dd hh:mm:ss.SSSZ)
  • style 属性:字符串类型。通过样式指定日期时间的格式,由两位字符组成,第一位表示日期的格式,第二位表示时间的格式:S:短日期/时间格式、M:中日期/时间格式、L:长日期/时间格式、F:完整日期/时间格式、-:忽略日期或时间格式

      3数值格式化概述

  • @NumberFormat 可对类似数字类型的属性进行标注,它拥有两个互斥的属性:
    • style:类型为Style。用于指定样式类型,包括三种:Style.NUMBER(正常数字类型)、 Style.CURRENCY(货币类型)、 Style.PERCENT(百分数类型)
    • pattern:类型为 String,自定义样式,如pattern="#,###";

<!-- 声明类型转换器服务 -->

<!-- <bean id="conversionService"

 class="org.springframework.context.support.ConversionServiceFactoryBean"> -->

<bean id="conversionService"

 class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

<property name="converters">

<set>

<!-- 引用类型转换器 -->

<ref bean="stringToEmployeeConverter"/>

</set>                        

</property>

</bean>

      4实验代码(格式化日期)

  • 页面表单

 <!-- 解决问题:

                 1.数据类型转换

                 2.数据格式

                 3.数据校验                 

                 自定义类型转换器:

                         将字符串转换为Employee对象,完成添加功能

 -->

BirthDay :<input type="text" name="birthDay"/><br><br>

  • Employee类增加日期对象属性

//关于类型转换

private Date birthDay ;

  • 关于格式错误(框架默认支持的格式为斜线方式。1990/09/09)

在页面上设置格式为:1990-09-09

报错:

Java培训

  • 解决400错误:

在Employee类的日期属性上增加

@DateTimeFormat(pattern="yyyy-MM-dd")

private Date birthDay ;

  • 配置,配置时不能指定conversion-service属性,否则,依然报错400。

FormattingConversionServiceFactoryBean替换ConversionServiceFactoryBean后再进行引用。

<mvc:annotation-driven conversion-service="conversionService"/>

<mvc:annotation-driven />

    5 实验代码(格式化数字)

Salary : <form:input path="salary"/>

 

@NumberFormat(pattern="#,###,###.#")

private double salary ;

   6获取错误消息

(后台获取错误消息,并打印)

//添加员工

@RequestMapping(value="/empAdd",method=RequestMethod.POST)

public String empAdd(Employee employee,BindingResult bindingResult){

System.out.println("empAdd - employee="+employee);

 

if(bindingResult.getErrorCount() > 0 ){

System.out.println("类型转换处错误了");

List<FieldError> fieldErrors = bindingResult.getFieldErrors();

for(FieldError fieldError : fieldErrors){

System.out.println(fieldError.getField() + " - " + fieldError.getDefaultMessage());

}

}

 

employeeDao.save(employee);

return "redirect:/empList";

}

 

类型转换出错误了

birthDay - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthDay'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value 's'; nested exception is java.lang.IllegalArgumentException: Unable to parse 's'

salary - Failed to convert property value of type 'java.lang.String' to required type 'double' for property 'salary'; nested exception is java.lang.NumberFormatException: For input string: "ss"

想要了解跟多关于Java培训课程内容欢迎关注尚硅谷Java培训,尚硅谷除了这些技术文章外还有免费的高质量Java培训课程视频供广大学员下载学习。