在SpringMVC开发中,使用JavaBean来接受参数,bean中有日期类型,而客户端会采用格式化后的日期进行传递,例如: <br> 若是不作处理直接传递到后台会出现:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' 异常,很明显是以字符串传递到服务器,而服务器须要的日期类型,形成转换错误的问题。<br> 想要解决这个问题只须要在controller处理类中添加一个initBinder()的处理方法便可。<br>java
@InitBinder public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }
这样就能够日期类型和JavaBean里面的日期类型自动转换了。服务器