dhtmlxTree+struts2实现简单的动态树形菜单

来源:互联网 发布:淘宝网注册账户 编辑:程序博客网 时间:2024/06/08 08:26
页面<%@ 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 = 0java.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.class);xstream.aliasAttribute(Tree.class, "id", "id");xstream.aliasAttribute(Tree.class, "board", "item");xstream.aliasAttribute(Board.class, "boardId", "id");xstream.aliasAttribute(Board.class, "boardName", "text");xstream.aliasAttribute(Board.class, "parentId", "parentId");xstream.aliasAttribute(Board.class, "boards", "item");xstream.addImplicitCollection(Board.class, "boards");String xml = xstream.toXML(tree);System.out.println(xml);HttpServletResponse response = ServletActionContext.getResponse();PrintWriter out = response.getWriter();out.print(xml);out.flush();out.close();}}

struts.xml配置文件<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="getXml" namespace="/getXml" extends="struts-default"><action name="tree" class="action.XmlAction" method="getXml"></action></package></struts>

model实体类package model;import java.util.ArrayList;import java.util.List;public class Board {private int boardId = 1;private String boardName = "Jsp";private int parentId = 0;private List<Board> boards = new ArrayList<Board>();public int getBoardId() {return boardId;}public void setBoardId(int boardId) {this.boardId = boardId;}public String getBoardName() {return boardName;}public void setBoardName(String boardName) {this.boardName = boardName;}public int getParentId() {return parentId;}public void setParentId(int parentId) {this.parentId = parentId;}public List<Board> getBoards() {return boards;}public void setBoards(List<Board> boards) {this.boards = boards;}}

中间实体类用来当作根节点package model;public class Tree {private Integer id;private Board board;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Board getBoard() {return board;}public void setBoard(Board board) {this.board = board;}}

红框中的是dhtmlxTree插件中的东西
1 0
原创粉丝点击