Spring表單提交日期類型報錯,不進後臺Controller層,ailed to convert from type java.lang.String to type java.util.Date

    問題原因,前臺表單提交到了Date格式的數據,後臺編碼時不能識別該日期格式,故而無法進入到Controller就被攔截!

解決方法,在需要進行轉換的Controller層加入如下代碼,進行初始化轉換。

    /* 
* 表單提交日期綁定 
*/  
@InitBinder  
public void initBinder(WebDataBinder binder) {  
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
   dateFormat.setLenient(false);  
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  

}  

此時就可以獲取到前臺傳來的日期類型的數據了。

發佈了28 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章