【问题标题】:understanding ajax execute behavior with datatable using jsf 2.0使用 jsf 2.0 通过数据表了解 ajax 执行行为
【发布时间】:2012-02-01 08:40:02
【问题描述】:

我正在尝试使用 搜索按钮 来返回所选项目(可以是多个)并更新数据表。然后我在每个列旁边都有一个 selectBooleanCheckbox,当用户选择“n”个项目然后按 选择选中的项目它插入 DB。

代码如下:

<h:panelGrid columns="5">
    <h:form>
      <h:outputText value="Item Name"/>
      <p:inputText value="#{StockController.itemName}"/>
      <h:commandButton value="Search" action="#{StockController.Search}">
         <f:ajax execute="@form" render=":results"/>
      </h:commandButton>
    </h:form>

//下面的代码示例属于BalusC 见帖子here

    <h:panelGroup id="results">
    <h:form>
    <h:dataTable value="#{bean.entities}" var="entity">
        <h:column>
            <h:selectBooleanCheckbox value="#{bean.checked[entity.id]}" />
        </h:column>
        ...
    </h:dataTable>
    <h:commandButton value="Select the checked Items" action="#{StockController.insertDao}" >
  <f:ajax execute="@form" render=":results"/>
 </h:commandButton>
 </h:form>
</h:panelGrid>

现在,我已经阅读了很多博客和 Core javaServer Faces 3,我认为 ajax 的使用没有逻辑错误。我通过删除每个 ajax 进行了测试,然后 两者都可以正常工作,但是每当我尝试将 两个 cummondButtons 与 ajax 一起使用时,第二个“选择选中的项目" 甚至不调用 "StockController.insertDao" 方法。

感谢任何帮助。

谢谢大家。

【问题讨论】:

    标签: ajax jsf jsf-2 datatable


    【解决方案1】:

    当你想要 ajax-render 内容又包含另一个表单时,你需要在 render 属性中包含那个表单的 ID,否则另一个表单将失去它的视图状态,什么都不会在提交时处理。

    <h:form>
      ...
      <h:commandButton value="Search" action="#{StockController.Search}">
        <f:ajax execute="@form" render=":results :resultsForm" />
      </h:commandButton>
    </h:form>
    
    <h:panelGroup id="results">
      <h:form id="resultsForm">
        ...
      </h:form>
    </h:panelGroup>
    

    但是,如果在同一 &lt;h:panelGroup&gt; 内没有任何内容超出 &lt;h:form&gt;,那么您可以完全省略 &lt;h:panelGroup&gt; 并直接重新渲染表单。

    <h:form>
      ...
      <h:commandButton value="Search" action="#{StockController.Search}">
        <f:ajax execute="@form" render=":results" />
      </h:commandButton>
    </h:form>
    
    <h:form id="results">
      ...
    </h:form>
    

    这与JSF spec issue 790有关。

    另见:

    【讨论】:

    • 非常感谢刚开始学习jsf,容易犯这种错误。现在我明白了。我还想说我非常喜欢你的博客和回复。
    • 不客气。我不会立即说这是你自己的错误。这并不直观。这只是 JSF 规范中的一个错误/疏忽,需要解决,直到他们修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2011-05-02
    • 2015-07-19
    • 2011-09-23
    • 2012-05-23
    相关资源
    最近更新 更多