struts2.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="example" namespace="/example" extends="struts-default">
<action name="getCarTypeXML" method="getCarTypeXML" class="com.iss.action.CarTypeAction">
<result name="xmlMessage" type="plaintext"></result>
</action>
</package>
</struts>
index.jsp,加載樹的頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>UTF-8</title>
</head>
<body>
<link rel="STYLESHEET" type="text/css" href="util/css/dhtmlxtree.css">
<script src="util/js/dhtmlxcommon.js"></script>
<script src="util/js/dhtmlxtree.js"></script>
<div id="treeboxbox_tree" style="width:300px; height:200px;background-color:#f5f5f5;border :1px solid Silver; "/>
<script>
tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
tree.setImagePath("util/img/csh_bluebooks/");
tree.enableCheckBoxes(0);
tree.enableDragAndDrop(0);
tree.loadXML("<%=basePath%>/example/getCarTypeXML.action");
</script>
</body>
</html>
struts2中action代碼:
public void getCarTypeXML() throws Exception {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("tree");
root.addAttribute("id", "0");
Element item = root.addElement("item");
item.addAttribute("text", "我的工作臺");
item.addAttribute("id", "work console");
item.addAttribute("im0", "folderClosed.gif");
item.addAttribute("im1", "folderOpen.gif");
item.addAttribute("im2", "folderClosed.gif");
Element subItem = item.addElement("item");
subItem.addAttribute("text", "代辦事宜");
subItem.addAttribute("id", "working");
subItem.addAttribute("im0", "book_titel.gif");
subItem.addAttribute("im1", "fbook.gif");
subItem.addAttribute("im2", "book_titel.gif");
subItem = item.addElement("item");
subItem.addAttribute("text", "代辦事宜2");
subItem.addAttribute("id", "working");
subItem.addAttribute("im0", "book_titel.gif");
subItem.addAttribute("im1", "fbook.gif");
subItem.addAttribute("im2", "book_titel.gif");
String content = document.asXML();
System.out.println(content);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter pw=response.getWriter();
pw.write(content);
pw.flush();
pw.close();
}