我有一个用例,其中我必须使用资源包在UI上显示各种文本。其中一些资源束条目采用参数(例如{0}),因为这些参数我使用h:outputFormat,但有时这还不够。
例如
someMessage=Display this message with param {0}
在资源束中。
要在xhtml上显示它,我通常会这样做:
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
如果只是简单的情况,那很好用,但是对于更复杂的用例来说,还不够。例如,如果我希望commandLink的'title'属性使用上述资源束条目:
<h:commandLink action="logout" title="#{msg['someMessage']}">
<f:param value="#{someBean.value}" />
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
这是行不通的。我也尝试过:
<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不允许 child ,所以这也不起作用。
即使有黑客可以绕过这个问题(例如使用素数中的悬停组件),其他字段也可能需要参数化的消息。
有谁知道使用MessageFormat的方法,该方法在JSF组件的非值字段中接受参数?
最佳答案
您可以为此创建a custom EL function,最终最终可以像这样:
<h:commandLink ... title="#{my:format(msg['someMessage'], someBean.value)}" />
您可以使用
MessageFormat
API来执行此工作,就像<h:outputFormat>
在幕后所做的一样。一种替代方法是创建一个自定义组件,该组件的功能与JSTL的优良功能'ol
<fmt:message>
相同,后者支持var
属性以将格式化的消息导出到EL范围中。<my:outputFormat ... var="linkTitle">
...
</my:outputFormat>
<h:commandLink ... title="#{linkTitle}" />
更新:JSF实用程序库OmniFaces具有
#{of:formatX()}
函数和 <o:outputFormat>
组件。
关于jsf - 如何将参数化的MessageFormat与JSF组件的非Value属性一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12000006/