Search code examples
javascriptjsfresourcebundleparam

JSF resource bundle params in javascript?


I have a command button that generates a confirm pop-up. The text of the confirm comes from a message bundle. Is it possible to pass parameters to the confirm?

This is what I am trying:

<h:commandButton value="#{tkmsg.addAccount}" action="#{ebfAccountControllerBean.specifyEbfAddAccount}" 
                        onclick="return confirm('#{tkmsg.confirmAddAccount}');">
            <f:param value="this account"/>
            <f:param value="this email"/>
</h:commandButton>

But it doesn't work. I just get

Are you sure you want to add account {0} with email {1}?

Do parameters only work with OutputText or OutputFormat? Is there any other way to do this? My next step would be to replace "this account" with data from the form.


Solution

  • What you're doing indeed doesn't work that way. JSF has no way to relate the f:param tags to that EL expression. Just think about it, even a human would not be able to guess this ;)

    You could render your message upfront into something accessible via EL, using e.g. Tomahawks buffer:

    <t:buffer into="#{buffer['confirm']}">
       <h:outputFormat value="#{tkmsg.confirmAddAccount}">
          <f:param value="this account"/>
          <f:param value="this email"/>
       </h:outputFormat>
    </t:buffer>
    

    #{buffer} is simply a hasmap declared as managed bean with request scope. After this you can reference #{buffer['confirm']} in the javascript statement.