SpringMVC——注解驱动的属性格式化

一、什么是注解驱动的属性格式化?

在bean的属性中设置,SpringMVC处理 方法参数绑定数据、模型数据输出时自动通过注解应用格式化的功能。

二、注解方式进行属性格式化

@DateTimeFormat,互斥属性

  • iso。类型为DateTimeFormat.ISO
    • DateTimeFormat.ISO.DATE: 格式yyyy-MM-dd
    • DateTimeFormat.ISO.DATE_TIME: 格式yyyy-MM-dd HH:mm:ss .SSSZ
    • DateTimeFormat.ISO.TIME: 格式HH:mm:ss .SSSZ
    • DateTimeFormat.ISO.NONE: 表示不使用ISO格式的时间。
  • pattern。类型为String,使用自定义的时间格式化字符串。
  • style。类型为String,通过样式指定日期时间的格式,由两位字符组成,第1位表示日期的样式,第2位表示时间的格式:
    • S: 短日期/时间的样式;
    • M: 中日期/时间的样式;
    • L: 长日期/时间的样式;
    • F: 完整日期/时间的样式;
    • -: 忽略日期/时间的样式;

@NumberFormat

  • pattern。类型为String,使用自定义的数字格式化字符串,"##,###.##"。
  • style。类型为NumberFormat.Style,常用值:
    • Style.NUMBER正常数字类型
    • Style.PERCENT百分数类型
    • Style.CURRENCY 货币类型

三、格式化测试

因为在SpringMVC配置文件中<mvc:annotation-driven/>标签内部默认创建的ConversionService实例就是一个FormattingConversionServiceFactoryBean,这样就可以支持注解驱动的格式化功能了

 1、在实体类中的属性上添加格式化注解

// 域对象,实现序列化接口
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;  
    
    get和set方法      
}

2、Controller层

@Controller
public class FormatterController{
    
     @RequestMapping(value="/{pageName}")
     public String loginForm(@PathVariable String pageName){   
        // 动态跳转页面
        return formName;
     }
     
     @RequestMapping(value="/test",method=RequestMethod.POST)
     public String test(@ModelAttribute("user") User user,Model model) {
       System.out.println(user);
         model.addAttribute("user", user);
         return "success";
     }
}

3、编写填写表单页面和成功页面

testForm.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试AnnotationFormatterFactory 接口</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form action="test" method="post">
     <table>
          <tr>
            <td><label>日期类型: </label></td>
             <td><input type="text" id="birthday" name="birthday" ></td>
         </tr>
         <tr>
            <td><label>整数类型: </label></td>
             <td><input type="text" id="total" name="total" ></td>
         </tr>
         <tr>
            <td><label>百分数类型: </label></td>
             <td><input type="text" id="discount" name="discount" ></td>
         </tr>
         <tr>
            <td><label>货币类型: </label></td>
             <td><input type="text" id="money" name="money" ></td>
         </tr>
         <tr>
             <td><input id="submit" type="submit" value="提交"></td>
         </tr>
     </table>
</form>
</body>
</html>

success.jsp(使用spring的form标签一个格式化的方式进行渲染数据)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试AnnotationFormatterFactory</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form:form modelAttribute="user" method="post" action="" >
<table>
    <tr>
        <td>日期类型:</td>
        <td><form:input path="birthday"/></td>
    </tr>
    <tr>
        <td>整数类型:</td>
        <td><form:input path="total"/></td>
    </tr>
    <tr>
        <td>百分数类型:</td>
        <td><form:input path="discount"/></td>
    </tr>
    <tr>
        <td>货币类型:</td>
        <td><form:input path="money"/></td>
    </tr>
</table>
</form:form>
</body>
</html>

4、测试结果

填写数据:

显示数据:

 

posted @ 2020-11-19 20:45  Arbitrary233  阅读(246)  评论(0编辑  收藏  举报