如何识别我的托管 Bean 中单击的命令按钮



我的托管 Bean 中有一个 actionListener 方法,它被许多命令按钮调用。

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

我卡住的部分是识别单击的命令按钮。我如何识别它?

你可以这样使用它

在 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{
  }
}

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

if (event.getComponent().getClientId().equals("firstButton")).....

这也适用于 ajax 侦听器。我曾经使用过Primefaces 5 ajax监听器。例如:

public void myListener(AjaxBehaviorEvent ev) {
  String clientId = ev.getComponent().getClientId();
  ...
}

最新更新