- `@NumberFormat` 注解的作用
- 使用示例
负责输入输出数据的格式化工作,如将数据转换成特定的货币类型格式,百分比类型格式等
- pattern:类型 String,使用自定义的数字格式化串,如 ##,### 表示 50,000
- style:类型 NumberFormat.Style,几个常用值
- Style.CURRENCY:货币类型
- Style.NUMBER:正常数字类型
- Style.PERCENT:百分比类型
先写个 index.html,分别测试整数、百分数、货币类型
Spring MVC的数据类型转换
实体类 User,注解就加在该类的实例变量上
public class User {
@NumberFormat(style=Style.NUMBER,pattern="#,###")
private int total;
@NumberFormat(style=Style.PERCENT)
private double discount;
@NumberFormat(style=Style.CURRENCY)
private double money;
}
controller 层
@Controller
public class UserController {
@RequestMapping("/register")
public String register(User user,Model model){
model.addAttribute("user", user);
return "result";
}
}
result.jsp 页面
<%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
测试AnnotationFormatterFactory
测试表单数据格式化
整数类型:
百分数类型:
货币类型:
输入
输出



