JSF 2.0 中不带参数的 MethodExpression 调用

发布于 2024-12-03 16:00:23 字数 319 浏览 0 评论 0原文

我试图通过

#{myBean.foo()}

(没有任何周围的标签)从 JSF 2.0 Facelet 调用无参数方法。

根据 Burns/Schalk:完整参考:JSF 2.0,这是可能的(第 126 页,#{userBean.pullValuesFromFlash( )})。

然而,框架将表达式视为值表达式,因此认为 foo 应该是 bean 属性。 在 JBOSS 7.0.1(以及 6)上,我收到

“类 '...' 没有属性 'foo'”

错误消息。

I'm trying to invoke a parameterless method from a JSF 2.0 facelet by

#{myBean.foo()}

(without any surrounding tag).

According to Burns/Schalk: The Complete Reference: JSF 2.0 that's possible (page 126, #{userBean.pullValuesFromFlash( )}).

However the framework takes the expression to be a value expression and thus thinks that foo should be a bean property.
On JBOSS 7.0.1 (and 6, too) I get a

"The class '...' does not have the property 'foo'"

error message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空袭的梦i 2024-12-10 16:00:23

JBoss 论坛上的此回复来看,方法表达式只能在支持的属性中使用他们。

斯坦·西尔弗特写道:

在我看来,这正在按预期工作。这没有什么关系
缺乏论据。你的表情,
#{elManagedBean.hello()} 被视为 ValueExpression。如果
您将方法更改为 getHello() 然后它就可以工作了。这
问题是,它应该被视为 ValueExpression 还是
方法表达式?例如,如果您在
action 属性将被视为 MethodExpression


您已将表达式放在
Facelets 页面而不是作为属性的值。据我所知
要知道,这将始终被视为 ValueExpression。我没看到
这在 Glassfish 中如何运作。可能有一些
代码将其作为 ValueExpression 进行尝试,然后将其作为
MethodExpression 如果失败。但是,我认为这会违背
EL 规格。换句话说,我很惊讶这会起作用
玻璃鱼。

Judging by this response on the JBoss forum, method expressions must only be used in attributes that support them.

Stan Silvert wrote:

It looks to me like this is working as expected. This has nothing to
do with a lack of arguments. Your expression,
#{elManagedBean.hello()} is being treated as a ValueExpression. If
you changed your method to getHello() then it would work. The
question is, should it be treated as a ValueExpression or a
MethodExpression? For instance, if you had the same expression in an
action attribute it would be treated as a MethodExpression.

<h:commandButton value="Hello" action="#{elManagedBean.hello()}" 
     id="submit_button"/>

You have put the expression in the middle of
the Facelets page and not as the value of an attribute. As far as I
know, this will always be treated as a ValueExpression. I don't see
how this would work in Glassfish. It's possible that there is some
code that tries it out as a ValueExpression and then tries it as a
MethodExpression if it fails. However, I think that would go against
the EL spec. In other words, I'm surprised that this would work on
Glassfish.

陈独秀 2024-12-10 16:00:23

McDowell 已经回答了问题的原因:内联表达式被视为值表达式,而不是方法表达式。

至于如何实现功能需求,请使用

<f:event type="preRenderView" listener="#{myBean.foo}" />

这将在渲染响应之前调用该方法。

McDowell has answered the cause of the problem: inline expressions are treated as value expressions, not as method expressions.

As to how to achieve the functional requirement anyway, use <f:event>.

<f:event type="preRenderView" listener="#{myBean.foo}" />

This will invoke the method right before render response.

享受孤独 2024-12-10 16:00:23

这取决于您在 servlet 容器上使用的 EL 版本。如果使用 Tomcat 6,则包含 EL 2.1,并且如果表达式位于 Facelets 页面的中间,则不支持“()”作为 MethodExpression。 Tomcat 7(包括 EL 2.2)确实支持此功能,甚至增强了功能,能够将参数传递给方法表达式:

因此您可以这样做:

<h:outputText value="#{object.test(10)}" ></h:outputText>

并在 bean 中接收参数(可能需要额外的转换和验证):

public String test(MyObject o)
{
    ...
    return value;
}

参考:
http://tomcat.apache.org/whichversion.html
将 EL 2.2 与 Tomcat 6.0.24 结合使用

This depends on the EL version you are using on your servlet container. If using Tomcat 6, EL 2.1 is included and this does not support '()' as MethodExpression if the expression is in the middle of the Facelets page. Tomcat 7, which includes EL 2.2 does support this and even enhanced features as being able to pass parameters to the method expression:

So you do this:

<h:outputText value="#{object.test(10)}" ></h:outputText>

And receive the parameter in your bean (extra conversion and validation might be needed):

public String test(MyObject o)
{
    ...
    return value;
}

References:
http://tomcat.apache.org/whichversion.html
Using EL 2.2 with Tomcat 6.0.24

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文