Home >> Struts
Rendering DOJO Tree Widget using Struts2 Plugin for DOJO
I hereby will try to quickly discuss an example with Tree widget
using Struts2 Plugin for DOJO, along with a demo action and
dynamically populating Tree Node on click of the + sign.
This example uses AJAX way of rendering/populating dynamic
runtime values from the view JSP, thus making this example
more useful in comparison to my earlier example, where
I just showed a possible way to render a Tree widget by
some statically coded TreeNodes.
Various pieces of this example includes required JAR files
from the Struts 2.1.8 distribution (I have used this version
of Struts 2 in my local example setup environment), simple
web application setup with a struts.xml file, web.xml file
and two JSP files and a Action class .
My example folder and file structure is as follows:
|
|  |
|
example
|
|_____ sample.jsp (Main JSP file)
|_____ sampletree.jsp (Suppliment file for providing JSON string
| based on nodeId from request parameter)
|
|_____ WEB-INF
|
|_____lib
|____ commons-fileupload-1.2.1.jar
|____ commons-io-1.3.2.jar
|____ commons-logging-1.1.jar
|____ freemarker-2.3.13.jar
|____ ognl-2.6.11.jar
|____ struts2-core-2.1.8.1.jar
|____ struts2-dojo-plugin-2.1.8.1.jar
|____ xwork-core-2.1.6.jar
|
|_____ web.xml
|
|_____ classes
|
|_____ struts.xml
|_____ example
|
|_____ TreeRenderAction.java (Action class file)
|
After setting up all these files, I tested this example by a URL
on browser as follows:
http://<machine_ip>:port/example/sample.jsp
Let us explore all the files created for this example :
sample.jsp
<%@ taglib uri="/struts-dojo-tags" prefix="s2d"%>
<html>
<head><title></title>
<s2d:head cache="true"/>
</head>
<body>
<div>
<s2d:tree id="tree" href="example/tree-example"/>
</div>
</body>
</html>
|
sampletree.jsp
<%
String nodeId = (String)request.getParameter("nodeId");
System.out.println("node id:"+nodeId);
%>
<% if(nodeId != null && nodeId.equals("books")) { %>
[ { label: "Java Books", hasChildren: true, id: "JavaBooks" } ]
<% } else if (nodeId != null && nodeId.equals("JavaBooks")) { %>
[ { label: "Threading Books", hasChildren: false, id: "ThreadBooks" } ,
{ label: "Networking Books", hasChildren: false, id: "NetworkingBooks" } ,
{ label: "GUI Books", hasChildren: false, id: "GUIBooks" } ]
<% } else {%>
[ { label: "Books", hasChildren: true, id: "books" } ]
<% } %>
|
As this sampletree.jsp file is receiving the request with default
parameter as nodeId with a value as the id of the node that is
clicked on screen/browser Tree widget.
So depending on the id, appropriate sub/child tree node per se
are supplied as response in form of JSON string array.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Struts2 and DOJO example</display-name>
<filter>
<filter-name>sample-filter</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sample-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="dojo-tree-example" namespace="/example"
extends="struts-default">
<action name="tree-example" class="example.TreeRenderAction"
method="preview">
<result name="preview">/sampletree.jsp</result>
</action>
</package>
</struts>
|
The href attribute of the tree widget tag from Struts2 Plugin
has the namespace and action as depicted in bold letters in the
above sample.jsp file, as mentioned here as well "
example/tree-example"
So on click of the node, this action is called and it goes through the
preview method and then to the sampletree.jsp file as mentioned in the
result value in the struts.xml configuration file.
TreeRenderAction.java
package example;
public class TreeRenderAction {
public String preview() {
return "preview";
}
}
|
Hope this helps.
If anything missed out , please let me know at
techienjoy at yahoo . com