将 JSF 中的日期转换为“标题”表中的属性

发布于 2024-12-07 10:17:31 字数 576 浏览 1 评论 0原文

JSF 是否可以转换日期值并将其放入“title”属性中?在类似的问题 JSF Convert dates for title attribute 中,有一个答案,可以使用 JSTL 的 fmt:formatDate,但不在重复组件中,例如 UIData。我需要在表(扩展 HtmlDataTable)内执行此操作。

例如,以下代码正确地将日期显示为文本值,但不在标题属性中:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-12-14 10:17:31

仅转换 value 属性,而不转换其他属性。在这种特殊情况下,最好的选择是为此创建一个自定义 EL 函数。

首先创建一个带有 public static 方法的 final 类,该方法接受必要的参数并委托给 JSF DateTimeConverter (包/类/方法名称为自由选择):

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);
    }

}

/META-INF/functions.taglib.xml 中将其定义为 facelet-taglib (文件名可以自由选择):

<?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>

(注意:对于 Facelets 2.x,您需要 XSD 而不是 DTD;有关示例,请参阅 这个答案

/WEB-INF/web.xml 中将其注册为新标签库:

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

(注意:如果您已经拥有facelets.LIBRARIES 定义,然后您只需添加以逗号分隔的新路径;对于 Facelets 2.x,您需要 javax.faces.FACELETS_LIBRARIES 上下文参数)

在 Facelets XHTML 文件中将其声明为新的 XML 命名空间:

<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"
    ...
>

最后,您可以按预期使用它:

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

如果需要,您可以对函数中的类型和样式进行硬编码,并为该方法指定一个不同的名称来指示这些名称默认值。

如果您碰巧使用 JSF 实用程序库OmniFaces,那么您也可以使用它的#{of:formatDate()} 函数代替。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文