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));
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM