Flo*_*ser 16 formatting spring-mvc
我的模型中有一个Joda-DateTime字段,希望在JSP视图中显示格式.我用新的@DateTimeFormat注释注释了它:
public class Customer {
   private DateTime dateOfBirth;
   @DateTimeFormat(style="M-")
   public DateTime getDateOfBirth() {
      return dateOfBirth;
   }
}
现在我想在JSP中显示dateOfBirth:
Date of birth: ${customer.dateOfBirth}
但结果是未格式化的:
Date of birth: 2010-08-11T11:23:30.148+02:00
经过一些研究后我发现,我必须使用spring:bind-tag:
<spring:bind path="customer.dateOfBirth">
   Date of birth: ${status.value}
</spring:bind>
这有效.但对于这个简单的任务来说似乎太复杂了.在列表上使用此方法甚至更复杂,因为您必须将list-index集成到绑定路径中.
所以我的问题是:是否有一种更简单的方法来使用弹簧格式化注释显示格式化的值?
nai*_*kus 19
因为它是一个joda datetime对象,而不是Date对象,所以您必须编写自己的格式化函数或自定义标记.
如果你可以以某种方式将它转换为java.util.Date,那么你可以使用内置的jstl 的fmt taglibrary.
<fmt:formatDate value="${customer.dateOfBirth}" type="both" 
      pattern="MM-dd-yyyy" />
小智 19
使用spring:eval了解@DateTimeFormat规则的标签:
<spring:eval expression="customer.dateOfBirth" />
或者,您可以将@DateTimeFormat模型中的规则全部移除,并使用Joda的formatDate标记,如Naikus所示.
在未来的版本中,Spring计划支持插入"SpEL",它是由spring:eval调用的"Spring表达式语言",作为JSP页面的默认Unified EL实现.这将允许内联表达式${customer.dateOfBirth}也可以通过SpEL进行路由.您可以在此处投票通知此增强功能请求:https://jira.spring.io/browse/SPR-7459.
| 归档时间: | 
 | 
| 查看次数: | 29542 次 | 
| 最近记录: |