SpringMVC使用@DateTimeFormat、@NumberFormat註解對資料進行格式化
阿新 • • 發佈:2019-02-19
JSP頁面:
<form action="testFormat" method="post">
<input type="text" name="date">
<input type="submit" value="資料格式化">
</form>
後臺方法:
@RequestMapping("/testFormat")
public String testFormat(Address address){
System.out.println(address);
return "success";
}
實體類:
//時間 @DateTimeFormat(pattern="yyyy-MM-dd") private Date date; //浮點數 @NumberFormat(pattern="#,###,###.#") private Float ind;
jsp頁面輸入:2017-1-1,後臺的實體類可以把它轉換成Date型別。
springmvc配置檔案需要加入:
<mvc:annotation-driven></mvc:annotation-driven>
經過測試,發現要讓註解起效,那麼在mvc:annotation-driven這個標籤裡面就不能在加其它屬性,這裡只測試了自定義型別轉換,發現用了自定義型別轉換後,那麼對資料進行格式的註解就用不了了。