利用dTree展示目录结构

最近项目中需要向用户暴露一个目录,并向用户友好得展示出来,所以就选择了dTree这个js框架,地址:

http://www.destroydrop.com/javascripts/tree/dtree.zip

这里用struts2结合dtree展示目录结构,部分代码如下:

DtreeUtil.java

 

package com.coship.util;

import java.io.File;

public class DtreeUtil {
	private static int globalNum = 1;

	private static final int ROOT_ID = 0;

	private static final int ROOT_PID = -1;

	private static final String FILE_SEPARATOR = "/";

	private static final String LINE_BREAK = "\n";

	public static String getDtreeCode(String rootDirPath) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("d = new dTree('d');" + LINE_BREAK);
		buffer.append("d.add(" + ROOT_ID + "," + ROOT_PID + ",'" + rootDirPath
				+ "','" + rootDirPath + "');" + LINE_BREAK);
		DtreeUtil.recursive(rootDirPath, ROOT_ID, buffer);
		buffer.append("document.write(d);" + LINE_BREAK);
		String dTreeCode = buffer.toString();

		return dTreeCode;
	}

	private static void recursive(String dirPath, int pid, StringBuffer buffer) {
		File curDir = new File(dirPath);

		String[] nameOfFiles = curDir.list();

		for (String nameOfFile : nameOfFiles) {
			String curFilePath = dirPath + FILE_SEPARATOR + nameOfFile;
			int id = globalNum++;
			File curFile = new File(curFilePath);
			buffer.append("d.add(" + id + "," + pid + ",'" + nameOfFile + "','"
					+ curFilePath + "');" + LINE_BREAK);
			if (curFile.isDirectory()) {
				DtreeUtil.recursive(curFilePath, id, buffer);
			}
		}
	}

	public static void main(String[] args) {
		String rootDirPath = "E:/study";
		String dTreeCode = DtreeUtil.getDtreeCode(rootDirPath);
		System.out.println(dTreeCode);
	}
}

 

DtreeAction.java

 

package com.coship.action;

import com.coship.util.DtreeUtil;
import com.opensymphony.xwork2.ActionSupport;

public class DtreeAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private String path;

	private String dTreeCode;

	public String getdTreeCode() {
		return dTreeCode;
	}

	public void setdTreeCode(String dTreeCode) {
		this.dTreeCode = dTreeCode;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String dtree() {
		this.dTreeCode = DtreeUtil.getDtreeCode(this.path);
		return SUCCESS;
	}

}

 

dTree.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dtree</title>
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
</head>
<body>
<div class="dtree">

<p><a href="javascript: d.openAll();">open all</a> | <a
	href="javascript: d.closeAll();">close all</a></p>

<script type="text/javascript">
<!--
	<s:property value="dTreeCode" />
//-->
</script></div>
</body>
</html>

 在第一个页面输入要展示的目录路径:



 提交后,将展示目录树结构:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值