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.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插件中的东西
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。