I read on other threads that this does not work:
<h:commandButton value="Create New Account"
action="#{acctBean.doCreate}" >
<f:param name="acctName" value="#{acctBean.handle}" />
<f:param name="acctNo" value="#{acctBean.id}" />
</h:commandButton>
The doCreate() method will return a navigation to a “congratulations” page if it creates the account. The target page can then resolve #{param.handle} and #{param.id}.
I know this will work if I use h:commandLink instead, but I want a button, not a link. Is there any generally accepted way of doing that?
UPDATE:
Based on the first answer from @BalusC I created the following test code:
<h:commandButton value="Push Me" action="goAcctCreated" >
<f:param name="acctName" value="This Is Account Name" />
<f:param name="acctNo" value="1234" />
</h:commandButton>
<h:button value="Push Me #2" outcome="newAcct" >
<f:param name="acctName" value="This Is Account Name" />
<f:param name="acctNo" value="1234" />
</h:button>
And in the target page I have:
<p>You may now log in with the account you just created: <b>#{param['acctName']}</b>.</p>
<p>This is account number <b>#{param['acctNo']}</b>.</p>
As before, the h:commandButton does not work with a POST transaction and as BalusC said, h:button does a GET and does work.
Interestingly enough, on the POST that the h:commandbutton does it has the parameters encoded, as viewed by Firebug:
acctName This Is Account Name
acctNo 1234
javax.faces.ViewState 8642267042811824055:-4937858692781722161
testForm testForm
testForm:j_idt55 testForm:j_idt55
So the f:param tags are doing their job at least, but the target page doesn’t resolve the EL expressions #{param[xxx]}. They also don’t show up in the scoped variables report (ctrl-shift-D). Is there something I am supposed to do on the target page?
This should work perfectly fine on JSF 2.x. Did you ever try it yourself? If it doesn’t work, then you’re either actually using JSF 1.x or you’re sending a redirect after POST.
The other threads which you’re referring to were undoubtely talking about JSF 1.x, when the
<f:param>was indeed not supported on<h:commandButton>. On JSF 1.x, you would have used<f:setPropertyActionListener>instead or some shot of CSS to style the<h:commandLink>to look like a button.E.g.
with
Note that in JSF 2.x you’ve also the opportunity to use the new
<h:button>which fires a GET request instead of a POST request. This is better if you don’t need to execute any bean action (i.e. your current action is just returning a plain navigation case outcome) and want the request to be idempotent.This will navigate to
create.xhtmlwith the given parameters in request URL.