Java springMVC前端傳入字符串、後臺接收Date的錯誤解決

今天在一個基於SSM的項目裏出現以下報錯

Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

報錯內容大概理解,就是前端輸入給後臺的是一個字符串,後臺需要Date類型嘛,這之間的轉換出錯,解決辦法是:

在對應的controller中增加屬性編輯器:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章