6
votes

I have a recursive object bean structure, which is like

list of Master DTO -> value -> list of Master DTO

I am trying to create a recursive composite component with one input text and a button with value binded to value field. I am calling the same component to build sub types but it gives me stack overflow error.

I tried with rendered attribute in the encompassing panel based on if list is empty but it does not work. I tried to wrap the call to composite component (from within composite component) in c:if but it does not work.

I always get a StackOverflowError.

Any help on how to build recursive composite components would be help. Thanks for your time!

1

1 Answers

8
votes

I always get a StackOverflowError

Then you've surely arrived at the right site now ;)

Jokes aside, composite components unfortunately do not support recursion. At my work we recently worked on a component that had similar requirements, and the way we solved it was to replace direct recursion with several Java based components that can be placed on a composite component to declare the recursive structure.

I can't give the actual code as it's copyrighted by my company, but the following is an example of how it could be used:

<jcf:recursion value="#{foo.someData}" var="item">

    <jcf:recursionNode level="0">
        <jcf:recursionNodeItem rendered="#{foo.someCondition}">
            <!-- Some markup possibly referencing item -->
                <jcf:insertRecursionNodeChildren />
            <!-- Some more markup  -->
        </jcf:recursionNodeItem>
    </jcf:recursionNode>

    <jcf:recursionNode>
        <!-- Some markup  -->
        <jcf:recursionNodeItem rendered="#{foo.someOtherCondition}">
            <!-- Some more markup  -->
                <jcf:insertRecursionNodeChildren />
            <!-- Some more markup  --> 
        </jcf:recursionNodeItem>
        <!-- Some more markup  -->
    </jcf:recursionNode>

</jcf:recursion>

The idea is that for each iteration of the recursion, one can define explicit markup and instructions. Shown in the example are instructions for the first level. If no level is defined, the instructions are for every level in the recursion that does not already have an explicit level defined. Nodes denote new levels, while NodeItems represent all items on a given level.

The parent Java based component then starts the actual recursion in Java code and delegates to its children for the rendering.

Hopefully this can get you going.