页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'index.jsp' starting page
<script type="text/java script" src="jquery-1.10.2.js">
<script type="text/java script" src="dhtmlxcommon.js">
<script type="text/java script" src="dhtmlxtree.js">
<script type="text/java script">
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");
}
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配置文件
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
boards = new ArrayList
(); public int getBo