使用h:commandButton传递参数 - 或等效参数

Ala*_*ect 2 jsf-2

我在其他线程上读到这不起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

doCreate()如果创建帐户,该方法将返回导航到"祝贺"页面.然后,目标页面可以解析#{param.handle}#{param.id}.

我知道如果我使用它会有效h:commandLink,但我想要一个按钮,而不是一个链接.有没有普遍接受的方式呢?

更新:

基于@BalusC的第一个答案,我创建了以下测试代码:

<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>
Run Code Online (Sandbox Code Playgroud)

在目标页面中,我有:

<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>
Run Code Online (Sandbox Code Playgroud)

和以前一样,它h:commandButton不适用于POST事务,正如BalusC所说,h:button做一个GET并且确实有效.

有趣的是,在POST上,h:commandbutton它具有编码的参数,如Firebug所见:

acctName    This Is Account Name
acctNo  1234
javax.faces.ViewState   8642267042811824055:-4937858692781722161
testForm    testForm
testForm:j_idt55    testForm:j_idt55
Run Code Online (Sandbox Code Playgroud)

所以f:param标签至少在做它们的工作,但目标页面不能解析EL表达式#{param[xxx]}.它们也不会出现在范围变量报告中(ctrl-shift-D).我应该在目标页面上做些什么吗?

Bal*_*usC 6

这应该在JSF 2.x上完全正常.你有没有亲自尝试过?如果它不起作用,那么你要么实际使用JSF 1.x,要么在POST后发送重定向.

你所指的其他主题毫无疑问是在讨论JSF 1.x,当时<f:param>确实不支持<h:commandButton>.在JSF 1.x上,您可以使用<f:setPropertyActionListener>CSS代替或者使用一些CSS样式<h:commandLink>来使其看起来像一个按钮.

例如

<h:commandLink styleClass="button" ...>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,在JSF 2.x中,您还有机会使用new <h:button>来激发GET请求而不是POST请求.如果您不需要执行任何bean操作(即您当前的操作只是返回简单的导航案例结果)并希望请求是幂等的,那么这样做会更好.

<h:button value="Create New Account" outcome="create">
    <f:param name="acctName" value="#{acctBean.handle}" />
    <f:param name="acctNo" value="#{acctBean.id}" />
</h:button>
Run Code Online (Sandbox Code Playgroud)

这将导航到create.xhtml请求URL中的给定参数.