SpringMvc中Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'的错误

spring mvc出现 Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错:

19:41:01,234  WARN DefaultHandlerExceptionResolver:354 - Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException

解决方法:

在相关的Controller类中添加

@InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
	
发布了183 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/SEVENY_/article/details/90757656