Spring mvc @DateTimeFormat无法按预期工作 [英] Spring mvc @DateTimeFormat not working as expected

查看:341
本文介绍了Spring mvc @DateTimeFormat无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在jsp中制作的简单页面中添加了一个jQuery日期选择器。使用Spring mvc 4.0.0。我想在我的模型类中有java.util.Date字段,让spring将日期String从前端转换为日期。我的问题是,如果我有

I just added a jQuery date picker to my simple page made in jsp. Using Spring mvc 4.0.0. I would like to have java.util.Date field in my model class and let spring to convert the date String coming from the front end to date. My issue is that, if i have

 @DateTimeFormat(pattern = "dd/MM/yy")
 private Date startDate;

在我的模型类中,我在spring控制器中收到null值。 (startDate setter既不被调用)如果我将startDate更改为一个简单的String,那么正确填充该值并且在控制器中我能够检索startDate字符串。你能告诉我我错过了什么吗?

In my model class I receive null value in my spring controller. (The startDate setter is not called neither) If i change the startDate to be a simple String, than the value is populated correctly and in the controller i am able to retrieve the startDate string. Could you please advice what am I missing?

试图按照 http://gerrydevstory.com/2013/05/21/binding-date-form-input-on-spring-mvc/ 似乎没有其他技巧用于将String转换为Date。

Was trying to follow the http://gerrydevstory.com/2013/05/21/binding-date-form-input-on-spring-mvc/ seems that no other tricks has been used to convert the String to Date.

推荐答案

我认为你应该将@DateTimeFormat置于@RequestMapping之下,如下所示:

I think you shoule put @DateTimeFormat under the @RequestMapping, like below:

@RequestMapping
public String someMethod(@RequestParam(value =date,required = false)@DateTimeFormat(pattern =dd / MM / YY)date date)

这篇关于Spring mvc @DateTimeFormat无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆