使用h:commandButton传递参数-或等效参数 [英] Passing parameters with h:commandButton -- or the equivalent

查看:158
本文介绍了使用h:commandButton传递参数-或等效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他线程上看到这是行不通的:

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>

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

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}.

我知道,如果我改用h:commandLink,这将起作用,但是我想要一个按钮,而不是链接.有什么公认的方式吗?

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?

更新:

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

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>

在目标页面中,我有:

<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>

与以前一样,h:commandButton不适用于POST事务,并且正如BalusC所说,h:button执行GET并起作用.

As before, the h:commandButton does not work with a POST transaction and as BalusC said, h:button does a GET and does work.

有趣的是,在POST上,h:commandbutton确实已对参数进行了编码,如Firebug所查看:

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

因此,f:param标记至少可以完成其工作,但是目标页面无法解析EL表达式#{param[xxx]}.它们也不会显示在范围变量报告(ctrl-shift-D)中.我应该在目标页面上做什么吗?

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?

推荐答案

这在JSF 2.x上应该可以正常工作.你自己尝试过吗?如果它不起作用,则说明您实际上是在使用JSF 1.x,或者是在POST之后发送重定向.

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.

例如

<h:commandLink styleClass="button" ...>

使用

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;
}

请注意,在JSF 2.x中,您还可以使用新的

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>

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

This will navigate to create.xhtml with the given parameters in request URL.

这篇关于使用h:commandButton传递参数-或等效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆