Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dates in JSF for "title" attribute in a table

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>
like image 409
pkalinow Avatar asked Sep 28 '11 14:09

pkalinow


1 Answers

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

First create a final class with a public static method which takes the necessary arguments and delegates to the JSF DateTimeConverter (package/class/method name is free to your choice):

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 a facelet-taglib in /META-INF/functions.taglib.xml (filename is free to 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 a XSD instead of a DTD; for an example see this answer)

Register it as 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 already have the facelets.LIBRARIES definied, then you can just add the new path commaseparated; for Facelets 2.x, you need javax.faces.FACELETS_LIBRARIES context param instead)

Declare it in the Facelets XHTML file as 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 as intended:

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

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.

like image 198
BalusC Avatar answered Nov 15 '22 08:11

BalusC