我不会粘贴我的 bean 和模型类,因为代码一切正常。问题是,dashboard.xhtml 中的第一个循环 (ui:repeat) 工作正常,但 comments.xhtml 中的第二个循环 (c:forEach) 不起作用。
我测试了 comments.xhtml 中的 #{comment.listComments.size()} 确实返回大于零,这意味着 c:forEach 循环必须至少遍历一次注释,但它没有发生。
Dashboard.xhtml
<h:dataTable id="commentout" value="#{messageBean.getComments(msg)}" var="com" rendered="#{not empty messageBean.getComments(msg)}">
<h:column>
<div>
<b><i>#{com.owner.firstName} #{com.owner.lastName}:</i></b>
<h:outputText value=" #{com}" />
<h:commandLink style="margin: 0 0 0 10px; padding: 0;" styleClass="add_comment" value="Answer" action="#{commentBean.createCommentOnComment(com)}" />
</div>
<ui:repeat value="#{com.listComments}" var="comment" rendered="#{com.hasChildren}">
<ui:include src="comments.xhtml" />
</ui:repeat>
</h:column>
</h:dataTable>
comments.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<div style="margin-left: 20px;">
<div>
<b><i>>#{comment.owner.firstName} #{comment.owner.lastName}:</i></b>
<h:outputText value=" #{comment}" />
<h:commandLink style="margin: 0 0 0 10px; padding: 0;" styleClass="add_comment" value="Answer" action="#{commentBean.createCommentOnComment(comment)}" />
</div>
<c:when test="#{comment.hasChildren}">
#{comment.listComments.size()} <!-- Visual test for checking if it is really greater than zero -->
<c:forEach items="#{comment.listComments}" var="comment">
<ui:include src="comments.xhtml" />
</c:forEach>
</c:when>
</div>
</ui:composition>
视觉问题:
或
如果有人对递归方法有更好的想法,那么它也可能是正确的答案......
更新:
如果我更改<c:forEach />
至<ui:repeat />
而不是使用 </c:when>
使用“渲染”属性,它卡住并给我错误“溢出”。
为什么<ui:repeat />
与 <ui:include />
总之给了我溢出错误?
最佳答案
感谢@BalusC,我从以下来源获得了成功解决方案的信息: c:forEach inside primefaces(e.g. p:panelgrid) inside ui:repeat .
我编写的dashboard.xhtml代码只是所有代码的一部分。这部分位于另外两个数据表中。我不知道数据表是否也与这个问题有关,但为了减少问题,我将它们更改为 <c:when>
, <c:forEach>
简单<div>
标签。
当我全部更改<ui:include>
的时候问题就解决了至<c:forEach>
标签。
如果您遇到像我一样的问题,那么您应该执行以下步骤来找到解决方案:
1) 更改全部:
<h:dataTable value="#{messageBean.getComments(msg)}" var="com" rendered="#{not empty messageBean.getComments(msg)}">
<h:column>
<div>
...
</div>
</h:column>
</h:dataTable>
致:
<c:when test="#{not empty messageBean.getComments(msg)}">
<c:forEach items="#{messageBean.getComments(msg)}" var="com">
<div>
...
</div>
</c:forEach>
</c:when>
2) 更改全部:
<ui:repeat value="#{com.listComments}" var="comment" rendered="#{com.hasChildren}">
<ui:include src="comments.xhtml" />
</ui:repeat>
致:
<c:when test="#{com.hasChildren}">
<c:forEach items="#{com.listComments}" var="subComment">
<ui:include src="comments.xhtml">
<ui:param name="comment" value="#{subComment}" />
</ui:include>
</c:forEach>
</c:when>
3) 重要或只是建议:
3.1) 分割<ui:include>
标记并放置 <ui:param />
里面有标签!
3.2) 对于<ui:include>
标签使用与 <ui:param />
不同的“var”参数标签的“name”参数有(示例已描述,可以在第二章中看到)。
关于java - JSF-c :forEach Not Working On Recursive Method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141280/