代码之家  ›  专栏  ›  技术社区  ›  Florian Fankhauser

如何在springmvc3.0中显示格式化的DateTime?

  •  16
  • Florian Fankhauser  · 技术社区  · 14 年前

    public class Customer {
       private DateTime dateOfBirth;
    
       @DateTimeFormat(style="M-")
       public DateTime getDateOfBirth() {
          return dateOfBirth;
       }
    }
    

    现在我想在JSP中显示出生日期:

    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>
    

    所以我的问题是:有没有一种更简单的方法使用spring格式注释来显示格式化的值?

    2 回复  |  直到 11 年前
        1
  •  19
  •   tk_ Collin Price    7 年前

    使用 spring:eval @DateTimeFormat 规则:

    <spring:eval expression="customer.dateOfBirth" />
    

    @日期时间格式

    ${customer.dateOfBirth} 也要通过SpEL。您可以在此处投票支持此增强请求: https://jira.spring.io/browse/SPR-7459 .

        2
  •  19
  •   naikus    14 年前

    因为它是joda datetime对象,而不是Date对象,所以您必须编写自己的格式化函数或自定义标记。

    fmt

    <fmt:formatDate value="${customer.dateOfBirth}" type="both" 
          pattern="MM-dd-yyyy" />
    

    但是最新的jsp允许您快速创建 custom tags .