JSF/Seam -- Nested/Recursive iteration
I've got a situation where I would like to perform nested iterations on a series of options and display the results using JSF. As an example, think of a discussion board. Each comment may contain a parent comment and 0-n child comments. So, my root object would be something like this:
public class MyObject {
...
public List<Comment> getComments();
...
}
And my comments would look like this:
public class Comment 开发者_Go百科{
...
public Comment getParent();
...
public List<Comment> getChildComments();
...
}
I've got all of my entities setup using JPA and they are populated within the database. But, my dilema is, how do I iterate over each comment so I can display its child comments inline? I know I can use <ui:repeat value="#{myObj}" var="comment" />
to iterate over the root-level comments for an object. But, how do I then iterate over the children comments for each of those? And then, how do I iterate over their child comments. And so on, and so on.
Has anyone ever done anything like this? I suppose "Nested Iteration" could really be thought of more as "recursive iteration". Thoughts?
For fixed-size nested iterations, simply use:
<ui:repeat value="#{myObj}" var="comment">
<ui:repeat value="#{comment}" var="subComment">
<ui:repeat value="#{subComment}" var="subSubComment">
</ui:repeat>
</ui:repeat>
</ui:repeat>
If you want full recursion, fetch the data in some Tree structure in your bean, using whatever loops you like, and use it just for visualization, in a simple iteration. You may need to have a "level" property of the objects that you put in your Tree.
精彩评论