3

我读了很多关于 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 标记呈现包装。但是,当单击第二个按钮时,什么也没有发生。

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

我认为这与面板组的渲染选项有关,但我无法准确定位它。

谢谢

4

2 回答 2

5

根据未调用 commandButton/commandLink/ajax action/listener 方法或未设置/更新输入值的第 9 点,您是JSF 规范问题 790的受害者。当您 ajax-update 一些又包含表单的内容时,它将失去其视图状态。作为一种快速解决方法,只需为要更新的表单提供一个 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>
于 2013-06-12T11:39:49.380 回答
0

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

于 2013-06-12T08:04:50.750 回答