问题描述
我使用Glassfish的3.1.2.2和JSF Mojarra 2.1.6。
I'm using Glassfish 3.1.2.2 and JSF Mojarra 2.1.6.
我有以下的Facelets页:
I have the following Facelets page:
<h:form>
<h:commandLink value="link">
<f:actionListener binding="#{backingBean.someMethod(1)}"/>
</h:commandLink>
</h:form>
和下面的支持bean:
And the following backing-bean:
@RequestScoped
@ManagedBean
public class BackingBean {
public void someMethod(int i) {
System.out.println("It was called: " + i);
}
}
当我点击链接,信息:它被称为:1出现在控制台
When I click the link, "Info: It was called: 1" appears in the console.
为结合
的文件内容如下:
库:, http://java.sun.com/jsf/core (JSF核心)
标签:的ActionListener
Tag: actionListener
结合
值绑定前pression中的一个的对象的实现javax.faces.event.ActionListener。 [重点煤矿]
Value binding expression that evaluates to an object that implements javax.faces.event.ActionListener. [emphasis mine]
此外,接受的答案来说,它不可能为 F:。的ActionListener
来调用任意方法
Also, the accepted answer to this question states that it's not possible for an f:actionListener
to call an arbitrary method.
如果不支持此操作为什么叫backing bean的方法?
Why is the backing bean method called if this isn't supported?
推荐答案
这是通过调用值前pression方法的新EL 2.2功能的结果#{豆。方法()}
语法,而不是仅通过#{bean.property}
语法引用一个属性(这的确应该是准确的类型的ActionListener
)。它不会在EL 2.1或以上工作过,它也不会当您删除的参数和括号工作。相比的; EL 2.2中引入2009年12月)。然而,我不同意,它需要在该部分的更新,因为它是混乱的起动器。
This is the consequence of the new EL 2.2 feature of calling a method in a value expression via the #{bean.method()}
syntax instead of only referencing a property via the #{bean.property}
syntax (which should indeed be of the exact type ActionListener
). It wouldn't have worked in EL 2.1 or older and it would also not work when you remove the arguments and the parentheses. That document was written when EL 2.2 didn't exist (it's actually not modified as compared to JSF 1.2 version from May 2006; EL 2.2 was introduced December 2009). I however do agree that it needs an update on that part as it's confusing to starters.
您找到了答案做出了点基于文档,但却回答者似乎没有根据的问题认识到,虽然绑定=#{} testController.nodeListener
失败了,绑定=#{testController.nodeListener(事件)}
实际的工作。这不仅不会给你机会传递的ActionEvent
。答案是,如果它建议只使用更好的绑定=#{testController.nodeListener()}
,而不是抢在其他的方式该事件的信息,如通过调用 UIComponent#getCurrentComponent()
甚至通过传递#{组件}
作为论据。只有当你真的需要有它的手,当然。
The answer you found made its points based on the document, but the answerer however didn't seem to realize based on the question that while binding="#{testController.nodeListener}"
failed, the binding="#{testController.nodeListener(event)}"
actually worked. This only doesn't give you the opportunity to pass the ActionEvent
. The answer was better if it suggested to just use binding="#{testController.nodeListener()}"
instead and grab the event information in other way, such as by calling UIComponent#getCurrentComponent()
or even by passing #{component}
as argument. Only if you really need to have a hand of it, of course.
<h:commandLink value="link">
<f:actionListener binding="#{bean.someMethod(component)}"/>
</h:commandLink>
public void someMethod(UIComponent component) {
System.out.println("It was called on: " + component); // HtmlCommandLink
}
参见:
- 参数/变量/参数
- 之间
- 之间
- Invoke direct methods or methods with arguments / variables / parameters in EL
- Difference between JSP EL, JSF EL and Unified EL
- Differences between action and actionListener
See also:
这篇关于为什么我能够绑定&LT; F:ActionListener的&GT;到任意的方法,如果它不是由JSF支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!