Struts 2: Using the s:tree tag

Mar 20 2009

It took me a bit of effort to figure out how to use the <s:tree> tag properly, but once I figured it out, my code was minimal.

Static Content

This is simple enough:


<s:tree id="..." label="..." theme="ajax">
<s:treenode id="..." label="..." theme="ajax" />
<s:treenode id="..." label="..." theme="ajax">
<s:treenode id="..." label="..." theme="ajax" />
</s:treenode>
</s:tree>

Dynamic Content

For this to work, a couple things needs to happen:

  1. You must not define the label property.
  2. You must define the rootNode property.

You can use the childCollectionProperty to specify the property of a node that defines its children. The nodeTitleProperty and nodeIdProperty specify the properties of a node that define its title and id, respectively. Here is an example:


<s:tree id="..." theme="ajax" rootNode="myRootNode" nodeIdProperty="id" nodeTitleProperty="name" childCollectionProperty="children" />

If you setup your node objects properly, you can create an entire tree structure using just this one tag.

- Phillip Singer, Senior Developer