0

Using JSF2 markup, is it possible to pass properties parameters to button value? I am aware of <h:outputFormat> tag but I need it to be a button. I am interested more in something like below.

My XHTML code:

<h:button value="#{webCommonMessages['notifications']}" outcome="...">
    <f:param value="1"/>
</h:button>

My webCommonMessages bundle:

notifications=Notifications available: ({0})

This of course does not work but is there any solution/workaround for it?

wst
  • 4,040
  • 4
  • 41
  • 59

1 Answers1

2

Unfortunately, JSF doesn't provide built-in functionality to get what you want, so basically you have three ways to go:

  1. create a custom EL function to get the substituted value, as described in Pass parameters to messages from resource bundle to components other than h:outputFormat,
  2. create a custom component that exports the substituted value as a request scope variable visible in EL context, as hinted in How to use Parameterized MessageFormat with non-Value attributes of JSF components or
  3. use the solution proposed in utility library OmniFaces maintained by BalusC as per the component <o:outputFormat> that effectively incorporates in itself the second way.

So, if you are up to the third way, which I personally warmly recommend, the solution is basically as follows:

<o:outputFormat value="#{webCommonMessages['notifications']}" var="t">
    <f:param value="1" />
</o:outputFormat>
<h:button value="#{t}" outcome="..." />

Using OmniFaces in a JSF project is ridiculously easy and is descibed on its homepage, but to repeat: just add its jar to /WEB-INF/lib folder of your web application (or include it as a maven dependency) and add a proper namespace in your facelet views, like xmlns:o="http://omnifaces.org/ui".

It's also worth adding that my initial answer was wrong. It erroneously assumed that nesting JSF's <h:outputFormat> will render the necessary HTML element. Still, it is true for a plain navigational link, as provided in JSF by, for example, <h:link>, and is to be used like:

<h:link outcome="...">
    <h:outputFormat value="#{webCommonMessages['notifications']}">
        <f:param value="1" />
    </h:outputFormat>
</h:link>

You wrongly assumed that <f:param> nested in <h:button> will be incorporated in your message from resource bundle. It is used to append query parameters to the generated URL. Instead you need to use <h:outputFormat> tag for your purpose nested within your button and, thus, not specifying its value attribute:

<h:button outcome="...">
    <h:outputFormat value="#{webCommonMessages['notifications']}">
        <f:param value="1" />
    </h:outputFormat>
</h:button>
Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Just tried that and it rendered empty button (no value) and text message from outputFormat next to it. What I need is button with parametrized value. – wst Jul 30 '13 at 12:58
  • I will use ``h:link`` first, but OmniFaces looks promising. Many thanks for help. – wst Jul 30 '13 at 19:11