Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property XXX前端
缘由:个人Controller中须要接收的是Date类型,可是在页面传过来的时间是String类型。前端表单包含时间的字符串传进去,转换成日期时报错了java
解决:在Controller中加入ui
@InitBinder public void initBinder(WebDataBinder binder) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(true); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
或者spa
@InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
参考:.net