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



我读了很多关于f:ajax与其他jsf或html标签的工作,但它似乎还不够!

我有一个命令按钮与(必要吗?)f:ajax标签在我的页面和动作永远不会被触发。代码如下:

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

所以像上面一样,我有两个按钮,第一个在wrap工作,它必须显示在开始隐藏的wrap面板。第二个在包装中,必须启动一个完全不同的动作。

这是豆子。

@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标记呈现包装。然而,当第二个按钮被点击时,什么也没有发生。

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

我想是关于面板组的渲染选项,但是我找不到它的精确位置。

谢谢

根据未调用commandButton/commandLink/ajax action/listener方法或未设置/更新输入值的第9点,您是JSF规范问题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>

我决定做一些更简单的事情。实际上,我的页面并不需要ajax,因为我的bean在ViewScope中。所以页面可以重新加载,这不是问题。我去掉了f:ajax标签,一切正常

最新更新