运行后tomcat显示报错:
21-Sep-2022 09:20:47.318 警告 [http-nio-8080-exec-5] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘user’ on field ‘birthday’: rejected value [2001-10-10]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘birthday’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value ‘2001-10-10’; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2001-10-10]]]
21-Sep-2022 09:20:58.122 警告 [http-nio-8080-exec-7] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘user’ on field ‘birthday’: rejected value [2001/10/10]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘birthday’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value ‘2001/10/10’; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2001/10/10]]]
在ssm学习过程中,有时候要用到输入日期类型
因为当你输入日期时候,需要用到类型的转换,这里你可以自定义一个日期时间类型,也可以使用maven中自带的
1.方法类中对定义的日期变量上添加日期转化
2.当配置了类型转换器,需要将实现类改为:FormattingConversionServiceFactoryBean 才可以同时支持自定义类型转换器和格式化,xml中加入
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
</bean>
即可解决。