创建实体类
在每一个字段上面添加对应格式化的注解
package com.alin.AnnotationFormatter;
import java.io.Serializable;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
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 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;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
创建控制器
package com.alin.AnnotationFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FormatterController {
@RequestMapping(value="/Form")
public String registerForm(){
return "Formatter/Form";
}
@RequestMapping(value = "/formatter")
public String formatter(@ModelAttribute User user, Model model) {
model.addAttribute("user",user);
return "Formatter/success";
}
}
Form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>格式化</title>
</head>
<body>
<form:form method="post" action="formatter">
<span>日期类型</span>
<input type="text" id="birthday" name="birthday"/><br/>
<span>整数类型</span>
<input type="text" id="total" name="total"/><br/>
<span>百分数类型</span>
<input type="text" id="discount" name="discount"/><br/>
<span>货币类型</span>
<input type="text" id="money" name="money"/>
<input id="submit" type="submit" value="提交"/>
</form:form>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>格式后数据</title>
</head>
<body>
<form:form modelAttribute="user" method="post" action="">
<span>日期类型</span>
<form:input path="birthday" /><br/>
<span>整数类型</span>
<form:input path="total" /><br/>
<span>百分数类型</span>
<form:input path="discount" /><br/>
<span>货币类型</span>
<form:input path="money" />
</form:form>
</body>
</html>