错误信息:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'student' on field 'create_date': rejected value [2018-10-13]; codes [typeMismatch.student.create_date,typeMismatch.create_date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student.create_date,create_date]; arguments []; default message [create_date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'create_date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2018-10-13'; nested exception is java.lang.IllegalArgumentException]
错误原因:
spring框架数据验证失败,前端页面发送过来的日期类型数据为String型,而数据库中接收的类型为datetime型,可能就会报以下错误。
解决方法:在控制器中加上该方法,问题搞定
//当前端页面传过来的的String类型的日期与后台实体类的Date类型不匹配时,需要加上该方法
@InitBinder
public void init(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}