JSP page file directory tree source code (recursive algorithm)

Source: Internet
Author: User
Tags array return string javascript array
js| Recursion | <%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<title></title>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"; >
<script language= "JavaScript" >
<!--
function Mm_gotourl () {//v3.0
var i, args=mm_gotourl.arguments; Document. Mm_returnvalue = false;
For (i=0 i< (args.length-1); i+=2) eval (args[i]+ ". location= '" "+args[i+1]+" ");
}
-->
</script>
<link href= "Css/ip.css" rel= "stylesheet" type= "Text/css" >

<body topmargin = "0" leftmargin = "0" marginheight = "0" marginwidth = "0" bgcolor= "#FFFFFF" >
<table border= "0" cellspacing= "0" cellpadding= "0" height= "80%" >
<tr><td>
</td>
</tr>
&LT;TR valign= "Top" >
&LT;TD height= "223" > <span id= "Menus" ></span>
<span id= "View" ></span>

</td>
</tr>
<tr valign= "Bottom" >
<td> </td>
</tr>
</table>
<div align= "right" ></div>
</body>
<script>
/**
* Construct tree with initial value of 0
*/
function Isbrother ()
{
var is=flase;

}
function tree (n) {
var id = new Array ("bar", "pad", " ', " ', " ");
if (n = = 0) {//Initialize variable
n = 1;
i = 0;
s = "";
}
S + + "<table border=0 cellspacing=0 cellpadding=0 >";
for (; i<tree_ar.length-1;i++) {
var k = (n >= tree_ar[i+1][0])? 0:1;
if (Tree_ar[i][2]==false)
{
id[2]= " ';
}
S + + "<tr id= '" +id[k]+ "' value=" +i+ "&GT;&LT;TD >" +id[k+2]+ "</td><td>" +tree_ar[i][1]+ "</td> </tr> "; Construct the node, note the custom attribute value here. The function is to simplify the description of the construction node and share the parameter array information.
if (n > tree_ar[i+1][0]) {//If the expected level is greater than the current level, end this level to return to the previous level.
S + + "</td></tr></table>";
return tree_ar[i+1][0];
}
if (n < tree_ar[i+1][0]) {//If the expected level is less than the current level, recursion goes to the next level.
S + + "<tr style= ' Display:none ' ><td";
if (tree_ar[i][2]==true)
{//If there is the next sibling node, draw the link between the sibling nodes
s+= "background= ' images/line.gif ' border=0 align= ' absmiddle '";
}

s+= "></td><td>";
var m = tree (tree_ar[++i][0]);
S + + "</td></tr>";
if (M < n) {//when the recursive return value is less than the current level expectation, a sequential return action is generated
S + + "</table>";
return m;
}
}
}
S + + "</table>";
return s;
}
</script>

<script For=pad event=onclick>
Click response for split points
v = this.parentelement.rows[this.rowindex+1].style;
if (V.display = = ' block ') {
V.display = ' None ';
this.cells[0].innerhtml = " ';
view.innerhtml = "";//modify the closed action defined for the parameter array
}else {
V.display = ' block ';
this.cells[0].innerhtml = " ';
view.innerhtml = "<b>" +tree_ar[this.value][1]+ "</b>"; Self-modifying an unwind action defined for a parameter array
}

/**
* The following code is used to close other branched branches that have been expanded
* To return or delete this code directly from here if the branch is required to close itself
*/
if (! Tree_ar[this.value].type)//If the node is first entered, record the level information
Gentreeinfo (this);
var n = 1*this.value+1;
for (i=n;i<tree_ar.length-1;i++) {//close the tree that is arranged after the current node
if (Tree_ar[i].type = = "Pad") {
Tree_ar[i].obj2.style.display = ' None ';
tree_ar[i].obj1.cells[0].innerhtml = " ';
}
}
while (Tree_ar[--n][0] > 1); Backtrack to the beginning of the current tree
while (--n >= 0)//close the tree before the beginning of the current tree
if (Tree_ar[n].type = = "Pad") {
Tree_ar[n].obj2.style.display = ' None ';
tree_ar[n].obj1.cells[0].innerhtml = " ';
}


/** Records hierarchy information to simplify the complex node description when traversing a tree **/
function Gentreeinfo (o) {
var el = o.parentelement;
for (Var i=0;i<el.rows.length;i++) {
if (el.rows[i].id!= "") {
Tree_ar[el.rows[i].value].type = el.rows[i].id;
}
if (el.rows[i].id = = "Pad") {
Tree_ar[el.rows[i].value].obj1 = El.rows[i];
Tree_ar[el.rows[i].value].obj2 = el.rows[i+1];
}
}
}
</script>

<script For=bar event=onclick>
Click-Response without branching points
view.innerhtml = "<b>" +tree_ar[this.value][1]+ "</b>"; Self-Modifying the click action defined for the parameter array
Self-Modifying the click action defined for the parameter array

</script>


<script For=bar event=onclick>
Click-Response without branching points
view.innerhtml = "<b>" +tree_ar[this.value][1]+ "</b>"; \ufffd\ufffd\ufffds\ufffdc\ufffd\ufffd?\ufffdq\ufffd\ufffd\ufffd\ufffd?\ufffd\ufffd?\ufffdi\ufffd_?? \ufffd\ufffd
</script>

<script>
/**
* The basic parameter array, expands according to the concrete application
* Data is simpler to be provided by the server side
* Column 1: node hierarchy
* Column 2: node title
* Column 3: There is no next sibling node
* Other self-expansion
*/
Tree_ar = new Array (

<%

Filetree tree=new Filetree ();
String array=tree.showfiletree ("e:\\");

%>
<%=array%>
New Array (1, "", false)//additional empty data items for simplified termination judgment
);


/*** Create Menu ***/
Menus.innerhtml =tree (0);
</script>





Import Java.io.File;

public class Filetree {
Public Filetree () {
}

/**
*
* Generate file Directory tree
*
* @param path
* Directory path
* @return the directory Array (JavaScript array) needed to return the JSP page
*
*/
/*
* Have no brothers node
*/
Private Boolean hasnexetbrother = false;
/*
* Node level
*/
private int index=1;

public string Showfiletree (string path) {

String s = "";
File dir = new file (path);
if (dir!= null && dir.isdirectory ()) {
String Absolutepath = Dir.getabsolutepath ();


Absolutepath = Absolutepath.replaceall ("\\\\", "\\\\\\\\"),//processing of ' \ ', error when JSP parameter passes ' \ ' and space
Absolutepath = Absolutepath.replaceall ("", "@"); For the processing of spaces, then use ' @ ' instead
S + + "new Array" ("+ Index +", ' <a href=filelist.jsp?path= ')
+ Absolutepath + "\\\\ target=mainframe>" + dir.getname ()
+ "</a>", "+ Hasnexetbrother +"), ";
Hasnexetbrother = false;
index++;
file[] dirs = Dir.listfiles ();
Java.util.List List = new Java.util.ArrayList ();
for (int i = 0; i < dirs.length; i++) {
if (Dirs[i].isdirectory ()) {//Remove file. Get Folder List

List.add (Dirs[i]);

}
}
for (int i = 0; i < list.size (); i++) {
if (I!= list.size ()-1)
Hasnexetbrother = true;
String Dirpath = ((File) list.get (i)). GetAbsolutePath ();
S + + showfiletree (dirpath);//child folder recursive processing

}
index--;
}

return s;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.