<s:tree /> 태그를 이용해서 트리를 구현하는 예제를 작성합니다.

결과는 다음과 같습니다.



[TreeNode.java: 모델 클래스]

package example.chapter6;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    private String id;
    private String name;
    private List children = new ArrayList();
 
    public TreeNode() {}
 
    public TreeNode(String id, String name, TreeNode... children) {
        this.id = id;
        this.name = name;
        this.children = new ArrayList();
        for (TreeNode child : children) {
            this.children.add(child);
        }
    }

    public List getChildren() {return children;}
    public void setChildren(List children) {this.children = children;}
    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
}


[TreeSampleAction.java : 액션 클래스]

package example.chapter6;

import com.opensymphony.xwork2.ActionSupport;

public class TreeSampleAction extends ActionSupport {
    private TreeNode nodeRoot;
 
    public String execute() throws Exception {

        nodeRoot = new TreeNode("00000", "00000",
                                      new TreeNode("10000", "10000",
                                                 new TreeNode("11000", "11000"),
                                                 new TreeNode("12000", "12000")),
     
                                      new TreeNode("20000", "20000",
                                                 new TreeNode("21000", "21000"),
                                                 new TreeNode("22000", "22000"))
                            );
     
         return "success";
    }

    public TreeNode getNodeRoot() {return nodeRoot;}
    public void setNodeRoot(TreeNode nodeRoot) {this.nodeRoot = nodeRoot;}
}


[treeSample.jsp : JSP]

<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
    <title>tree sample</title>
    <s:head theme="ajax" debug="true" />
</head>

<body>
    <s:tree
           theme="ajax"
           rootNode="%{nodeRoot}"
           childCollectionProperty="children"
           nodeIdProperty="id"
           nodeTitleProperty="name"
           templateCssPath="style/tree.css" toggle="fade"  />
</body>
</html>

[struts.xml : 설정파일]
...
  <action name="treeSample" class="example.chapter6.TreeSampleAction">
      <result>/chapter6/treeSample.jsp</result>
  </action>
...

AND