梅花雪树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>
?
?
總結
 
                            
                        - 上一篇: Robotics System Tool
- 下一篇: BIEE-1 初始化块和变量
