如何在Spring MVC 3.0中显示格式化的DateTime? [英] How to display a formatted DateTime in Spring MVC 3.0?
问题描述
在我的模型中有一个Joda-DateTime字段,并希望以JSP视图格式显示它。我注释了新的@DateTimeFormat批注:
pre code public class Customer {
private DateTime dateOfBirth;
$ b @DateTimeFormat(style =M-)
public DateTime getDateOfBirth(){
return dateOfBirth;
$ b $ p $现在我想在我的JSP中显示dateOfBirth:
出生日期:$ {customer.dateOfBirth}
$ p $但是结果是没有格式化的:
出生日期:2010-08 -11T11:23:30.148 + 02:00
经过一番调查,我发现,使用spring:bind-tag:
< spring:bind path =customer.dateOfBirth>
出生日期:$ {status.value}
< / spring:bind>
有了这个功能,但是这个简单的任务似乎太复杂了。在列表中使用这种方法更复杂,因为你必须将列表索引整合到绑定路径中。所以我的问题是:是否有一个更简单的方法使用spring格式化注释显示一个格式化的值?
使用spring:eval 使用
code>标签,它理解
@DateTimeFormat
规则:
< spring:eval expression =customer.dateOfBirth/>
或者,您可以删除
@DateTimeFormat
规则从你的模型到所有在一起,并使用Joda的formatDate标签Naikus说明。
在未来的版本中,春计划支持插入SpEL,这是 Spring表达式语言由spring:eval调用,作为JSP页面的默认统一EL实现。这将允许像
$ {customer.dateOfBirth}
这样的内联表达式通过SpEL路由。你可以在这里投票这个改进要求: https://jira.spring.io/browse/SPR -7459 。I have a Joda-DateTime field in my model and want to display it formatted in a JSP view. I have annotated it with the new @DateTimeFormat annotation:
public class Customer { private DateTime dateOfBirth; @DateTimeFormat(style="M-") public DateTime getDateOfBirth() { return dateOfBirth; } }
Now I want to display the dateOfBirth in my JSP:
Date of birth: ${customer.dateOfBirth}
But the result is unformatted:
Date of birth: 2010-08-11T11:23:30.148+02:00
After some research I found out, that I have to use the spring:bind-tag:
<spring:bind path="customer.dateOfBirth"> Date of birth: ${status.value} </spring:bind>
With this it works. But it seems too complicated for this simple task. And using this method on lists is even more complicated since you have to integrate the list-index into the binding-path.
So my question is: Is there a simpler way to display a formatted value using the spring formatting-annotations?
解决方案Use the
spring:eval
tag which understands the@DateTimeFormat
rule:<spring:eval expression="customer.dateOfBirth" />
Alternatively, you can remove the
@DateTimeFormat
rule from your model all-together and use Joda's formatDate tag as Naikus illustrates.In a future release, Spring plans to support plugging in "SpEL", which is the "Spring Expression Language" invoked by spring:eval, as the default Unified EL implementation for your JSP pages. This would allow in-line expressions like
${customer.dateOfBirth}
to be routed through SpEL as well. You can vote up this enhancement request here: https://jira.spring.io/browse/SPR-7459.这篇关于如何在Spring MVC 3.0中显示格式化的DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!