<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Pencil &#187; java</title>
	<atom:link href="http://www.codepencil.com/index.php/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codepencil.com</link>
	<description>Manu Mahajan`s blog</description>
	<lastBuildDate>Tue, 14 Apr 2009 16:15:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Struts2 &#8211; Dojo Dynamic Tree</title>
		<link>http://www.codepencil.com/index.php/struts2-dojo-dynamic-tree/</link>
		<comments>http://www.codepencil.com/index.php/struts2-dojo-dynamic-tree/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 06:50:22 +0000</pubDate>
		<dc:creator>Manu Mahajan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.codepencil.com/?p=7</guid>
		<description><![CDATA[In my previous attempt at creating a tree using struts2 and dojo, I stumbled upon a roadblock. As the size of the tree grew larger the performance started deteriorating. Our tree had 500 nodes and it took about 1min 20sec for the tree to render in internet explorer 6!
So the need was to create a [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="?p=4">previous attempt</a> at creating a tree using struts2 and dojo, I stumbled upon a roadblock. As the size of the tree grew larger the performance started deteriorating. Our tree had 500 nodes and it took about <strong>1min 20sec</strong> for the tree to render in internet explorer 6!</p>
<p>So the need was to create a dynamic tree which would load nodes as and when required.</p>
<p><span id="more-7"></span></p>
<p>I managed to create a simple application based on the struts-blank application which demonstrates how this can be achieved. I am pasting the code here so that it can help other people who want to achieve this using struts 2.0.9.</p>
<h3>The JSP Code</h3>
<p>I start by creating a <em>dynamicTree.jsp</em> file which will paint the tree. </p>
<p>I will create a tree with one root node in my html page and when that node is expanded its sub nodes will be loaded. It is possible to load a tree with any number of nodes in the beginning but I am going to keep this example simple so I am starting with a single node.</p>
<p>For painting the tree tag and the root node I <em>could not</em> use the struts2 taglib as I need to use attributes for dojo widgets that are not supported by struts2. Like the attribute <em>&#8216;controller&#8217;</em> is not supported by the tree tag and <em>&#8216;isFolder&#8217;</em> is not supported by the <em>treeNode</em> tag. The next version (2.1.0) has this fixed but the latest stable version at the moment is 2.0.9.</p>
<p>Here is what my jsp looks like</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%@ page <span style="color: #000066;">contentType</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> %<span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%@ taglib <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;s&quot;</span> <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;/struts-tags&quot;</span> %<span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;html<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;head<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;s:head</span> <span style="color: #000066;">theme</span>=<span style="color: #ff0000;">&quot;ajax&quot;</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
   //Load the tree controller and extensions
  dojo.require(&quot;dojo.widget.TreeLoadingController&quot;);
  dojo.require(&quot;dojo.widget.TreeControllerExtension&quot;);
&nbsp;
  dojo.addOnLoad(function() {
    //Add the extensions to the controller
    dojo.lang.mixin(dojo.widget.byId('treeController'), dojo.widget.TreeControllerExtension.prototype);
  });
&nbsp;
}
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/head<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;body<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">dojoType</span>=<span style="color: #ff0000;">&quot;TreeLoadingController&quot;</span>
  <span style="color: #000066;">widgetId</span>=<span style="color: #ff0000;">&quot;treeController&quot;</span>
  <span style="color: #000066;">RPCUrl</span>=<span style="color: #ff0000;">&quot;tree_gettreenodes.action&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dojo:TreeSelector</span> <span style="color: #000066;">widgetId</span>=<span style="color: #ff0000;">&quot;treeSelector_contentTree&quot;</span>
  <span style="color: #000066;">eventNames</span>=<span style="color: #ff0000;">&quot;select:treeSelected;&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dojo:TreeSelector<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">dojoType</span>=<span style="color: #ff0000;">&quot;Tree&quot;</span> 
  <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contentTree&quot;</span> <span style="color: #000066;">selector</span>=<span style="color: #ff0000;">&quot;treeSelector_contentTree&quot;</span>
  <span style="color: #000066;">controller</span>=<span style="color: #ff0000;">&quot;treeController&quot;</span> <span style="color: #000066;">toggle</span>=<span style="color: #ff0000;">&quot;fade&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">dojoType</span>=<span style="color: #ff0000;">&quot;TreeNode&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;&lt;s:property value='#request.rootNode.name'/&gt;</span>&quot;
    id=&quot;<span style="color: #009900;">&lt;s:property value='#request.rootNode.id'/&gt;</span>&quot; isFolder=&quot;true&quot;&gt;<span style="color: #009900;">&lt;/div&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;/div&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;/body&gt;</span>
<span style="color: #009900;">&lt;/html&gt;</span></span></pre></div></div>

<h3>struts.xml</h3>
<p>My struts.xml is pretty simple. There is one mapping to an action class which will expose methods for painting the tree and a default result for loading the jsp.</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;struts<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.enable.DynamicMethodInvocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.devMode&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;package</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;default&quot;</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;struts-default&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tree_*&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;{1}&quot;</span> 
            <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.codepencil.sandbox.tree.web.action.DynamicTreeAction&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    	    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;result<span style="font-weight: bold; color: black;">&gt;</span></span></span>/tree/dynamicTree.jsp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/result<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/action<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/package<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/struts<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<h3>The Action Class</h3>
<p>In the action class I need to implement a method that will be called when child nodes of a tree are requested. When the request is made dojo passes a parameter name <em>data</em> which contains the JSON objects of the tree and the tree node that was clicked. The parameter looks something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;node&quot;</span>:<span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;widgetId&quot;</span>:<span style="color: #3366CC;">&quot;1&quot;</span> isFolder=\<span style="color: #3366CC;">&quot;true&quot;</span>,<span style="color: #3366CC;">&quot;objectId&quot;</span>:<span style="color: #3366CC;">&quot;&quot;</span>,<span style="color: #3366CC;">&quot;index&quot;</span>:<span style="color: #CC0000;">1</span>,<span style="color: #3366CC;">&quot;isFolder&quot;</span>:<span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;tree&quot;</span>:<span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;widgetId&quot;</span>:<span style="color: #3366CC;">&quot;contentTree&quot;</span>,<span style="color: #3366CC;">&quot;objectId&quot;</span>:<span style="color: #3366CC;">&quot;&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The treeLoadingController expects the server response to be a JSON array of <em>node</em> objects. For dealing with JSON I am using the set of Java classes available at <a href="http://www.json.org/java/">json.org</a> Since my needs are very basic I can make do with these simple classes. You can use any JSON library of your choice.</p>
<p>After this I write an action class and create a function named gettreenodes which does this job for me.</p>
<p>This is my action code.</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">codepencil</span>.<span style="color: #006600;">sandbox</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">action</span>;
&nbsp;
<span style="color: #a1a100;">import java.io.IOException;</span>
<span style="color: #a1a100;">import java.io.PrintWriter;</span>
<span style="color: #a1a100;">import java.util.List;</span>
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span>
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span>
<span style="color: #a1a100;">import org.apache.struts2.interceptor.ServletRequestAware;</span>
<span style="color: #a1a100;">import org.apache.struts2.interceptor.ServletResponseAware;</span>
<span style="color: #a1a100;">import org.apache.struts2.showcase.ajax.tree.Category;</span>
<span style="color: #a1a100;">import org.json.JSONException;</span>
<span style="color: #a1a100;">import org.json.JSONObject;</span>
<span style="color: #a1a100;">import org.json.JSONStringer;</span>
<span style="color: #a1a100;">import com.opensymphony.xwork2.ActionSupport;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DynamicTreeAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #000000; font-weight: bold;">implements</span> ServletResponseAware,ServletRequestAware <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> serialVersionUID = -9131739831820245692L;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> data;
    <span style="color: #000000; font-weight: bold;">private</span> HttpServletResponse response;
    <span style="color: #000000; font-weight: bold;">private</span> HttpServletRequest request;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> data;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setData<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> data<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">data</span> = data;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setServletResponse<span style="color: #66cc66;">&#40;</span>HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">response</span> = response;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setServletRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">request</span> = request;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> gettree<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        Category rootNode = Category.<span style="color: #006600;">getById</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
        request.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;rootNode&quot;</span>, rootNode<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">return</span> SUCCESS;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> gettreenodes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        JSONObject jsonData,node;
        <span style="color: #aaaadd; font-weight: bold;">PrintWriter</span> writer;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
            jsonData = <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span>;
            node = jsonData.<span style="color: #006600;">getJSONObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;node&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #aaaadd; font-weight: bold;">String</span> nodeId = node.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;widgetId&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            Category category = Category.<span style="color: #006600;">getById</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Long</span>.<span style="color: #006600;">parseLong</span><span style="color: #66cc66;">&#40;</span>nodeId<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            JSONStringer stringer = <span style="color: #000000; font-weight: bold;">new</span> JSONStringer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            stringer.<span style="color: #006600;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            List&lt;Category&gt; children = category.<span style="color: #006600;">getChildren</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;  
            <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i=<span style="color: #cc66cc;">0</span>;i&lt;children.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
                Category childCategory = children.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">key</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#40;</span>childCategory.<span style="color: #006600;">getId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">key</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#40;</span>childCategory.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">key</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;isFolder&quot;</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#40;</span>childCategory.<span style="color: #006600;">getChildren</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&gt;<span style="color: #cc66cc;">0</span> ? <span style="color: #000000; font-weight: bold;">true</span> : <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
                stringer.<span style="color: #006600;">endObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            stringer.<span style="color: #006600;">endArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            writer = response.<span style="color: #006600;">getWriter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            writer.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span>stringer.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>JSONException e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">IOException</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>For creating the tree I am using the Category class from the struts showcase application as I was feeling too lazy to write a brand new class for this purpose. In my actual application I have some complex hibernate mappings and I load the tree node objects from the database.</p>
<p>Another important thing that caused me a lot of frustration is that you should always return <strong>null</strong> from an action method that handles ajax calls otherwise you wont get the desired result.</p>
<h3>The Sample Application</h3>
<p>The code of the full application can be downloaded from the following link. You can simply deploy this as it is in a container. (I have tested it with Tomcat5.5 and jdk6)</p>
<p><a href="/code/struts2-dynamic-tree-application.zip">Download the Dynamic Tree Sample Application</a></p>
<p>You can use the tree loading controller for saving and restoring the state of the tree in a similar way as in the <a href="?p=4">previous example</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codepencil.com/index.php/struts2-dojo-dynamic-tree/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Creating A Tree Widget Using Struts2</title>
		<link>http://www.codepencil.com/index.php/creating-a-tree-widget-using-struts2/</link>
		<comments>http://www.codepencil.com/index.php/creating-a-tree-widget-using-struts2/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 08:45:23 +0000</pubDate>
		<dc:creator>Manu Mahajan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.codepencil.com/?p=4</guid>
		<description><![CDATA[Struts 2 includes the dojo toolkit and provides a taglib for using dojo features in a java environment. The &#60;s:tree&#62; tag can be used to generate a tree widget which can be very useful for displaying hierarchical data. For example, I recently used it in a project to display the folder structure for a filesystem.
This [...]]]></description>
			<content:encoded><![CDATA[<p>Struts 2 includes the dojo toolkit and provides a taglib for using dojo features in a java environment. The &lt;s:tree&gt; tag can be used to generate a tree widget which can be very useful for displaying hierarchical data. For example, I recently used it in a project to display the folder structure for a filesystem.</p>
<p>This is a screenshot of what the rendered tree looks like.</p>
<p><img width="256" src="http://www.codepencil.com/attachments/struts2-tree.gif" alt="The tree widget" height="251" style="width: 256px; height: 251px" title="The tree widget" /></p>
<p>Our final tree will contain the following features</p>
<ul>
<li>Ability to set a default selected node</li>
<li>Trapping node select events</li>
<li>Trapping expand/collapse events</li>
<li>Saving/restoring the state of the tree</li>
</ul>
<p><span id="more-4"></span></p>
<p>If you are of the impatient type and want a quick solution you can jump to the <a href="#summary">summary with working code.</a></p>
<p>Struts2 contains the showcase web application which provides some sample code. It can be downloaded from <a href="http://struts.apache.org/download.cgi#struts209" title="Struts2 downloads">struts2 downloads</a> page. (Download the <em>sample applications</em> package).</p>
<p>Unfortunately, some features in the tree example are broken as of now. We can use the tree example as a reference and build upon it to add more features.</p>
<p>I am discussing the presentation logic in the jsp. If you want to understand how the data structure to be displayed is created have a look at the following class in the showcase application.</p>
<p>org.apache.struts2.showcase.ajax.tree.Category</p>
<h3>Displaying the tree</h3>
<p>To begin with our jsp must contain the &lt;s:head&gt; tag inside the &lt;head&gt; section. This tag tells struts to paint the initialization code for dojo.</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;s:head</span> <span style="color: #000066;">theme</span>=<span style="color: #ff0000;">&quot;ajax&quot;</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></pre></div></div>

<p>To paint the tree we use the &lt;s:tree&gt; tag</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float:left; margin-right: 50px;&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;s:tree</span> 
    <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contentTree&quot;</span> 
    <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;contentTree&quot;</span>
    <span style="color: #000066;">theme</span>=<span style="color: #ff0000;">&quot;ajax&quot;</span>
    <span style="color: #000066;">rootNode</span>=<span style="color: #ff0000;">&quot;%{rootNode}&quot;</span> 
    <span style="color: #000066;">childCollectionProperty</span>=<span style="color: #ff0000;">&quot;children&quot;</span> 
    <span style="color: #000066;">nodeIdProperty</span>=<span style="color: #ff0000;">&quot;id&quot;</span>
    <span style="color: #000066;">nodeTitleProperty</span>=<span style="color: #ff0000;">&quot;name&quot;</span>
    <span style="color: #000066;">treeSelectedTopic</span>=<span style="color: #ff0000;">&quot;treeSelected&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/s:tree<span style="font-weight: bold; color: black;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>The &#8220;rootNode&#8221; attribute defines the data structure(Collection object) that will be displayed in the form of a tree. This needs to be set via an action. In the showcase example the ShowDynamicTreeAction.java is doing this job for us.</p>
<p><a name="capture-node-select"></a></p>
<h3>Capturing node select events</h3>
<p>In the &lt;s:tree&gt; tag above the &#8220;treeSelectedTopic&#8221; defines the dojo event topic that will be published when a node of the tree is selected by the user. </p>
<p>This however, does not work by default in struts 2.0.9. A jira ticket has been lodged for this <a href="https://issues.apache.org/struts/browse/WW-1813" target="_blank">https://issues.apache.org/struts/browse/WW-1813</a> and it will be fixed in version 2.1.0</p>
<p>To work around this for the current version download the updated version of the file <a href="http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/main/resources/template/ajax/tree.ftl?view=markup&#038;pathrev=528866" target="_blank">tree.ftl</a> from the SVN and place it in your WEB_APPLICATION_ROOT/templates/ajax directory </p>
<p>For trapping node select events make a javascript funtion that would be called when the event fires and connect it to the event by using dojo.topic.subscribe as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> treeNodeSelected<span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #009900; font-style: italic;">/*
  * Place your logic here
  * I am just displaying the id of the node that was clicked
  */</span>
             <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>message.<span style="color: #006600;">node</span>.<span style="color: #006600;">widgetId</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>;
dojo.<span style="color: #006600;">event</span>.<span style="color: #006600;">topic</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;treeSelected&quot;</span>,<span style="color: #000066; font-weight: bold;">this</span>,treeNodeSelected<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h3>Trapping expand/collapse events</h3>
<p>The TreeLoadingController widget is required for being able to capture expand and collapse events. We will also be using the tree controller extensions which will give us additional ability to control the loading of the tree.</p>
<p>First, tell dojo to load the required files</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009900; font-style: italic;">//Load the tree controller widget and controller extension</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.widget.TreeLoadingController&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.widget.TreeControllerExtension&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// Add extensions to controller.</span>
dojo.<span style="color: #006600;">lang</span>.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'treeController'</span><span style="color: #66cc66;">&#41;</span>, dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">TreeControllerExtension</span>.<span style="color: #006600;">prototype</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Also, add the tree controller widget to the body section of your page</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">dojoType</span>=<span style="color: #ff0000;">&quot;TreeLoadingController&quot;</span> <span style="color: #000066;">widgetId</span>=<span style="color: #ff0000;">&quot;treeController&quot;</span> <span style="color: #000066;">RPCUrl</span>=<span style="color: #ff0000;">&quot;local&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>Now subscribe to the expand and collapse events</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">dojo.<span style="color: #006600;">event</span>.<span style="color: #006600;">topic</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;contentTree/expand&quot;</span>,saveExpandedIndices<span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">event</span>.<span style="color: #006600;">topic</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;contentTree/collapse&quot;</span>,saveExpandedIndices<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>&#8220;saveExpandedIndices&#8221; is the name of the function that I am going to use to save the state of the tree every time a node is expanded/collapsed. You can have your own function in its place if you want.</p>
<h3>Saving the state of the tree</h3>
<p>For saving the state of the tree. The treeController widget provides a function called &#8220;saveExpandedIndices&#8221; which returns a multi dimension array of integers, each integer representing an expanded node. You can save this array based on your logic. For my implementation I am storing it inside a cookie. For converting the object to a string I use json.js which you can get from <a href='http://www.json.org/json.js'>json.org</a></p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> saveExpandedIndices<span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  indices = dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'treeController'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">saveExpandedIndices</span><span style="color: #66cc66;">&#40;</span>
    dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contentTree'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>readCookie<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;categoryTreeState&quot;</span><span style="color: #66cc66;">&#41;</span>==<span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
    createCookie<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;categoryTreeState&quot;</span>,indices.<span style="color: #006600;">toJSONString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">else</span> 
    updateCookie<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;categoryTreeState&quot;</span>,indices.<span style="color: #006600;">toJSONString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We can load the same state of the tree anytime. I use it at the body onload event so that when the user revisits that page it is in the same state.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> restoreExpandedIndices<span style="color: #66cc66;">&#40;</span>indices<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'treeController'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">restoreExpandedIndices</span><span style="color: #66cc66;">&#40;</span>
    dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contentTree'</span><span style="color: #66cc66;">&#41;</span>, indices
  <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> bodyOnLoad<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  treeState = readCookie<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;categoryTreeState&quot;</span><span style="color: #66cc66;">&#41;</span>;
  indices = treeState.<span style="color: #006600;">parseJSON</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  restoreExpandedIndices<span style="color: #66cc66;">&#40;</span>indices<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Setting the default selected element</h3>
<p>Sometime you might want to select a node of the tree on page load or at any other event. This can be accomplished by the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">treeSelector = dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'treeSelector_contentTree'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #009900; font-style: italic;">//The id will be treeSelector_TREENAME (its defined in tree.ftl)</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900; font-style: italic;">// try to select node based on ID</span>
<span style="color: #66cc66;">&#123;</span>
  treeNode = dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>ID<span style="color: #66cc66;">&#41;</span> 
  treeSelector.<span style="color: #006600;">doSelect</span><span style="color: #66cc66;">&#40;</span>treeNode<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #009900; font-style: italic;">//publish treeSelected topic to trigger handling of event</span>
  dojo.<span style="color: #006600;">event</span>.<span style="color: #006600;">topic</span>.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;treeSelected&quot;</span>, <span style="color: #66cc66;">&#123;</span>node: dojo.<span style="color: #006600;">widget</span>.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>treeSelection<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #009900; font-style: italic;">// if unable to select or node not found</span>
<span style="color: #66cc66;">&#123;</span>
  treeNode = tree.<span style="color: #006600;">getDescendants</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #009900; font-style: italic;">// try selecting the root node</span>
  treeSelector.<span style="color: #006600;">doSelect</span><span style="color: #66cc66;">&#40;</span>treeNode<span style="color: #66cc66;">&#41;</span>;
  dojo.<span style="color: #006600;">event</span>.<span style="color: #006600;">topic</span>.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;treeSelected&quot;</span>, <span style="color: #66cc66;">&#123;</span>node: tree.<span style="color: #006600;">getDescendants</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Summarizing</h3>
<p><a name="summary"></a></p>
<p>Here is a summary of the code snippets put together which you can directly use in your application.</p>

<div class="wp_syntax"><div class="code"><pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;html<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;head<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;s:head</span> <span style="color: #000066;">theme</span>=<span style="color: #ff0000;">&quot;ajax&quot;</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;Javascript&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/scripts/json.js&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;Javascript&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/scripts/cookies.js&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;javascript&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
&nbsp;
  //The following extensions are required to save or restore state of a tree
  dojo.require(&quot;dojo.widget.TreeLoadingController&quot;);
  dojo.require(&quot;dojo.widget.TreeControllerExtension&quot;);
&nbsp;
  //This function is called after dojo scripts are loaded
  dojo.addOnLoad(function() {
    dojo.lang.mixin(dojo.widget.byId('treeController'), dojo.widget.TreeControllerExtension.prototype);
    //The following code will make sure that the saveExpandedIndices function is called everytime a node is collapsed or expanded
    dojo.event.topic.subscribe(&quot;contentTree/expand&quot;,saveExpandedIndices);
    dojo.event.topic.subscribe(&quot;contentTree/collapse&quot;,saveExpandedIndices);
  });
&nbsp;
  //The following function saves the state of the tree
  function saveExpandedIndices(message) {
    indices = dojo.widget.byId('treeController').saveExpandedIndices(
    dojo.widget.byId('contentTree')
  );
  if (readCookie(&quot;categoryTreeState&quot;)==null)
    createCookie(&quot;categoryTreeState&quot;,indices.toJSONString());
  else 
    updateCookie(&quot;categoryTreeState&quot;,indices.toJSONString());
  }
&nbsp;
  //The following function restores the state of the tree. This can probably be called body-onload
  //You will have to pass it the stored indices object though
  function restoreExpandedIndices(indices) {
    dojo.widget.byId('treeController').restoreExpandedIndices(
    dojo.widget.byId('contentTree'), indices
    );
  }
function bodyOnLoad(){
  treeState = readCookie(&quot;categoryTreeState&quot;);
  indices = treeState.parseJSON();
  restoreExpandedIndices(indices);
}
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/head<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;body</span> <span style="color: #000066;">onload</span>=<span style="color: #ff0000;">&quot;bodyOnLoad()&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">dojoType</span>=<span style="color: #ff0000;">&quot;TreeLoadingController&quot;</span> <span style="color: #000066;">widgetId</span>=<span style="color: #ff0000;">&quot;treeController&quot;</span> <span style="color: #000066;">RPCUrl</span>=<span style="color: #ff0000;">&quot;local&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;s:tree</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contentTree&quot;</span> 
    <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;contentTree&quot;</span>
    theme = <span style="color: #ff0000;">&quot;ajax&quot;</span>
    <span style="color: #000066;">rootNode</span>=<span style="color: #ff0000;">&quot;%{rootNode}&quot;</span>
    <span style="color: #000066;">childCollectionProperty</span>=<span style="color: #ff0000;">&quot;children&quot;</span>
    <span style="color: #000066;">nodeIdProperty</span>=<span style="color: #ff0000;">&quot;id&quot;</span>
    <span style="color: #000066;">nodeTitleProperty</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/s:tree<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/body<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p><del datetime="2007-09-19T10:43:31+00:00">I am working on a tree with lazy loading that will load partially at first and the rest will be loaded based on when the user expands a node. I will post the code when I get it working!</del></p>
<hr />
<h4>Follow Ups:</h4>
<p>&bull; <a href="/?p=7">Struts2 &#8211; Dojo Dynamic Tree</a> : In this example I have attempted to create a tree which loads partially at first and rest is loaded as and when requested by the user.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codepencil.com/index.php/creating-a-tree-widget-using-struts2/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
