日期:2014-05-16  浏览次数:20602 次

dhtmlxTree+struts2实现简单的动态树形菜单
页面
<%@ 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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- 配置插件 -->
<link rel="stylesheet" type="text/css" href="dhtmlxtree.css">
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript" src="dhtmlxcommon.js"></script>
<script type="text/javascript" src="dhtmlxtree.js"></script>
<script type="text/javascript">
	function init() {
			//获取要显示的位置
			var divTree = document.getElementById("divTree");
			//建立tree树形控件的对象
			//设置现实的位置,占的大小,数的根节点,以ID进行查询
			tree = new dhtmlXTreeObject(divTree, "100%", "100%", 0);
			//选取显示的图片
			tree.setImagePath("imgs/csh_winstyle/");
			//加载xml文件----
			//发送请求获取动态的数据
			tree.loadXML("<%=path%>/getXml/tree");
	}
</script>
</head>

<body>
	<input type="button" id="bt" value="产生树" onclick="init()" />
	<div style="width: 300px;height: 500px" id="divTree"></div>
</body>
</html>
action请求
package action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import model.Board;
import model.Tree;

import com.opensymphony.xwork2.ActionSupport;
import com.thoughtworks.xstream.XStream;



public class XmlAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5576292360270369489L;

	public void getXml() throws IOException {
		Tree tree = new Tree();
		tree.setId(0);

		Board board = new Board();
		board.setBoardId(1);
		board.setBoardName("CSDN论坛");
		board.setParentId(0);

		// 第一个主板块
		Board java = new Board();
		// 主板块parentid = 0
		java.setBoardId(2);
		java.setParentId(1);
		java.setBoardName("java");
		// 子版块
		Board jsp = new Board();
		jsp.setParentId(1);
		jsp.setBoardId(3);
		jsp.setBoardName("jsp");

		Board ssh = new Board();
		ssh.setParentId(1);
		ssh.setBoardId(4);
		ssh.setBoardName("ssh");

		// 主板块添加子版块
		java.getBoards().add(jsp);
		java.getBoards().add(ssh);

		// 第二个主板块
		Board net = new Board();
		net.setParentId(0);
		net.setBoardId(5);
		net.setBoardName(".net");

		// 子版块
		Board mvc = new Board();
		mvc.setParentId(5);
		mvc.setBoardId(6);
		mvc.setBoardName("mvc");

		Board wcf = new Board();
		wcf.setParentId(5);
		wcf.setBoardId(7);
		wcf.setBoardName("wcf");

		// 主板块添加子版块
		net.getBoards().add(mvc);
		net.getBoards().add(wcf);

		board.getBoards().add(java);
		board.getBoards().add(net);

		tree.setBoard(board);

		XStream xstream = new XStream();

		xstream.alias("tree", Tree.class);
		xstream.alias("item", Board.cla