Convert dates to JSF for attribute "title" in table

Is it possible in JSF to convert a date value and put it in the title attribute? In a similar JSF question to convert dates for the title attribute , there was an answer that this can be done using JSTL fmt:formatDate , but not in duplicate components like UIData . I need to do this inside a table (extended HtmlDataTable).

For example, the following code correctly displays the date as a 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> 
+4
source share
1 answer

<f:convertDateTime> converts only the value attribute, not the other attributes. In this particular case, it is best to create a custom EL function for this.

First, create the final class using the public static method, which takes the necessary arguments and delegates in the JSF DateTimeConverter (the package / class / method name is optional):

 package com.example.util; import java.util.Date; import java.util.TimeZone; import javax.faces.component.UIOutput; import javax.faces.context.FacesContext; import javax.faces.convert.DateTimeConverter; public final class Functions { private Functions() { // Hide constructor. } public static String convertDateTime(Date date, String type, String dateStyle, String timeStyle, TimeZone timeZone) { DateTimeConverter converter = new DateTimeConverter(); converter.setType(type); converter.setDateStyle(dateStyle); converter.setTimeStyle(timeStyle); converter.setTimeZone(timeZone); return converter.getAsString(FacesContext.getCurrentInstance(), new UIOutput(), date); } } 

Define it as facelet-taglib in /META-INF/functions.taglib.xml (the file name is free of choice):

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> <facelet-taglib> <namespace>http://example.com/util/functions</namespace> <function> <function-name>convertDateTime</function-name> <function-class>com.example.util.Functions</function-class> <function-signature>java.lang.String convertDateTime(java.util.Date, java.lang.String, java.lang.String, java.lang.String, java.util.TimeZone)</function-signature> </function> </facelet-taglib> 

(note: for Facelets 2.x you need XSD instead of DTD, for an example see this answer )

Register it as the new taglib in /WEB-INF/web.xml :

 <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/META-INF/functions.taglib.xml</param-value> </context-param> 

(note: if you have already defined facelets.LIBRARIES , then you can just add a new path processed by processing, for Facelets 2.x you need javax.faces.FACELETS_LIBRARIES context parameter)

Declare it in the Facelets XHTML file as the new XML namespace:

 <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:uf="http://example.com/util/functions" ... > 

Finally, you can use it for its intended purpose:

 <h:outputText value="foo" title="#{uf:convertDateTime(bean.date, 'both', 'medium', 'short', bean.timeZone)}" /> 

If necessary, you can specify the type and styles in the function and give the method a different name that indicates these default values.

If you use the JSF OmniFaces utility library , you can also use it #{of:formatDate()} .

+8
source

All Articles