Spring验证:无法从字符串转换为日期
我正在做一些春季表单验证,但是我得到:
Failed to convert property value of type 'java.lang.String' to required type 'ja
va.util.Date' for property 'birthdate'; nested exception is java.lang.Illega
lStateException: Cannot convert value of type [java.lang.String] to required typ
e [java.util.Date] for property 'birthdate': no matching editors or conversi
on strategy found
但是,在我的 modelAttribute 表单中,我有:
@NotNull
@Past
@DateTimeFormat(style="S-")
private Date birthdate;
我认为 DateTimeFormat 对此负责?
我正在使用 hibernate-validator 4.0。
I'm doing some spring form validation, however I'm getting:
Failed to convert property value of type 'java.lang.String' to required type 'ja
va.util.Date' for property 'birthdate'; nested exception is java.lang.Illega
lStateException: Cannot convert value of type [java.lang.String] to required typ
e [java.util.Date] for property 'birthdate': no matching editors or conversi
on strategy found
However, in my modelAttribute form I have:
@NotNull
@Past
@DateTimeFormat(style="S-")
private Date birthdate;
I thought the DateTimeFormat was responsible for this?
I'm using the hibernate-validator 4.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有可能必须在控制器中使用注册
CustomDateEditor
来将字符串转换为日期。下面的示例方法适用于您的控制器,但您必须更改日期格式以匹配您正在使用的任何格式。Theres a chance you'll have to use register a
CustomDateEditor
in your controller(s) to convert from a String to a Date. The example method below goes in your controller, but you'll have to change the date format to match whatever you're using.为了使用
@DateTimeFormat
,您需要安装FormattingConversionServiceFactoryBean
。
隐式执行此操作,但如果您无法使用它,则需要如下所示:In order to use
@DateTimeFormat
you need to installFormattingConversionServiceFactoryBean
.<mvc:annotation-driven>
does it implicitly, but if you cannot use it you need something like this: