【问题标题】:How to identify clicked commandButton in my managed bean如何在我的托管 bean 中识别单击的 commandButton
【发布时间】:2012-10-09 10:02:16
【问题描述】:

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

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

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

【问题讨论】:

    标签: jsf


    【解决方案1】:

    你可以这样使用它

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢回复,请问是javax.faces.event.ComponentSystemEvent类型吗?
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 2013-10-13
        • 2012-09-03
        • 1970-01-01
        • 2011-10-11
        • 2011-06-10
        相关资源
        最近更新 更多