Spring Framework 3.0發布了。這里我們介紹其中的一個:用於格式化的注解。
簡介
Spring 3 提供了兩個可以用於格式化數字、日期和時間的注解@NumberFormat和@DateTimeFormat,這兩個標簽可以用於bean的屬性或方法參數上。@NumberFormat可以用來格式化任何的數字的基本類型(如int,long)或java.lang.Number的實例(如 BigDecimal, Integer)。@DateTimeFormat可以用來格式化java.util.Date、java.util.Calendar和 java.util.Long類型,也可以用於Joda Time類型的字段或參數。(Joda Time是一個開源的包,提供了對date和time類的一些替代類)。
要指定數字或日期/時間類型的屬性,只需要在其上添加 @NumberFormat或@DateTimeFormat注解接可以了。例如下面的代碼:
-
import java.math.BigDecimal; import java.util.Calendar; import java.util.Date; import org.joda.time.LocalTime; public class Employee { private String name; private double salary; private double w4AdditionalWithdraw; private int dependents; private BigDecimal visualAcuity; private Date birthDate; private Calendar hireDate; private LocalTime startTime; private long lastTimeEntry; /** * initialization block to provide sample data for display */ { this.name = "John Doe"; this.salary = 30100.50; this.w4AdditionalWithdraw = 0.02; this.dependents = 5; this.visualAcuity = new BigDecimal(".1"); Calendar dob = Calendar.getInstance(); dob.set(1964, Calendar.AUGUST, 30); this.birthDate = dob.getTime(); this.hireDate = Calendar.getInstance(); this.startTime = new LocalTime(8, 0); this.lastTimeEntry = (new Date()).getTime() - 10000; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double getW4AdditionalWithdraw() { return w4AdditionalWithdraw; } public void setW4AdditionalWithdraw(double w4AdditionalWithdraw) { this.w4AdditionalWithdraw = w4AdditionalWithdraw; } public int getDependents() { return dependents; } public void setDependents(int dependents) { this.dependents = dependents; } public BigDecimal getVisualAcuity() { return visualAcuity; } public void setVisualAcuity(BigDecimal visualAcuity) { this.visualAcuity = visualAcuity; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public LocalTime getStartTime() { return startTime; } public void setStartTime(LocalTime startTime) { this.startTime = startTime; } public Calendar getHireDate() { return hireDate; } public void setHireDate(Calendar hireDate) { this.hireDate = hireDate; } public long getLastTimeEntry() { return lastTimeEntry; } public void setLastTimeEntry(long lastTimeEntry) { this.lastTimeEntry = lastTimeEntry; } } 復制代碼
這段代碼中我們沒有添加注解。如果我們使用Spring 份額Form 標簽將這個bean中的數據用於顯示的時候,我們會得到類似下圖的結果:
可以看到,所有的數據都是以默認的格式顯示的,Spring會使用默認的格式將數據顯示在HTML表格中(其他的框架也大多如此)。如果想顯示特殊的格式,開發人員必須編寫特殊的getter/setter方法或使用PropertyEditors來顯示特殊格式的數字或者日期/時間。
現在有了 @NumberFormat和@DateTimeFormat注解,我們可以使用這兩個注解來完成同樣的功能。如下面的代碼所示:
-
import java.math.BigDecimal; import java.util.Calendar; import java.util.Date; import org.joda.time.LocalTime; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.NumberFormat; import org.springframework.format.annotation.DateTimeFormat.ISO; import org.springframework.format.annotation.NumberFormat.Style; public class Employee { private String name; /** * numeric fields using @NumberFormat annotation for formatting. */ @NumberFormat(style = Style.CURRENCY) private double salary; @NumberFormat(style = Style.PERCENT) private double w4AdditionalWithdraw; @NumberFormat private int dependents; @NumberFormat(pattern = "0.00") private BigDecimal visualAcuity; /** * date and time fields using @DateTimeFormat annotation for formatting. */ @DateTimeFormat(style = "M-") private Date birthDate; @DateTimeFormat(pattern = "w:yyyy") private Calendar hireDate; @DateTimeFormat(style = "-S") private LocalTime startTime; @DateTimeFormat(iso = ISO.DATE_TIME) private long lastTimeEntry; /** * initialization block to provide sample data for display */ { this.name = "John Doe"; this.salary = 30100.50; this.w4AdditionalWithdraw = 0.02; this.dependents = 5; this.visualAcuity = new BigDecimal(".1"); Calendar dob = Calendar.getInstance(); dob.set(1964, Calendar.AUGUST, 30); this.birthDate = dob.getTime(); this.hireDate = Calendar.getInstance(); this.startTime = new LocalTime(8, 0); this.lastTimeEntry = (new Date()).getTime() - 10000; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double getW4AdditionalWithdraw() { return w4AdditionalWithdraw; } public void setW4AdditionalWithdraw(double w4AdditionalWithdraw) { this.w4AdditionalWithdraw = w4AdditionalWithdraw; } public int getDependents() { return dependents; } public void setDependents(int dependents) { this.dependents = dependents; } public BigDecimal getVisualAcuity() { return visualAcuity; } public void setVisualAcuity(BigDecimal visualAcuity) { this.visualAcuity = visualAcuity; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public LocalTime getStartTime() { return startTime; } public void setStartTime(LocalTime startTime) { this.startTime = startTime; } public Calendar getHireDate() { return hireDate; } public void setHireDate(Calendar hireDate) { this.hireDate = hireDate; } public long getLastTimeEntry() { return lastTimeEntry; } public void setLastTimeEntry(long lastTimeEntry) { this.lastTimeEntry = lastTimeEntry; } }
當在頁面上顯示的時候,我們會直接得到我們想要的格式,如下圖所示:
注解的可選屬性
注解的屬性決定了@NumberFormat和@DateTimeFormat注解怎樣格式化相應的屬性。 @NumberFormat注解有兩個可選的屬性:style和pattern。style屬性是一個NumberFormat.Style枚舉值,可以是以下的三個值之一:
NumberFormat.Style 枚舉值 | 是否缺省值 |
NUMBER | 是 |
CURRENCY | 否 |
PERCENT | 否 |
這 3種style的應用我們在上面的代碼中都有相應的例子。
具體的style的表現形式是與區域相關的。例如,一個double類型的字段,如果style是CURRENCY,那么在en-us的區域顯示的時候前面會加上$,在zh-cn的區域顯示的時候前面會加上¥。
如果以上的3中方式無法滿足需求,我們可以使用pattern屬性來指定特殊的輸出格式。Pattern的值要遵循Java標准的numeric formatting pattern。對於@NumberFormat來說缺省的是沒有pattern的。
@DateTimeFormat 注解有3個可選的屬性:style,pattern和iso。屬性style允許我們使用兩個字符的字符串來表明怎樣格式化日期和時間。第一個字符表明了日期的格式,第二個字符表明了時間的格式。下面的表格中列出了可用的選擇以及相應的輸出的例子:
描述 | 字符串值 | 示例輸出 |
短格式(這是缺省值) | SS | 8/30/64 11:24 AM |
中等格式 | MM | Aug 30, 1964 11:24:41 AM |
長格式 | LL | August 30, 1964 11:24:41 AM CDT |
完整格式 | FF | Sunday, August 30, 1964 11:24:41 AM CDT |
使用短橫線省略日期或時間 | M- | Aug 30, 1964 |
Pattern 屬性允許我們使用自定義的日期/時間格式。該屬性的值遵循java標准的date/time格式規范。缺省的該屬性的值為空,也就是不進行特殊的格式化。
最后,可以使用org.springframework.format.annotation.DateTimeFormat.ISO枚舉值來使用ISO標准的日期/時間格式來格式化。下面的表格中列出了可能的值和相應的輸出
ISO枚舉值 | 輸出 |
DATE | 2000-10-31 |
TIME | 01:30:00.000-05:00(最后的是時區) |
DATE_TIME | 2000-10-31 01:30:00.000-05:00. |
NONE | 不進行ISO標准的格式化 |
使用格式化注解的設置
要使用上面介紹的格式化注解,需要進行如下的配置:
1.下載Spring 3.0的jar包並加入到類路徑中。
2. 將下面的配置信息加入到spring bean的XML配置文件中。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven /> </beans>
解析參數
我們也可以使用這些注解來告訴Spring怎樣解析輸入的數據。下面的例子顯示了怎樣使用@DateTimeFormat來解析以ISO格式輸入的時間信息:
public String getEmployeesOnTheClock(@DateTimeFormat(iso=ISO.TIME) java.util.Date time) { ... }