jsp

位置:IT落伍者 >> jsp >> 浏览文章

JSP页面文件目录树源码(递归算法)


发布日期:2018年06月23日
 
JSP页面文件目录树源码(递归算法)

<%@ page contentType="text/html; charset=gb" language="java" import="javasql*" errorPage="" %>

<html>

<head>

<title></title>

<meta httpequiv="ContentType" content="text/html;charset=gb"; >

<script language="JavaScript">

<!

function MM_goToURL() { //v

var i args=MM_goToURLarguments; documentMM_returnValue = false;

for (i=; i<(argslength); i+=) eval(args[i]+"location="+args[i+]+"");

}

//>

</script>

<link href="css/ipcss" rel="stylesheet" type="text/css">

</head>

<body topmargin = "" leftmargin = "" marginheight = "" marginwidth = "" bgcolor="#FFFFFF">

<table border="" cellspacing="" cellpadding="" height="%">

<tr><td>

</td>

</tr>

<tr valign="top">

<td height=""> <span id="menus"></span>

<span id="view"></span>

</td>

</tr>

<tr valign="bottom">

<td>&nbsp;</td>

</tr>

</table>

<div align="right"></div>

</body>

<script>

/**

* 构造树初值为

*/

function isbrother()

{

var is=flase;

}

function tree(n) {

var id = new Array("bar""pad""<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>""<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>""<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>");

if(n == ) { // 初始化变量

n = ;

i = ;

s = "";

}

s += "<table border= cellspacing= cellpadding= >";

for(;i<tree_arlength;i++) {

var k = (n >= tree_ar[i+][])?:;

if(tree_ar[i][]==false)

{

id[]="<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>";

}

s += "<tr id="+id[k]+" value="+i+"><td >"+id[k+]+"</td><td>"+tree_ar[i][]+"</td></tr>"; // // 构造节点注意这里的自定义属性value作用是简化构造节点的描述共享参数数组信息

if(n > tree_ar[i+][]) { // 若期望层次大于当前层次结束本层次返回上一层次

s += "</td></tr></table>";

return tree_ar[i+][];

}

if(n < tree_ar[i+][]) { // 若期望层次小于当前层次递归进入下一层次

s += "<tr style=display:none ><td ";

if(tree_ar[i][]==true)

{ //如果有下个兄弟节点则画兄弟节点间的连线

s+="background=images/linegif border= align=absmiddle";

s+="></td><td>";

var m = tree(tree_ar[++i][]);

s += "</td></tr>";

if(m < n) { // 当递归返回值小于当前层次期望值时将产生连续的返回动作

s += "</table>";

return m;

}

}

}

s += "</table>";

return s;

}

</script>

<script for=pad event=onclick>

// 分枝节点的点击响应

v = thisparentElementrows[thisrowIndex+]style;

if(vdisplay == block) {

vdisplay = none;

thiscells[]innerHTML = "<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>";

viewinnerHTML = "";// 自行修改为参数数组定义的闭合动作

}else {

vdisplay = block;

thiscells[]innerHTML = "<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>";

// viewinnerHTML = "<b>"+tree_ar[thisvalue][]+"</b>"; // 自行修改为参数数组定义的展开动作

/**

* 以下代码用于关闭已展开的其他分枝

* 如需自行关闭展开的分枝则从这里直接返回或删去这段代码

*/

if(! tree_ar[thisvalue]type) //如该节点为首次进入则记录所在层次信息

genTreeInfo(this);

var n = *thisvalue+;

for(i=n;i<tree_arlength;i++) { // 关闭排列在当前节点之后的树

if(tree_ar[i]type == "pad") {

tree_ar[i]objstyledisplay = none;

tree_ar[i]objcells[]innerHTML = "<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>";

}

}

while(tree_ar[n][] > ); //回溯到当前树的起点

while(n >= ) // 关闭排列在当前树的起点之前的树

if(tree_ar[n]type == "pad") {

tree_ar[n]objstyledisplay = none;

tree_ar[n]objcells[]innerHTML = "<IMG src=border= align=absmiddle><IMG src=border= align=absmiddle>";

/** 记录层次信息用以简化遍历树时的复杂的节点描述 **/

function genTreeInfo(o) {

var el = oparentElement;

for(var i=;i<elrowslength;i++) {

if(elrows[i]id != "") {

tree_ar[elrows[i]value]type = elrows[i]id;

}

if(elrows[i]id == "pad") {

tree_ar[elrows[i]value]obj = elrows[i];

tree_ar[elrows[i]value]obj = elrows[i+];

}

}

}

</script>

<script for=bar event=onclick>

// 无分枝节点的点击响应

//viewinnerHTML = "<b>"+tree_ar[thisvalue][]+"</b>"; // // 自行修改为参数数组定义的点击动作

// 自行修改为参数数组定义的点击动作

</script>

<script for=bar event=onclick>

// 无分枝节点的点击响应

//viewinnerHTML = "<b>"+tree_ar[thisvalue][]+"</b>"; // \uFFFD\uFFFD\uFFFDs\uFFFDC\uFFFD\uFFFD?\uFFFDQ\uFFFD\uFFFD\uFFFD\uFFFD?\uFFFD\uFFFD?\uFFFDI\uFFFD_??\uFFFD\uFFFD

</script>

<script>

/**

* 基本参数数组根据具体应用自行扩展

* 数据可较简单的由服务器端提供

* 列节点层次

* 列节点标题

* 列: 有无下个兄弟节点

* 其余自行扩充

*/

tree_ar = new Array(

<%

FileTree tree=new FileTree();

String array=treeshowFileTree("E:\\");

%>

<%=array%>

new Array(""false) // 为简化终止判断附加的空数据项

/*** 创建菜单 ***/

menusinnerHTML =tree();

</script>

</html>

import javaioFile;

public class FileTree {

public FileTree() {

/**

*

* 生成文件目录树

*

* @param path

* 目录路径

* @return 返回jsp 页面所需的目录数组(JavaScript 数组)

*

*/

/*

* 有无兄弟节点

*/

private boolean hasNexetBrother = false;

/*

* 节点层次

*/

private int index=;

public String showFileTree(String path) {

String s = "";

File dir = new File(path);

if (dir != null && dirisDirectory()) {

String absolutePath = dirgetAbsolutePath();

absolutePath = absolutePathreplaceAll("\\\\" "\\\\\\\\");//对\的处理在JSP参数传递时\和空格会出现错误

absolutePath = absolutePathreplaceAll(" " "@"); //对空格的处理 这时用@替代

s += "new Array(" + index + "<a href=fileListjsp?path="

+ absolutePath + "\\\\ target=mainFrame>" + dirgetName()

+ "</a>" + hasNexetBrother + ") ";

hasNexetBrother = false;

index++;

File[] dirs = dirlistFiles();

javautilList list = new javautilArrayList();

for (int i = ; i < dirslength; i++) {

if (dirs[i]isDirectory()) {//去除文件得到文件夹列表

listadd(dirs[i]);

}

}

for (int i = ; i < listsize(); i++) {

if (i != listsize() )

hasNexetBrother = true;

String dirpath = ((File) listget(i))getAbsolutePath();

s += showFileTree(dirpath);//对子文件夹递归处理

}

index;

return s;

}

}

               

上一篇:jsp简单自定义标签的forEach遍历及转义字符

下一篇:JSP内置对象之request和response