熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JSP頁面文件目錄樹源碼(遞歸算法)

2022-06-13   來源: 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;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20339.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.