Home >> Struts
There could be many ways of creating Tree structure while working
with DOJO, Struts2 Plugin for DOJO.
In this example I shall try to cover a very simple way of putting
various pieces of code together to come up with a Tree Layout for
displaying set of Books as a case study.
In this example this particular Tree structure will be formed out
of a dynamically populated object at server side.
This object will have a parent-child association with multiple instances
of the same class as child to the root node.
This means, if Book is a case study class in this example, then it
can have other Book instances as child stored inside the parent Book
object with a data type as List.
Book.java
package example;
import java.util.List;
public class Book
{
private String bookId;
private String bookName;
private List<Book> booksList;
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookId() {
return this.bookId;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookName() {
return this.bookName;
}
public void setBooksList(List booksList) {
this.booksList = booksList;
}
public List<Book> getBooksList() {
return this.booksList;
}
}
|
Now, the actual Tree widget values will be mapped to this Book instance
that is created within a method of this example Action class, as follows:
BooksTreeAction.java
package example;
import java.util.List;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class BooksTreeAction extends ActionSupport
{
public Book getBooksTree() {
Book bk = new Book();
bk.setBookId("BK001");
bk.setBookName("Java Books");
Book bk1 = new Book();
bk1.setBookId("C001");
bk1.setBookName("Books");
List list = new ArrayList();
list.add(bk);
bk1.setBooksList(list);
return bk1;
}
}
|
In order to make this example work, it is needed to complete
action mapping in struts.xml file and writing the appropriate
JSP file as mentioned in ths action result.
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-example1" namespace="/example1"
extends="struts-default">
<action name="tree-example1" class="example.BooksTreeAction">
<result>/sample.jsp</result>
</action>
</package>
</struts>
|
and the
sample.jsp file
<%@ taglib uri="/struts-dojo-tags" prefix="s2d"%>
<html>
<head><title></title>
<s2d:head cache="true"/>
</head>
<body>
<div>
<s2d:tree id="treeOfBooks" rootNode="%{booksTree}"
nodeIdProperty="bookId" nodeTitleProperty="bookName"
childCollectionProperty="booksList"/>
</div>
</body>
</html>
|
This rootNode (value of this attribute is booksTree) should be inline
with the getter method name in the action mapping which is getBooksTree
in this case. Other attributes of the tree tag are pretty straight
forward to co-relate.
In order to test this example, the URL is as follows:
http://localhost:8080/struts2-and-dojo/example1/tree-example1
where localhost being the local machine and 8080 being the Tomcat
web server listening port for this example's runtime environment.
example1 being the namespace and tree-example1 being the action
name.
Hope this helps.
If anything missed out , please let me know at
techienjoy at yahoo . com