开发者

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.


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.


Arjan gave a good example. An alternative is to write a JS function for that yourself. Here's a basic kickoff example which extends the JS String prototype with a new format() function.

<script>
    String.prototype.format = function() {
        var args = arguments;

        return this.replace(/\{(\d+)\}/g, function() {
            return args[arguments[1]];
        });
    }
</script>

Use it as follows:

onclick="return confirm('#{tkmsg.confirmAddAccount}'.format('this account', 'this email'));"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜