ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-在JSF和ui:repeat中嵌套ajax

2019-12-09 01:03:38  阅读:372  来源: 互联网

标签:ajax jsf jsf-2 facelets java


<h:form>
    <fieldset>
        <h:selectManyListbox id="listbox" value="#{form.items}">
            <f:selectItems value="#{form.allItems}">
        </h:selectManyListbox>
    </fieldset>
    <h:commandButton value="Get Table" action="#{form.getTable}">
        <f:ajax render="result_table" execute="listbox" />
    </h:commandButton>
    <h:panelGrid id="result_table">
        <table>
            <thead></thead>
            <tbody>
                <ui:repeat var="table" value="#{form.resultTable}">
                    <tr>
                         <td>#{table.name}</td>
                         <td>
                             <h:selectBooleanCheckbox binding="#{showMore}">
                                 <f:ajax render="inner" />
                             </h:selectBooleanCheckbox>
                         </td>
                    </tr>
                    <tr>
                         <td>
                             <h:panelGrid id="inner" rendered="#{not empty showMore.value and showMore.value}">
                                 <table></table>
                             </h:panelGrid>
                         </td>
                    </tr>
                </ui:repeat>
            </tbody>
        </table>
    </h:panelGrid>
</h:form>     

我遵循了BalusC here的复选框示例代码.

当我单击命令按钮时,基于列表框的值,它将生成一个表结果,该结果显示在h:panelGrid id =“ result_table”中.该命令按钮起作用.在生成的表中,我有一些要显示的子表,但是仅当我选中显示它们的框时.对于复选框,如果我使用render =“ @ form”,它将折叠我的外部父表.如果我使用render =“ inner”,则选中/取消选中框不会执行任何操作.

我该如何工作?是否因为两个ajax发生冲突?

仅供参考,我也尝试使用bean属性代替,但是得到了相同的结果.当我尝试使用同一链接中提到的javascript方法时,选中/取消选中该框无效.

更新以添加示例代码段和< ui:repeat>< / ui:repeat>以上:

<h:selectBooleanCheckbox binding="#{showMore}">
     <f:ajax render="inner" />
 </h:selectBooleanCheckbox>
 <h:panelGrid id="inner">
     <h:outputText value="always" />
     <h:outputText value="hidden" rendered="#{not empty showMore.value and showMore.value}" />
 </h:panelGrid>

解决方法:

罪魁祸首在这里:

<h:panelGrid id="inner" rendered="#{not empty showMore.value and showMore.value}">
    <table></table>
</h:panelGrid>

f:ajax使用JavaScript在客户端中定位要渲染的元素.如果JSF未将JavaScript呈现给客户端,则JavaScript将无法找到内部panelgrid的HTML表示形式.将内部ID放到始终呈现给客户端的父组件中,这样Ajax / JavaScript可以在不考虑呈现条件的情况下找到它.例如.:

<h:panelGroup id="inner">
    <h:panelGrid rendered="#{not empty showMore.value and showMore.value}">
        <table></table>
    </h:panelGrid>
</h:panelGroup>

更新:根据评论,使用ui:repeat应该不会在这里造成问题.甚至还要确保我将您的示例复制粘贴到我的游乐场环境中,并且可以正常工作(在Tomcat 6.0.29上使用Mojarra 2.0.3). bean的方式是标记为@ViewScoped.也许您的bean是@RequestScoped,而ui:repeat值的加载是基于某些请求范围内的条件,而该条件没有保留在后续的ajax调用中?

也可以看看:

> The benefits and pitfalls of @ViewScoped-解释@ViewScoped的值
> What are the main disadvantages of JSF 2.0?-有点历史

标签:ajax,jsf,jsf-2,facelets,java
来源: https://codeday.me/bug/20191208/2095315.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有