Is it possible in JSF to convert a date value and put it in “title” attribute? In a similar question, JSF Convert dates for title attribute, there was an answer, that it can be done with JSTL’s fmt:formatDate, but not in repeating components, such as UIData. I need to do it inside a table (extended HtmlDataTable).
For example, the following code correctly displays the date as text value, but not in the title attribute:
<h:outputText class="yui-tip" title="#{task[col.attributeName]}" value="#{task[col.attributeName]}">
<f:convertDateTime type="both" dateStyle="medium" timeStyle="short" timeZone="#{userProfileBean.clientTimeZone}" />
</h:outputText>
The
<f:convertDateTime>only converts thevalueattribute, not other attributes. In this particular case, your best bet is to create a custom EL function for that.First create a
finalclass with apublic staticmethod which takes the necessary arguments and delegates to the JSFDateTimeConverter(package/class/method name is free to your choice):Define it as a
facelet-taglibin/META-INF/functions.taglib.xml(filename is free to choice):(note: for Facelets 2.x, you need a XSD instead of a DTD; for an example see this answer)
Register it as new taglib in
/WEB-INF/web.xml:(note: if you already have the
facelets.LIBRARIESdefinied, then you can just add the new path commaseparated; for Facelets 2.x, you needjavax.faces.FACELETS_LIBRARIEScontext param instead)Declare it in the Facelets XHTML file as new XML namespace:
Finally you can use it as intended:
You can if necessary hardcode the type and styles in the function and give the method a different name which indicates those defaults.
If you happen to use JSF utility library OmniFaces, then you can also use its
#{of:formatDate()}function instead.