如果JSF不支持,为什么我能够将<f:actionListener>绑定到任意方法?

Dav*_*idS 4 jsf binding el actionlistener methodexpression

我正在使用Glassfish 3.1.2.2和JSF Mojarra 2.1.6.

我有以下Facelets页面:

<h:form>
  <h:commandLink value="link">
    <f:actionListener binding="#{backingBean.someMethod(1)}"/>
  </h:commandLink>
</h:form>
Run Code Online (Sandbox Code Playgroud)

以下支持bean:

@RequestScoped
@ManagedBean
public class BackingBean {
  public void someMethod(int i) {
    System.out.println("It was called: " + i);
  }
}
Run Code Online (Sandbox Code Playgroud)

当我单击链接时,控制台中出现"Info:It called called:1".

binding读取文档:

图书馆:http://xmlns.jcp.org/jsf/core,http://java.sun.com/jsf/core(Jsf Core)

标签:actionListener

捆绑

值绑定表达式,其值为实现javax.faces.event.ActionListener 的对象.[强调我的]

此外,该问题的接受答案表明,f:actionListener调用任意方法是不可能的.

如果不支持,为什么调用backing bean方法?

Bal*_*usC 5

这是新的EL 2.2特性通过#{bean.method()}语法调用值表达式中的方法而不是仅通过#{bean.property}语法引用属性(其确实应该是确切类型ActionListener)的结果.它不会在EL 2.1或更早版本中工作,并且在删除参数和括号时也不起作用.该文件是在EL 2.2不存在的情况下编写的(与2006年5月的JSF 1.2版相比,它实际上没有修改; EL 2.2于2009年12月推出).然而,我确实同意它需要对该部分进行更新,因为它让初学者感到困惑.

你找到的答案根据文件得出了它的观点,但是回答者似乎没有意识到基于这个问题,虽然binding="#{testController.nodeListener}"失败了,binding="#{testController.nodeListener(event)}" 实际上是有效的.这只是没有给你机会通过ActionEvent.答案是更好的,如果它建议只使用binding="#{testController.nodeListener()}"而是以其他方式获取事件信息,例如通过调用UIComponent#getCurrentComponent()或甚至通过#{component}作为参数传递.当然,只有当你真的需要掌握它时.

<h:commandLink value="link">
    <f:actionListener binding="#{bean.someMethod(component)}"/>
</h:commandLink>
Run Code Online (Sandbox Code Playgroud)
public void someMethod(UIComponent component) {
    System.out.println("It was called on: " + component); // HtmlCommandLink
}
Run Code Online (Sandbox Code Playgroud)

也可以看看: