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