• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JSTL fmt:formatDate and fmt:formatNumber in JSF pages

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 28472
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
And then the flying monkeys attacked. My only defense was this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic