今天在一個基於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));
}