5

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>
Community
  • 1
  • 1
pkalinow
  • 1,619
  • 1
  • 17
  • 43
  • Are you using JSF 1.x or 2.x? Are you using JSP or Facelets? – BalusC Sep 28 '11 at 14:12
  • I'm using JSF 1.2 (or 1.1) and Facelets. – pkalinow Sep 28 '11 at 14:25
  • Thanks. I posted an answer. For JSP instead of Facelets, you'd need a `.tld` file in `/WEB-INF` instead of `.taglib.xml`. For JSF 2.x instead of JSF 1.x, you'd need to redeclare root element of `.taglib.xml` a bit differently (without doctype). – BalusC Sep 28 '11 at 14:41

1 Answers1

10

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555