Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jsf - Pass parameters to messages from resource bundle to components other than **h:outputFormat**

Is there a convenient way to pass parameters to messages from resource bundle to components other than h:outputFormat?

For instance, this is legal:

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

But I need it for a button, like this (which won't work):

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

Of course, I can use link instead of button, and I can make it through a property in a managed bean, but in this question I'm seeking for a convenient way to use the button...

I'm using RichFaces 3.3.3, JSF2, facelets.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about this approach ?

EL expression allow you to define a function .You first define a EL expression 's function , which accepts a resource bundle , its message key and placeholder 's parameter and output the resolved message .

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

Then call this function to get the resolved message in the <h:commandButton> :

<h:commandButton value="#{f:geti18nMsg(myBundle , parametricMessage, someParameterValue)}"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...