首页 > 编程 > JSP > 正文

不用迭代算法而快速实现的jsp树结构

2024-09-05 00:20:20
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 在web页面上实现树状结构,有点麻烦.
    在最近的一个mis系统的开发中,我们项目组大量用到了树结构:比如人员的选择,单位的选择等待.
    这个mis系统所用的数据库是oracle 9i.  oracle 9i 的sql支持迭代查询.我们的树是由牛人彭越写的,不过
    也参照了网络上比较著名的xtree(可以到此下载:http://webfx.eae.net/),他的树算法支持无限级的树结构,不过性能好像
    很慢.我持保留态度.
    他用到的关键技术就是这句话:
    string sql = "select dwxh,dwbh,dwmc,dwfxh,level cc from xt_dw connect by  prior dwxh = dwfxh start with dwfxh = 0";
    可是许多数据库不支持迭代查询,并且迭代查询速度真是不能忍受.有什么更好的办法呢.下面说说我的解决方案.

    一:需求的提出
    1:客户需要一个关于部门人员的树结构,数据库为mysql4.1
    2:java实现
    二:建表:
    1:
    用户信息表:
    各字段为:用户序号,用户编号,用户名称,单位序号,密码,用户登陆号
    create table xt_yh
    (
      yhxh  int(9) not null auto_increment primary key,
      yhbh  varchar(30),
      yhmc  varchar(30),
      dwxh  int(9),
      pwd   varchar(20),
      yhdlh varchar(30)
    )
    --插入三条测试数据:
    --insert into xt_yh(yhbh,yhmc,dwxh,pwd,yhdlh) values('licl','李春雷',2,'password','licl')
    --insert into xt_yh(yhbh,yhmc,dwxh,pwd,yhdlh) values('fengx','冯欣',2,'password','fengx')
    --insert into xt_yh(yhbh,yhmc,dwxh,pwd,yhdlh) values('wangqx','王庆香',6,'password','wangqx')
    2:
    单位部门表
    各字段为:单位序号,单位编号,单位名称,单位父序号
    create table xt_dw
    (
      dwxh  int(9) not null auto_increment primary key,
      dwbh  varchar(10),
      dwmc  varchar(30),
      dwfxh int(9)
    )
    --插入5条测试数据
    --insert into xt_dw(dwbh,dwmc,dwfxh) values('0100000000','武汉科技局',0);
    --insert into xt_dw(dwbh,dwmc,dwfxh) values('0101000000','人事处',1);
    --insert into xt_dw(dwbh,dwmc,dwfxh) values('0102000000','后勤处',1);
    --insert into xt_dw(dwbh,dwmc,dwfxh) values('0101010000','人事处son1',2);
    --insert into xt_dw(dwbh,dwmc,dwfxh) values('0101020000','人事处son2',2);
    --insert into xt_dw(dwbh,dwmc,dwfxh) values('0102010000','后勤处son1',3);

    注意:
    为了实现快速的树结构实现,我需要充分利用单位编号dwbh,dwbh才有10位编码,其中,第一第二位表示一级单位,第三第四位表示二级单位,
    第五六位表示三级单位...那么10位编码就可以实现五级单位的树结构.
    比如:测试数据的树结构如下:
      1  武汉科技局:
     2  人事处
      3  人事处son1
      3  人事处son2
     2  后勤处
      3后勤处son1

    其实xt_dw表中的父序号是多余的.不过如果你要用迭代算法来实现,就是必须的
    才有10位编码,我只需要一句简单快速的sql语句就可以实现树结构:
    string sql = "select dwxh,dwbh,dwmc,dwfxh from xt_dw order by dwbh"
    这句sql在几乎所有的数据库平台都能执行,速度也快.
    下面贴出采用xtree,用10位编码而不是迭代算法实现的树:

    /*******constants.java**********/

    package com.lcl.common;

    public class constants {
     
     public static final string dbdriver = "com.mysql.jdbc.driver";    //mysql驱动
     
     public static final string dburl="jdbc:mysql://localhost/beauoa"; //数据库url
     
     public static final string username="root";                       //数据库用户名
     
     public static final string password="root";     //数据库密码
     
     
    }


    /**********dbaccess.java****************/

    package com.lcl.common;

    import java.sql.*;
    import java.lang.*;

    /**
     * @author 李春雷
     *
     * todo 要更改此生成的类型注释的模板,请转至
     * 数据库访问类
     */
    public class dbaccess

     string strdbdriver = constants.dbdriver;
     string strdburl = constants.dburl;
     string username = constants.username;
     string password = constants.password;
     private connection conn = null;
     private statement stmt = null;
     resultset rs=null;
     //注册数据库驱动程序
     public dbaccess()
     { 
      try
      { 
       class.forname(strdbdriver);
      }
      //异常处理
      catch( java.lang.classnotfoundexception e)
      {
       system.err.println("dbaccess():"+e.getmessage());
      }
     }
     //建立数据库连接及定义数据查询
     public resultset executequery(string sql)
     {
      rs=null;
      try
      {
       conn=drivermanager.getconnection(strdburl,username,password);
       stmt=conn.createstatement();
       rs=stmt.executequery(sql);
      }
      catch(sqlexception ex)
      {
       system.err.println("ap.executequery:"+ex.getmessage());
      }
     
      return rs;
     }
     //定义数据操库作
     public void executeupdate(string sql)
     {
      stmt=null;
      rs=null;
      try
      {
       conn=drivermanager.getconnection(strdburl,username,password);
       stmt=conn.createstatement();
       stmt.executequery(sql);
       stmt.close();
       conn.close();
      }
      catch(sqlexception ex)
      {
       system.err.println("ap.executequery:"+ex.getmessage());
      }
     }
     //关闭数据库
     public void closestmt()
     {
      try
      {
       stmt.close();
      }
      catch(sqlexception e)
      {
       e.printstacktrace();
      }
     }
     public void closeconn()
     {
      try
      {
       conn.close();
      }
      catch(sqlexception e)
      {
       e.printstacktrace();
      }
     }
     public static void main(string[] args){
      system.out.println("hello,it's test");
      dbaccess dbaccess = new dbaccess();
      string sql = "select * from xt_yh";
      resultset rs = dbaccess.executequery(sql);
      try
      {
       while(rs.next()){
        system.out.print(rs.getstring(1)+rs.getstring(2)+rs.getstring(3)+rs.getstring(4)+rs.getstring(5)+rs.getstring(6));
        system.out.println();
       }
      dbaccess.closestmt();
      dbaccess.closeconn();
      }
      catch (sqlexception e)
      {
       // todo 自动生成 catch 块
       e.printstacktrace();
      }
     }
     }

     /*********depemplconfig.jsp************/

     <%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*,com.lcl.common.*" errorpage="" %>
    <!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=gb2312">
    <title>无标题文档</title>
    <head>
    <script type="text/javascript" src="../resources/xdatatree.js"></script>
    <link type="text/css" rel="stylesheet" href="../resources/xtree.css" />
    <style type="text/css">

    body {
     background: white;
     color:  black;
    }
    </style>
    <title> new document </title>
    <meta name="generator" content="editplus">
    <meta name="author" content="">
    <meta name="keywords" content="">
    <meta name="description" content="">
    </head>
    <script type="text/javascript"> 
    webfxtreeconfig.rooticon  = "../resources/images/xp/folder.png";
    webfxtreeconfig.openrooticon = "../resources/images/xp/openfolder.png";
    webfxtreeconfig.foldericon  = "../resources/images/xp/folder.png";
    webfxtreeconfig.openfoldericon = "../resources/images/xp/openfolder.png";
    webfxtreeconfig.fileicon  = "../resources/images/xp/file.png";
    webfxtreeconfig.lminusicon  = "../resources/images/xp/lminus.png";
    webfxtreeconfig.lplusicon  = "../resources/images/xp/lplus.png";
    webfxtreeconfig.tminusicon  = "../resources/images/xp/tminus.png";
    webfxtreeconfig.tplusicon  = "../resources/images/xp/tplus.png";
    webfxtreeconfig.iicon   = "../resources/images/xp/i.png";
    webfxtreeconfig.licon   = "../resources/images/xp/l.png";
    webfxtreeconfig.ticon   = "../resources/images/xp/t.png";
    webfxtreeconfig.blankicon       = "../resources/images/blank.png";

    var tree = new webfxtree("单位人员基本情况","r0");
    var child;
    var nodetoaddperson;

    function adddepttreenode(prenodelevel,curnodelevel,displabel,skey,stag) {
      if(curnodelevel==1) {
         child = tree.add(new webfxtreeitem(displabel,skey,stag));
      }
      else {
        if(curnodelevel==prenodelevel) {
           if(child.parentnode)
            child = child.parentnode.add(new webfxtreeitem(displabel,skey,stag));
        }
        if(curnodelevel>prenodelevel) {
           child = child.add(new webfxtreeitem(displabel,skey,stag));
        }
        if(curnodelevel<prenodelevel) {
            for(i=0;i<prenodelevel-curnodelevel+1;i++)
               child = child.parentnode;
            child = child.add(new webfxtreeitem(displabel,skey,stag));
        }
      }
      return child;
    }

    function treeclick() {
     if(tree.getselected()) {
         if(tree.getselected().childnodes.length==0&&tree.getselected().key!="r0")
           cmddelete.disabled = false;
         else
           cmddelete.disabled = true;
         if(tree.getselected().key.substr(0,2)=="rz") {
           cmdadddept.disabled = true;
           cmdaddpeople.disabled = true;
           var stryhxh;
           stryhxh = tree.getselected().key.substr(2);
           //window.open("../useradm/edityh.do?yhxh="+stryhxh,"main");
         }
         else if(tree.getselected().key.substr(0,2)=="rb") {
           cmdadddept.disabled = false;
           cmdaddpeople.disabled = false;
           var strdwxh;
           strdwxh = tree.getselected().key.substr(2);
           //window.open("../useradm/editbm.do?dwxh="+strdwxh,"main");
         }
         else {
           cmdadddept.disabled = false;
           cmdaddpeople.disabled = true;
           //window.open("yhroot.jsp","main");
         }
     }
    }

    function addpeople() {
        var strdwxh;
        if(tree.getselected()) {
       if (tree.getselected().key.substr(0,2)=="rb") {
            strdwxh = tree.getselected().key.substr(2);
      //window.open("../useradm/addyh.do?dwxh="+strdwxh,"main");
      alert("addpeople");
       }
        }
    }

    function adddept() {
        var strdwxh;
        if(tree.getselected()) {
       if (tree.getselected().key.substr(0,2)=="rb") {
            strdwfxh = tree.getselected().key.substr(2);
      //window.open("../useradm/addbm.do?dwfxh="+strdwfxh,"main");
        alert("adddept");
       }
          else if(tree.getselected().key=="r0") {
            //window.open("../useradm/addbm.do?dwfxh=0","main");
            alert("adddept");
          }
        }
    }

    function deleselected() {
      if(!confirm("确认删除该节点吗?"))
          return;
      if(tree.getselected()) {
        if(tree.getselected().key.substr(0,2)=="rb") {
           var strdwxh;
           strdwxh = tree.getselected().key.substr(2);
           //window.open("../useradm/delbm.do?dwxh="+strdwxh,"main");
           alert("deleselected");
        }
        else if(tree.getselected().key.substr(0,2)=='rz') {
           var stryhxh,stryhbh;
           stryhxh = tree.getselected().key.substr(2);
           stryhbh = tree.getselected().tag;
           //window.open("../useradm/delyh.do?yhxh="+stryhxh+"&yhbh="+stryhbh,"main");
           alert("deleselected");
        }
      }
    }

    function removenode() {
      if(tree.getselected()) {
        var node = tree.getselected();
        node.remove();
      }
    }

    function addpeoplenode(strparentkey,strkey,strtext,strtag) {
      if(tree.getselected()) {
        var node = tree.getselected();
        var childnode;
        //node.expand();
        childnode = node.add(new webfxtreeitem(strtext,strkey,strtag,"","","../resources/images/people1.png"));
        node.expand(); //why i do so? i dont want to tell you,hah!
        childnode.focus();
        treeclick();
      }
    }

    function adddeptnode(strparentkey,strkey,strtext,strtag) {
      if(tree.getselected()) {
        var node = tree.getselected();
        var childnode;
        childnode = node.add(new webfxtreeitem(strtext,strkey,strtag));
        node.expand();
        childnode.focus();
        treeclick();
      }
    }

    function updatedeptnode(strtag,strtext) {
      if(tree.getselected()) {
        var node = tree.getselected();
        node.text = strtext;
        node.tag  = strtag;
        node.focus();
      }
    }

    function updatepeoplenode(strtag,strtext) {
      if(tree.getselected()) {
        var node = tree.getselected();
        node.text = strtext;
        node.tag  = strtag;
        node.focus();
      }
    }
    </script>
    <%
    int dwxh;
    int dwfxh;
    int yhxh;
    string dwbh = null;
    string dwmc = null;
    string yhmc = null;
    string yhbh = null;
    int prelevel =1;
    int level = 1;
    dbaccess dbaccess = new dbaccess();
    string sql = "select dwxh,dwbh,dwmc,dwfxh from xt_dw order by dwbh";
    resultset rs = dbaccess.executequery(sql);
    try
    {
     while(rs.next())
     {
            dwxh = rs.getint(1);
            dwbh = rs.getstring(2);
            dwmc = rs.getstring(3);
            dwfxh = rs.getint(4);
    //通过单位编号计算level
      string last = dwbh.substring(9,10);
      int i = 9;
      while(last.equals("0") && i>0){
       i--;
       last = dwbh.substring(i,i+1);
      
      }
      
      if(i==0 || i==1) level =1;
      if(i==2 || i==3) level =2;
      if(i==4 || i==5) level =3;
      if(i==6 || i==7) level =4;
      if(i==8 || i==9) level =5;
    //
      %>
               <script type="text/javascript"> 
         nodetoaddperson = adddepttreenode(<%=prelevel%>,<%=level%>,"<%=dwmc%>","rb<%=dwxh%>","<%=dwbh%>");
            </script>  
      
      <%
      prelevel = level;
      string subsql = "select yhxh,yhmc,yhbh from xt_yh where dwxh = "+integer.tostring(dwxh);
      resultset subrs = dbaccess.executequery(subsql);
           while(subrs.next()) {
                  yhxh = subrs.getint(1);
                  yhmc = subrs.getstring(2);
                  yhbh = subrs.getstring(3);
      %>
                 <script type="text/javascript"> 
         nodetoaddperson.add(new webfxtreeitem("<%=yhmc%>","rz<%=yhxh%>","<%=yhbh%>","","","../resources/images/people1.png"));
            </script>
         <%
      }
      
     }
     dbaccess.closestmt();
     dbaccess.closeconn();
    }
    catch(exception e)
    {

    }
    %>

    <base target="_self">
    <meta http-equiv="pragma" content="no-cache">
    </head>
    <body>
    <table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td width="273" colspan="2">
           <font face="宋体" size="3">    
           </font>
        </td>
      </tr>
      <tr>
        <th width="33%" align="center" nowrap>
          <p align="center">
          <input id=cmdadddept name="adddept" type=button value="增加部门" >
          </p>
        </th>
        <th width="33%" align="center" nowrap>
          <p align="center">
          <input id=cmdaddpeople name="addpeople" type=button value="增加用户" >
          </p>
        </th>
        <th width="33%" align="center" nowrap>
          <p align="center">
          <input id=cmddelete name="delete" type=button value=" 删除 " disabled>
          </p>
        </th>
      </tr>
      <tr>
        <td width="273" height="8"  colspan="2">&nbsp;
         
        </td>
      </tr>
    </table>
    </body>
    <div >
    <script type="text/javascript"> 
     document.write(tree);
    </script>
    </div>
    </html>

    //其中jsp页面上的几个javascript函数为同事牛人彭越所写,我没改动,在此说明.

    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表