从未在h:commandButton上触发JSF动作(使用f:ajax)

时间:2013-06-12 06:28:23

标签: jsf render commandbutton

我读了很多关于f的信息:ajax使用其他jsf或html标签,但它似乎还不够!

我的页面中有一个带有(必要的?)f:ajax标记的commandButton,并且永远不会触发操作。这是代码。

<html>
  <h:head>
  </h:head>

  <h:body>
    <h:form>
      <h:commandButton action="#{bean.myFirstFunction}" value="Submit">
        <f:ajax render=":wrap" />
      </h:commandButton>
    </h:form>

    <h:panelGroup id="wrap">
      <h:panelGroup id="content" rendered="#{bean.myVariable != null}">

       <h:form>
         <h:commandButton action="#{bean.mySecondFunction}" value="Submit">
           <f:ajax render=":wrap" />
         </h:commandButton>
       </h:form>

      </h:panelGroup>
    </panelGroup>
  <h:body>
</html>

所以就像上面我得到了两个按钮,第一个从包装中工作,它必须显示在开头隐藏的包装面板。第二个是在包装中,必须启动一个完全不同的动作。 (包装必须保持显示)

这是豆。

@ManagedBean(name = "bean")
@ViewScoped
public class myBean {
  private Object myVariable;

  public void setMyVariable(Object o) { ... }
  public Object getMyVariable() { ... }

  @PostConstruct
  public void init() {
    this.myVariable = null;
  }

  public void myFirstFonction() {
    this.myVariable = new Object();
  }

  public void mySecondAction() {
    System.out.println("here");
  }
}

当我启动时,我看到第一个按钮。我点击它,第二个出现。 (因此调用第一个函数,实例化对象,并使用ajax标记呈现包装。 但是,当单击第二个按钮时,没有任何反应。

我只编写了部分代码,希望错误在这里。

我认为这是关于面板组的渲染选项,但我无法准确找到它。

谢谢

2 个答案:

答案 0 :(得分:4)

根据commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated的第9点,您是JSF spec issue 790的受害者。当你ajax更新一些内容,而这些内容又包含一个表单时,它将失去其视图状态。作为快速解决方法,只需为待更新表单提供ID,并在<f:ajax render>中明确引用它。

  <h:body>
    <h:form>
      <h:commandButton action="#{bean.myFirstFunction}" value="Submit">
        <f:ajax render=":wrap :wrapForm" />
      </h:commandButton>
    </h:form>

    <h:panelGroup id="wrap">
      <h:panelGroup id="content" rendered="#{bean.myVariable != null}">

       <h:form id="wrapForm">
         <h:commandButton action="#{bean.mySecondFunction}" value="Submit">
           <f:ajax render=":wrap" />
         </h:commandButton>
       </h:form>

      </h:panelGroup>
    </panelGroup>
  <h:body>

答案 1 :(得分:0)

我决定做点容易的事情。事实上,我的页面中并不需要ajax,因为我的bean在ViewScope中。所以页面可以重新加载,这不是一个真正的问题。所以我摆脱了我的f:ajax标签,一切都是对的。

相关问题