How to display a formatted DateTime in Spring MVC 3.0?

后端 未结 2 623
我寻月下人不归
我寻月下人不归 2020-12-14 19:05

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          


        
相关标签:
2条回答
  • 2020-12-14 19:46

    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.

    0 讨论(0)
  • 2020-12-14 20:00

    Because its a joda datetime object, and not a Date object, you will have to write your own formatting function or a custom tag.

    If you can convert it to java.util.Date somehow, then you can use the built in jstl's fmt taglibrary.

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

    But the latest jsp quickly allows you to create custom tags.

    0 讨论(0)
提交回复
热议问题