Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL fmt:formatDate and fmt:formatNumber in JSF pages

 
Wally Hartshorn
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read various messages and articles that discourage the use of JSTL in JSF pages, and I think I understand the reasoning behind that. However, I'm hoping that there is a better way to format dates and numbers for display than just returning the formatted String value from the managed bean.

If I have a person bean, with properties hireDate and salary, I would like person.getHireDate() to return a Date and person.getSalary() to return a float. However, when I'm displaying those properties in a web page, I need them to be formatted into a useful format.

In the JSTL world, I could use <fmt:formatDate> and <fmt:formatNumber>. However, I'm having trouble figuring out how to access those from within a JSF <h:dataTable>.

I supposed I could add person.getFormattedHireDate() and person.getFormattedSalary() to my bean, but that feels wrong (particularly if these values will need to be formatted in different ways at different places).

Here's a (non-working) code snippet:



Is there a good way to do this?

I'm using the latest versions of JSP, JSTL, and JSF.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18091
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problems with JSTL have to do with nested constructs (especially iterators) that interfere with each other, and you "should" be able to use the JSTL formatters with relative impunity. Work is in progress to make JSTL and JSF play nicer together.

You might want to check out Facelets. I'm almost totally ignorant on Facelets, but as I understand it, they're oriented towards the presentation layer, so maybe there's something useful for you there.
 
Wally Hartshorn
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I was making this way too difficult. It turns out all I needed to do was read one more chapter in the book and I would have discovered that JSF's <f:convertDateTime> and <f:convertNumber> do exactly what I want, without any need to fiddle with JSTL's <fmt:formatDate> and <fmt:formatNumber>. Here's the solution:



(Stupid smilies....)
[ December 13, 2005: Message edited by: Wally Hartshorn ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic