如何将参数化的MessageFormat与JSF组件的非Value属性一起使用 [英] How to use Parameterized MessageFormat with non-Value attributes of JSF components
问题描述
我有一个用例,其中我必须使用资源包在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?
推荐答案
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,与<h:outputFormat>
在幕后所做的完全一样.
You can use the MessageFormat
API to perform the job, exactly as <h:outputFormat>
is doing under the covers.
一种替代方法是创建一个自定义组件,该组件的功能与JSTL的良好功能相同'ol
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 具有 <o:outputFormat>
组件正是出于这一目的.
Update: JSF utility library OmniFaces has #{of:formatX()}
functions and a <o:outputFormat>
component for the very purpose.
这篇关于如何将参数化的MessageFormat与JSF组件的非Value属性一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!