Java 如何在全球范围内收听actionevents

Java 如何在全球范围内收听actionevents,java,jsf-2,Java,Jsf 2,是否可以注册一个全局actionlistener,该全局actionlistener将侦听页面上激发的所有动作事件? 如果是这样,这意味着我不需要在每个动作组件上注册相同的(默认)actionlister。有两种方法可以做到这一点: 使用actionListener参数: <h:commandButton id="test1" value="Test 1" actionListener="#{bean.action}" /> <h:commandButton id="test

是否可以注册一个全局actionlistener,该全局actionlistener将侦听页面上激发的所有动作事件?

如果是这样,这意味着我不需要在每个动作组件上注册相同的(默认)actionlister。

有两种方法可以做到这一点:

使用
actionListener
参数:

<h:commandButton id="test1" value="Test 1" actionListener="#{bean.action}" />
<h:commandButton id="test2" value="Test 2" actionListener="#{bean.action}" />


public void action(ActionEvent event)
{
    System.out.println(event.getComponent().getClientId());
}
<h:commandButton id="test1" value="Test 1" actionListener="#{bean.action('test1')}" />
<h:commandButton id="test2" value="Test 2" actionListener="#{bean.action('test1')}" />

public void action(String sender)
{
    System.out.println(sender);
}

公共无效行动(行动事件)
{
System.out.println(event.getComponent().getClientId());
}
使用正常参数:

<h:commandButton id="test1" value="Test 1" actionListener="#{bean.action}" />
<h:commandButton id="test2" value="Test 2" actionListener="#{bean.action}" />


public void action(ActionEvent event)
{
    System.out.println(event.getComponent().getClientId());
}
<h:commandButton id="test1" value="Test 1" actionListener="#{bean.action('test1')}" />
<h:commandButton id="test2" value="Test 2" actionListener="#{bean.action('test1')}" />

public void action(String sender)
{
    System.out.println(sender);
}

公共作废操作(字符串发送器)
{
System.out.println(发送方);
}

为什么不简单地始终使用相同的
actionListener=“#{sameBean.sameListener}”
对于每个组件,您都可以使用ActionEvent获取发送者。我还是不明白你为什么要这么做……你能告诉我你到底想做什么吗?只是想知道这个想法…@AlexandreLavoie我想保持我的代码干净,可能我需要另一个actionlistener作为一个组件。@FahimParkar我正试图找出这个动作是从页面的哪个部分产生的