最近项目中需要向用户暴露一个目录,并向用户友好得展示出来,所以就选择了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);
- }
- }
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;
- }
- }
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>
<%@ 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>
在第一个页面输入要展示的目录路径:
提交后,将展示目录树结构: