在使用SSM框架,页面传输数据时,日期有绑定报错
是因为String类型无法转换为Date类型
以下是我的报错信息:
2020-09-21 16:05:31.079 WARN 6068 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'birth': rejected value [2020-09-12]; codes [typeMismatch.user.birth,typeMismatch.birth,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birth,birth]; arguments []; default message [birth]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birth'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-09-12'; nested exception is java.lang.IllegalArgumentException]]
解决的方法就是:
在实体类中,给Date日期类型添加注解进行格式化
如下:
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
若热部署刷新不行时,重新启动项目