Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:48:15+00:00 2026-05-16T06:48:15+00:00

I am trying to build a navigation tree via recursion in JSF. I have

  • 0

I am trying to build a navigation tree via recursion in JSF. I have defined a navigationNode component as:

<composite:interface>
    <composite:attribute name="node" />
</composite:interface>

<composite:implementation>
<ul>
    <ui:repeat value="#{navigationTreeBean.getChildrenForNode(cc.attrs.node)}" var="child">
        <li><navigation:navigationNode node="#{child}" /></li>
    </ui:repeat>
</ul>
</composite:implementation>

My tree is declared as:

rootNode = new DefaultMutableTreeNode(new NodeData("Dashboard", "dashboard.xhtml"), true);
DefaultMutableTreeNode configurationsNode = new DefaultMutableTreeNode(new NodeData("Configurations", "configurations.xhtml"), true);
rootNode.add(configurationsNode);

I call component by:

<nav:navigationNode node="#{rootNode}" />

The problem is, this results in StackOverflowError.

There are a few references to building recursion in JSF (for example, c:forEach vs ui:repeat in Facelets). The common problem seems to be mixing the build-time and render-time components/tags. In my case:

  • My composite component is actually a tag, which is executed when the tree is built
  • ui:repeat is an actual JSF component, which is evaluated when the tree is rendered

Is the child component navigation:navigationNode actually processed before the ui:repeat component? If so, what object is it using for #{child}? Is it null (doesn’t seem so)? Is the problem here that the child component is actually created without even caring about the ui:repeat and so each time a new child component is created even though it is not necessarily wanted?

The c:forEach vs ui:repeat in Facelets article has a separate section for this (recursion). The suggestion is to to use c:forEach instead. I tried this, however it is still giving me the same StackOverflowError, with different trace that I cannot make sense of.

I know that we can also build components by extending UIComponent, but that approach (writing html in Java code) seems ugly. I would rather use MVC style / templates. However, if there are no other ways, do I have to implement this sort of recursion as UIComponent instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:48:16+00:00Added an answer on May 16, 2026 at 6:48 am

    JSF’s built-in declarative tags are ill-suited for handling this sort of recursion. JSF builds a stateful component tree that is persisted between requests. If the view is restored in a subsequent request, the view state may not reflect changes in the model.

    I would favour an imperative approach. You have two options as I see it:

    • Use the binding attribute to bind a control (e.g. some form of panel) to a backing bean that provides the UIComponent instance and its children – you write code to instantiate the UIComponent and add whatever children you want. See the spec for the binding attribute contract.
    • Write a custom control, implementing some of: a UIComponent; a Renderer; a tag handler; meta-data files (delete as appropriate – you do some or all of these depending on what you are doing and how and in which version of JSF).

    Perhaps another option is to pick up a 3rd party control that already does this.

    UPDATE:
    If one is using the very useful OmniFaces library (you should if you don’t already), there is the <o:tree> which has no html generation whatsoever but was specifically designed to support usecases like this.

    <o:tree value="#{bean.treeModel}" var="item" varNode="node">
        <o:treeNode>
            <ul>
                <o:treeNodeItem>
                    <li>
                        #{node.index} #{item.someProperty}
                        <o:treeInsertChildren />
                    </li>
                </o:treeNodeItem>
            </ul>
        </o:treeNode>
    </o:tree>
    

    EDIT:

    Here’s a model-driven approach that doesn’t involve writing custom components or backing-bean-generated component trees. It’s kind of ugly.

    The Facelets view:

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:ui="http://java.sun.com/jsf/facelets">
      <h:head><title>Facelet Tree</title></h:head>
      <h:body>
        <ul>
          <ui:repeat value="#{tree.treeNodes}" var="node">
            <h:outputText rendered="#{node.firstChild}"
                    value="&lt;ul&gt;" escape="false" />
            <li>
              <h:outputText value="#{node.value}" />
            </li>
            <ui:repeat rendered="#{node.lastChild and empty node.kids}"
                value="#{node.lastChildLineage}" var="ignore">
              <h:outputText
                  value="&lt;/ul&gt;" escape="false" />
            </ui:repeat>
          </ui:repeat>
        </ul>
      </h:body>
    </html>
    

    The managed bean:

    @javax.faces.bean.ManagedBean(name = "tree")
    @javax.faces.bean.RequestScoped
    public class Tree {
      private Node<String> root = new Node(null, "JSF Stuff");
    
      @PostConstruct
      public void initData() {
        root.getKids().add(new Node(root, "Chapter One"));
        root.getKids().add(new Node(root, "Chapter Two"));
        root.getKids().add(new Node(root, "Chapter Three"));
        Node<String> chapter2 = root.getKids().get(1);
        chapter2.getKids().add(new Node(chapter2, "Section A"));
        chapter2.getKids().add(new Node(chapter2, "Section B"));
      }
    
      public List<Node<String>> getTreeNodes() {
        return walk(new ArrayList<Node<String>>(), root);
      }
    
      private List<Node<String>> walk(List<Node<String>> list, Node<String> node) {
        list.add(node);
        for(Node<String> kid : node.getKids()) {
          walk(list, kid);
        }
        return list;
      }
    }
    

    A tree node:

    public class Node<T> {
      private T value;
      private Node<T> parent;
      private LinkedList<Node<T>> kids = new LinkedList<>();
    
      public Node(Node<T> parent, T value) {
        this.parent = parent;
        this.value = value;
      }
    
      public List<Node<T>> getKids() {return kids;}
      public T getValue() { return value; }
    
      public boolean getHasParent() { return parent != null; }
    
      public boolean isFirstChild() {
        return parent != null && parent.kids.peekFirst() == this;
      }
    
      public boolean isLastChild() {
        return parent != null && parent.kids.peekLast() == this;
      }
    
      public List<Node> getLastChildLineage() {
        Node node = this;
        List<Node> lineage = new ArrayList<>();
        while(node.isLastChild()) {
            lineage.add(node);
            node = node.parent;
        }
        return lineage;
      }
    }
    

    Output:

    *  JSF Stuff
          o Chapter One
          o Chapter Two
                + Section A
                + Section B 
          o Chapter Three 
    

    I would still bite the bullet and write a custom tree control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.