dhtmlXTree动态加载struts2中action的xml数据

本文介绍如何使用Struts2框架配置和实现XML格式的树状数据生成。通过具体的代码示例,展示了从struts2.xml配置到action处理过程,再到前端页面展示的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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();
	}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值