zoukankan      html  css  js  c++  java
  • dtree实现动态加载树形菜单,动态插入树形菜单

    1.导入  dtree文件    dtree.css   img文件夹   dtree.js  

    2. 建立对应 的数据库      1      父ID     name    id

    3    建立连接类     mysql   例子

    package com.dtree.test;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class ConnectionManager {
        
         private static final String driver ="org.gjt.mm.mysql.Driver";        
         private static final String url = "jdbc:mysql://localhost:3306/yuqing?useUnicode=true&characterEncoding=utf8";
         private static final String username="root";
         private static final String userpwd="zfn";
         
         public static Connection getConnection(){
             
             Connection conn=null;
             try{
                 Class.forName(driver);
                 conn=DriverManager.getConnection(url,username,userpwd);             
             }catch(Exception ex){
                 ex.printStackTrace();
             }
             return conn;
         }
       public static void closeConnection(Connection conn){
           try{
               if(conn!=null && (!conn.isClosed())){
                   conn.close();
               }
           }catch(SQLException ex){
               ex.printStackTrace();
           }
       }
       public static void closeResultSet(ResultSet res){
           try{
               if(res!=null){
                   res.close();
                   res=null;
               }
           }catch(SQLException ex){
               ex.printStackTrace();
           }
       }
       public static void closeStatement(PreparedStatement stmt){
           try{
               if(stmt!=null){
                   stmt.close();
               }
           }catch(SQLException ex){
               ex.printStackTrace();
           }
       }
    }

    4. 实现类 

    package com.dtree.test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    
    public class OrganizationService {
        
        
         private Connection conn;
           private PreparedStatement titleQuery;
           private ResultSet results;
           
        public  List getOrganization(){
            List list = new ArrayList();
    
                    try {
                         conn=ConnectionManager.getConnection();
                         String sql="select * from treetest"; 
                            titleQuery=conn.prepareStatement(sql);
                            results=titleQuery.executeQuery();
                    
                            while(results.next()){
                                Organization org1 = new Organization();
                                org1.setId(results.getInt("id"));
                                org1.setParentId(results.getInt("parentId"));
                                org1.setName(results.getString("name"));
                            
                            
                            list.add(org1);
                        }
                            
                        
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    finally{
                        
                         ConnectionManager.closeResultSet(results);
                         ConnectionManager.closeStatement(titleQuery);
                         ConnectionManager.closeConnection(conn);
                    }
                    
                    return list;
            
            
        }
        
        public int insertUser1(String name) {
               int num=0;
               try {
                     conn=ConnectionManager.getConnection();
                     String sql="insert into treetest (parentId,name,pid)values(0,?,1)"; 
                     titleQuery=conn.prepareStatement(sql);
                     titleQuery.setString(1,name);
                
                     num=titleQuery.executeUpdate();
                    
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                     ConnectionManager.closeResultSet(results);
                     ConnectionManager.closeStatement(titleQuery);
                     ConnectionManager.closeConnection(conn);
                }
               
               return num;
           }
    
        
        
        public int insertUser2(int parentId,String name) {
               int num=0;
               try {
                     conn=ConnectionManager.getConnection();
                     String sql="insert into treetest (parentId,name,pid)values(?,?,2)"; 
                     titleQuery=conn.prepareStatement(sql);
                     titleQuery.setInt(1,parentId);
                     titleQuery.setString(2,name);
                
                     num=titleQuery.executeUpdate();
                    
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                     ConnectionManager.closeResultSet(results);
                     ConnectionManager.closeStatement(titleQuery);
                     ConnectionManager.closeConnection(conn);
                }
               
               return num;
           }
        public  List getOrganization1(){
            List list = new ArrayList();
    
                    try {
                         conn=ConnectionManager.getConnection();
                         String sql="select * from treetest where pid=1"; 
                            titleQuery=conn.prepareStatement(sql);
                            results=titleQuery.executeQuery();
                    
                            while(results.next()){
                                Organization org1 = new Organization();
                                org1.setId(results.getInt("id"));
                                org1.setParentId(results.getInt("parentId"));
                                org1.setName(results.getString("name"));
                            
                            
                            list.add(org1);
                        }
                            
                        
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    finally{                     ConnectionManager.closeResultSet(results);
                         ConnectionManager.closeStatement(titleQuery);
                         ConnectionManager.closeConnection(conn);
                    }
                    
                    return list;    
        }
    }

    5. 实体类

    package com.dtree.test;
    
    public class Organization {
        private int id;
        
        private String name;
        
        private int parentId;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getParentId() {
            return parentId;
        }
    
        public void setParentId(int parentId) {
            this.parentId = parentId;
        }
    }

    6 action

    package com.dtree.test;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class DtreeAction extends ActionSupport {
        
        List list = new ArrayList();
        private String name = null;
        private String name2 = null;
        private int parentId;
        public int getParentId() {
            return parentId;
        }
        public void setParentId(int parentId) {
            this.parentId = parentId;
        }
        public String getName2() {
            return name2;
        }
        public void setName2(String name2) {
            this.name2 = name2;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String execute() {
            OrganizationService service = new OrganizationService();
             list = service.getOrganization();
        
            return SUCCESS;
        }
        
        public String insertname1() {
            OrganizationService service = new OrganizationService();
            System.out.print(name);
            int num=service.insertUser1(name);
        
            return SUCCESS;
        }
        
        public String insertname2() {
            OrganizationService service = new OrganizationService();
            System.out.println(parentId);
            System.out.println(name);
            int num=service.insertUser2(parentId, name2);
        
            return SUCCESS;
        }
        
        public List getList() {
            return list;
        }
        public void setList(List list) {
            this.list = list;
        }
    }

    6 struts  xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    <package name="dtree"  namespace="/dtree" extends="struts-default">
    <action name="dtree" class="com.dtree.test.DtreeAction">
    <result name="success">/dtree.jsp</result>
    </action>
    
    <action name="insertname1" class="com.dtree.test.DtreeAction" method="insertname1">
    <result name="success">/dtree.jsp</result>
    </action>
    
    
    <action name="insertname2" class="com.dtree.test.DtreeAction" method="insertname2">
    <result name="success">/dtree.jsp</result>
    </action>
    
    
    </package>
    
    </struts>

    7 显示树形菜单  jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <base href="<%=basePath%>">
        
        <title>树形结构例子</title>                                        
        <link rel="stylesheet" type="text/css" href="styles.css">
        <link rel="StyleSheet" href="css/dtree.css" type="text/css" />
        <script type="text/javascript" src="script/dtree.js"></script>
      </head>
      
      <body>
      <div class="dtree">
      <script type="text/javascript">
        d = new dTree('d');
        //构造根节点
        d.add(0,-1,'学校人员分布');
        
        <s:iterator value="list">
        d.add(<s:property value="id"/>,<s:property value="parentId"/>,'<s:property value="name"/>','example01.html');
        </s:iterator>
        document.write(d);
      </script>
    
    </div>
      </body>
    </html>

    8  动态增加 树形菜单列的jsp  

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ page import="com.dtree.test.*" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%
    
    
    
    
    
    
    
    
    
    
    &nbsp;%>
    <html>
    &nbsp; <head>&nbsp;
    &nbsp; &nbsp; <title></title>
    &nbsp; &nbsp; <script type="text/javascript">
    &nbsp; &nbsp; &nbsp; &nbsp; function sbmt(){
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.fm.action="dtree/insertname1.action";
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fm.submit();
    &nbsp; &nbsp; &nbsp; &nbsp;}
    &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; function sbmt2(){
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.fm.action="dtree/insertname2.action";
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fm.submit();
    &nbsp; &nbsp; &nbsp; &nbsp;}
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</script>
    
    
    &nbsp; </head>
    &nbsp;&nbsp;
    &nbsp; <body>
    
    
    <form name="fm" method="post" action="">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>m &gt; <br></p>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table width="846" height="63" border="0">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="118" scope="col"><div align="center">插入一级名称</div></th>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="217" scope="col"><div align="center">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></th>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="185" scope="col">&nbsp;</th>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="239" scope="col"><div align="center">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" onClick="return sbmt()" name="Submit" value="提交">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></th>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="65" scope="col">&nbsp;</th>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">插入二级名称</div></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name2">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><select name="parentId" >
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<%
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OrganizationService o=new OrganizationService();
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List list=o.getOrganization1();
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0;i<list.size();i++){
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Organization o1=(Organization)list.get(i);
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value=<%=o1.getId() %> selected><%=o1.getName() %></option>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<%} %>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" onClick="return sbmt2<span style="white-space:pre">    </span>()" name="Submit2" value="提交">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp;</td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">插入三级名称</div></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name3">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><select name="select2">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" name="Submit3" value="提交">
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp;</td>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>
    &nbsp; &nbsp; &nbsp; &nbsp; </form>
    
    
    &nbsp; </body>
    </html>

    http://blog.csdn.net/zhangfuning1986/article/details/7362109

  • 相关阅读:
    使用flv.js + websokect播放rtsp格式视频流
    form表单数据回显双向绑定失效的原因
    element UI日期选择器动态切换了type之后布局错乱
    umi+dva+antd+axios搭建项目,跨域代理问题
    浏览器-preview 改写了 response里面的int类型的字段数值
    mac笔记本分辨率为2560*1600,css样式错位问题
    常用的正则表达式
    vue 实现树形结构
    js禁止遮罩层下页面滚动
    ts封装axios
  • 原文地址:https://www.cnblogs.com/azhqiang/p/4097115.html
Copyright © 2011-2022 走看看