`

梅花雪树jsp

阅读更多

说明:代码在附件中,直接copy到myeclipse中,在数据库中执行附件中的sql,更改链接数据库的用户名和密码,就可访问http://localhost:8080/Mztree

1.创建数据库
create table tree
(
id int primary key,
parentId int,
text varchar(50),
hint varchar(100),
icon varchar(20),
data varchar(20),
url varchar(50),
target varchar(20),
method varchar(50)
)
2.下载梅花雪树控件(放在根目录)
3.创建连接数据库类DBfactory.java
package com.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBfactory {

public static Connection getConnection() throws SQLException ,
java.lang.ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
String userName = "root";
String password = "aidemama";
Connection con = DriverManager.getConnection(url,userName,password);
return con;
}


}

4.创建操作类TreeService.java

package com.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

 

public class TreeService {
 Connection con = null;
 public String getTree() throws SQLException  {
 
 try { 
  con = DBfactory.getConnection();
  String sql = "select * from tree";
  PreparedStatement pre = con.prepareStatement(sql);
  ResultSet rs =pre.executeQuery();
  String tree ="";
  
  while(rs.next()) {
   String id = String.valueOf(rs.getInt("id"));
   String pid = String.valueOf(rs.getInt("parentId"));
   String text = rs.getString("text");
   String hint = rs.getString("hint");
   String url = rs.getString("target");
   String target = rs.getString("target");
   tree +="\n"+"tree"+".nodes[\""+pid+"_"+id+"\"]=\"";
   
   if(text != null && text.trim()!="") {
    tree +="text:"+text+";";
    
   }
   if(hint != null && hint.trim()!="") {
    tree +="hint:"+hint+";";
    
   }
   if(url != null && url.trim()!="") {
    tree +="url:"+url+";";
    
   }
   if(target != null && target.trim()!="") {
    tree +="target:"+target+";";
    
   }
   
   
   tree += "\"";
  }
  return tree;
  
 }catch(Exception e){
  System.out.println("can't create tree");
  return null;
  
 }finally {
  
  con.close();
 }
 }

}
5.创建jsp


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.service.TreeService"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <title>梅花雪中文网 - 网页脚本控件集 MzTreeView10</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="author" content="黄方荣(meizz·梅花雪)://www.meizz.com">
    <script language="JavaScript" src="MzTreeView10/MzTreeView10.js"></script>
    <!--link href="http://www.meizz.com/Scripts/Global.css" type="text/css" rel="stylesheet"-->
    <style>
    A.MzTreeview
    {
      font-size: 9pt;
      padding-left: 3px;
    }
    </style>
  </head>

  <body  bgcolor="#FFFFDB" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" align="center">
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    window.tree = new MzTreeView("tree");

    tree.icons["property"] = "property.gif";
    tree.icons["css"] = "collection.gif";
    tree.icons["book"]  = "book.gif";
    tree.iconsExpand["book"] = "bookopen.gif"; //展开时对应的图片
    tree.setIconPath("MzTreeView10/"); //可用相对路径
 <%
  TreeService ts=new TreeService();
  String tree=ts.getTree();
  out.print(tree);
 %>
  
    tree.setURL("#");
    tree.setTarget("MzMain");
    document.write(tree.toString());    //亦可用 obj.innerHTML = tree.toString();
    //-->
    </SCRIPT>
  </body>
</html>

 

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics