1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <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>
|
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class User implements Serializable { private static final long serialVersionUID = 1L; @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(); } @Override public String toString() { return "User [birthday=" + birthday + ", total=" + total + ", discount=" + discount + ", money=" + money + "]"; } }
|
User
类的多个属性使用了@DateTimeFormat
和@NumberFormat
注解,用于将页面传递的String
转换成对应的格式化数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Controller public class FormatterController { @GetMapping(value = "/testForm") public String registerForm() { return "testForm"; } @PostMapping(value = "/test") public String test(@ModelAttribute User user, Model model) { System.out.println(user); model.addAttribute("user", user); return "success"; } }
|
success.jsp
如果希望在视图页面中将模型属性数据以格式化的方式进行渲染,则需要使用Spring
的页面标签显示模型数据。所以form:form
标签,并且绑定了User
对象.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> <!DOCTYPE html> <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>
|
springmvc-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?> <beans ... http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="org.fkit.controller" /> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".jsp" /> </beans>
|
在配置文件中只是使用了默认装配方案: mvc:annotation-driven
标签,而该标签内部默认创建的 Conversionservice
实例就是一个FormattingConversionServiceFactoryBean
,这样就可以支持注解驱动的格式化功能了,不需要过多的配置.
测试
填写表单
转换效果
控制台输出
1
| User [birthday=Tue Mar 04 00:00:00 CST 1253, total=155123, discount=0.15, money=123.0]
|
原文链接: 6.3 数据格式化 3.使用AnnotationFormatterFactory格式化数据