11

有没有一种方便的方法可以将参数从资源包传递到h:outputFormat以外的组件?

例如,这是合法的:

<h:outputFormat value="#{myBundle['parametricMessage']}">
    <f:param value="#{myBundle['someParameterValue']}"/>
</h:outputFormat>

但我需要一个按钮,像这样(不起作用):

<h:commandButton value="#{myBundle['parametricMessage']}">
    <f:param value="#{myBundle['someParameterValue']}"/>
</h:commandButton>

当然,我可以使用链接而不是按钮,并且可以通过托管 bean 中的属性来实现,但是在这个问题中,我正在寻找一种方便的方式来使用按钮......

我正在使用 RichFaces 3.3.3、JSF2、facelets。

4

3 回答 3

14

这种方法怎么样?

EL 表达式允许您定义一个函数。您首先定义一个 EL 表达式的函数,该函数接受一个资源包、它的消息键和占位符参数并输出解析的消息。

public static String geti18nMsg(ResourceBundle bundle ,String msgKey, String paramValue ) {
    String  msgValue = bundle.getString(msgKey);
    MessageFormat   messageFormat = new MessageFormat(msgValue);
    Object[] args = {paramValue};
    return messageFormat.format(args);
}

然后调用此函数以获取已解决的消息<h:commandButton>

<h:commandButton value="#{f:geti18nMsg(myBundle , parametricMessage, someParameterValue)}"/>
于 2011-04-20T06:35:44.650 回答
4

尝试这个:

<h:commandButton>
    <h:outputFormat value="#{myBundle['parametricMessage']}">
        <f:param value="#{myBundle['someParameterValue']}"/>
    </h:outputFormat>
</h:commandButton>

顺便说一句,这可以满足您的需求,并且还避免了编写支持 bean 代码。

于 2012-08-17T14:25:48.450 回答
0

Well I didn't find good answer on that, so the question will remain open. A good practice I've discovered, is to have a special class that represents each resource bundle (that has parametric stuff), and to transfer all the message formation, and working with context there (like, acquire locale from FacesContext, get a ResourceBundle, apply parameters, etc). And finally to provide access to a singleton of such service class from your ManagedBean.

This requires some additional work to be done, but solves the problem and is worth of the time.

于 2011-04-19T23:31:25.157 回答