Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

formatting - How to display a formatted DateTime in Spring MVC 3.0?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...