问题
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?
回答1:
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.
<h:commandLink styleClass="button" ...>
with
a.button {
display: inline-block;
background: lightgray;
border: 1px outset lightgray;
outline: none;
color: black;
text-decoration: none;
cursor: default;
}
a.button:active {
border-style: inset;
}
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.
<h:button value="Create New Account" outcome="create">
<f:param name="acctName" value="#{acctBean.handle}" />
<f:param name="acctNo" value="#{acctBean.id}" />
</h:button>
This will navigate to create.xhtml with the given parameters in request URL.
来源:https://stackoverflow.com/questions/7366480/passing-parameters-with-hcommandbutton-or-the-equivalent