2

我的actionListener托管 bean 中有一个由许多命令按钮调用的方法。

public void verifyTestDisponibility(ActionEvent actionEvent) {
    if (button1 clicked) {
        // ...
    }
    if (button2  clicked) {
        // ...
    }
}

我坚持的部分是识别单击的命令按钮。我怎样才能识别它?

4

3 回答 3

7

你可以像这样使用它

在 xhtml 页面中,我们可以使用<h:commandButton>带有 actionListener 的标签

<h:commandButton id="btn1" action="#{yourBean.method}" value="ButtonValue"
   actionListener="#{yourBean.commandClicked}"></h:commandButton>

在您的托管 bean 中

private String buttonId;
/* getters and setters for buttonId goes here */

public void commandClicked(ActionEvent event) {
  /* retrieve buttonId which you clicked */
  buttonId = event.getComponent().getId();

  /*check for which button you clicked*/
  if(buttonId.equals("btn1")){

  }else{

  }
}
于 2012-10-09T10:55:07.680 回答
2

您可以使用event.getComponent().getClientId();

if (event.getComponent().getClientId().equals("firstButton")).....
于 2012-10-09T10:17:45.103 回答
0

这也适用于 ajax 监听器。我曾与 Primefaces 5 ajax 监听器一起使用过。例如:

public void myListener(AjaxBehaviorEvent ev) {
  String clientId = ev.getComponent().getClientId();
  ...
}
于 2015-03-25T05:37:20.790 回答