传递作为 EL 操作方法参数传递的请求范围变量不起作用 [英] Passing request scoped variable passed as EL action method parameter does not work

查看:18
本文介绍了传递作为 EL 操作方法参数传递的请求范围变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Glassfish 4.1 上使用 JSF 2.2.

I am using JSF 2.2 on Glassfish 4.1.

我正在尝试将查询参数作为操作方法参数传入,如下所示:

I am trying to pass in a query parameter to as an action method argument as follows:

// Example 1. This does not work.

// at url http://localhost:8080/app/order.xhtml?email=test@email.com

<p:commandButton value="Place order" action="#{orderManager.placeOrder(param['email'])}" />

(知道 param 是一个 隐式 EL 对象.)

(Know that param is an implicit EL object.)

在服务器日志中,我已将其配置为打印方法参数,但我可以看到传入的是一个空字符串,而不是我预期的test@email.com".

In the server log I have configured it to print the method parameter, but I can see that an empty string was passed-in, not "test@email.com" as I expected.

我已确认我的整体配置有效.如果我将上面的代码段替换为以下内容,则服务器日志中会输出test@email.com":

I have confirmed that my overall configuration is working. If I replace the above snippet with the following, then "test@email.com" is output in the server log:

// Example 2. This works.

<p:commandButton value="Place order" action="#{orderManager.placeOrder('test@email.com')}" />

我也确认我使用 EL 隐式对象是可行的.如果我从 FacesContext 中检索参数,则以下代码段有效(当然,在从 placeOrder 的签名中删除电子邮件参数之后):

I have also confirmed that my use of EL implicit objects is feasible. The following snippet works if I retrieve the parameter from the FacesContext (after removing the email parameter from placeOrder's signature, of course):

// Example 3. This works.

<p:commandButton value="Place order" action="#{orderManager.placeOrder()}" >
  <f:param name="email" value="#{param['email']}"/>
</p:commandButton>

这是最后一个谜,它真正让我感到困惑,如果我使用以下代码段,我可以从方法参数 FacesContext 中检索电子邮件"参数,但请记住,示例 1 中方法参数不可检索!

And here is a final mystery, one that truly confuses me, if I use the following snippet, I can retrieve the "email" parameter from both the method parameter and the FacesContext, but recall that the method parameter wasn't retrievable in Example 1!

// Example 4. This works, and BOTH parameters are retrievable!

<p:commandButton value="Place order" action="#{orderManager.placeOrder(param['email'])}" >
  <f:param name="email" value="#{param['email']}"/>
</p:commandButton>

我可以将隐式 JSF EL 对象作为操作方法参数传入吗?

你有没有解释为什么它在示例 4 中有效,而在示例 1 中无效?

And do you have an explanation for why it seems to work in Example 4, but not Example 1?

推荐答案

action 属性在表单提交触发的 HTTP 请求的应用请求值阶段评估,因此是不同的 HTTP请求而不是在表单中生成 HTML 输出的请求(并且请求中存在 email 参数).

The action attribute is evaluated during apply request values phase of the HTTP request triggered by the form submit, which is thus a different HTTP request than the one which produced the HTML output with therin the form (and having the email parameter present in the request).

<f:param> 标记在 HTTP 请求的渲染响应阶段进行评估,该阶段需要生成包含表单的 HTML 输出.因此,这最终会在生成的 HTML 输出中硬编码"(与 action 属性中的 EL 方法参数相反!).当用户提交表单时,它只会作为普通的 String 请求参数传递回服务器(如果它最初是复杂类型,您需要将其转换回来).

The <f:param> tag is evaluated during render response phase of the HTTP request which needs to produce the HTML output with therein the form. This thus ends up "hardcoded" in the generated HTML output (on contrary to the EL method arguments in the action attribute!). When the user submits the form, this just gets passed back to the server as a plain vanilla String request parameter (which you would need to convert back if it was originally a complex type).

这与值是否为隐式 EL 对象无关.

This has got nothing to do with whether the value is an implicit EL object or not.

也就是说,还有另外两种方式:

That said, there are 2 other ways:

  1. 将其作为隐藏输入传递(不,不能使用 ).

<h:form>
    <input type="hidden" name="email" value="#{param.email}" />
    ...
</h:form>

  • 将其设置为视图作用域 bean 的属性,只要视图存在,它就会留在 bean 中.

  • Set it as property of a view scoped bean, it'll stay in bean as long as the view lives.

    <f:metadata>
        <f:viewParam name="email" value="#{viewScopedBean.email}" />
    </f:metadata>
    

  • 这篇关于传递作为 EL 操作方法参数传递的请求范围变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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