如何将参数化 MessageFormat 与 JSF 组件的非值属性一起使用 [英] How to use Parameterized MessageFormat with non-Value attributes of JSF components

查看:19
本文介绍了如何将参数化 MessageFormat 与 JSF 组件的非值属性一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,我必须使用资源包在 UI 上显示各种文本.其中一些资源包条目采用参数(例如 {0}),对于这些,我使用 h:outputFormat 但有时这还不够.

I have use case in which I have to use resource bundle to display various texts on UI. Some of these resource bundle entries take paramets (e.g. {0}), for these I use h:outputFormat but sometimes that isn't enough.

例如

someMessage=Display this message with param {0}

在资源包中.

要在 xhtml 上显示它,我通常会这样做:

To display it on xhtml I normally do:

<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>

这在简单的情况下效果很好,但对于更复杂的用例,这还不够.例如,如果我希望 commandLink 的title"属性使用上述资源包条目:

That works well when it's a simple case, but for more complex use cases it isn't enough. For example if I want the 'title' attribute of a commandLink to use the above resource bundle entry:

    <h:commandLink action="logout" title="#{msg['someMessage']}">
        <f:param value="#{someBean.value}" />
        <h:graphicImage library="images" name="image.png" />
    </h:commandLink>

这不起作用.我也试过:

which doesn't work. I also tried:

    <h:commandLink action="logout">
        <f:attribute name="title">
            <h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
        </f:attribute>
        <h:graphicImage library="images" name="image.png" />
    </h:commandLink>

这也不起作用,因为 f:attibute 不允许孩子.

which also doesn't work since f:attibute doesn't allow children.

即使有黑客绕过这个(例如使用来自 primefaces 的悬停组件),也有其他字段可能需要参数化消息.

Even if there is a hack to bypass this (e.g. using hover component from primefaces) there are other fields that might require a parameterized message.

有谁知道使用 MessageFormat 的方法,它在 JSF 组件的非值字段中接受一个参数?

Does anyone know of a way to use MessageFormat that takes an argument in a non-value field of a JSF component?

推荐答案

您可以创建 一个自定义的 EL 函数,你最终可以像这样:

You could create a custom EL function for this with which you can ultimately end up like:

<h:commandLink ... title="#{my:format(msg['someMessage'], someBean.value)}" />

您可以使用MessageFormat API 来执行这项工作,正如 在幕后所做的那样.

You can use the MessageFormat API to perform the job, exactly as <h:outputFormat> is doing under the covers.

另一种方法是创建一个自定义组件,它与 JSTL 的好 'ol 支持 var 属性将格式化的消息导出到 EL 范围.

An alternative is to create a custom component which does the same as JSTL's good 'ol <fmt:message> which supports a var attribute to export the formatted message into the EL scope.

<my:outputFormat ... var="linkTitle">
    ...
</my:outputFormat>
<h:commandLink ... title="#{linkTitle}" />

<小时>

更新:JSF 实用程序库 OmniFaces 具有 #{of:formatX()} 函数和 组件就是为了这个目的.


Update: JSF utility library OmniFaces has #{of:formatX()} functions and a <o:outputFormat> component for the very purpose.

这篇关于如何将参数化 MessageFormat 与 JSF 组件的非值属性一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆