my dog learned polymorphism*
The moose likes JSF and the fly likes JSTL fmt:formatDate and fmt:formatNumber in JSF pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSTL fmt:formatDate and fmt:formatNumber in JSF pages" Watch "JSTL fmt:formatDate and fmt:formatNumber in JSF pages" New topic

JSTL fmt:formatDate and fmt:formatNumber in JSF pages

Wally Hartshorn
Ranch Hand

Joined: Jan 30, 2003
Posts: 77
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.

Wally Hartshorn
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
Wally Hartshorn
Ranch Hand

Joined: Jan 30, 2003
Posts: 77
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 ]
I agree. Here's the link:
subject: JSTL fmt:formatDate and fmt:formatNumber in JSF pages
Similar Threads
h:datatable, how multiply row cell A with row cell B and set value in row cell C
Special Characters '&' and ':'
Using of nested Datatables
Which Package to Import to Use FacesContext's getRequestMap()?
JSF java.util. Concurrent ModificationException