📄 fivemenu.java
字号:
package com.ty.menu;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspException;
import java.util.*;
import java.io.*;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.JspWriter;
import java.sql.*;
/**
* <p>Title: 自定义标签之动态菜单</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: 腾岳科技</p>
*
* @author 岳伟才
* @version 1.0
*/
public class FiveMenu extends BodyTagSupport {
private String driver;
private String url;
private String uid;
private String pwd;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private ArrayList list = new ArrayList();
StringBuffer buffer = new StringBuffer("");
/**
* doAfterBody 实现菜单
* @return int
* @throws JspException
*/
public int doAfterBody() throws JspException {
BodyContent bodyContent = super.getBodyContent();
getMenuData();
JspWriter out = bodyContent.getEnclosingWriter();
try {
int h = 280;
int t = 20;
for (int k = 0; k < list.size(); k++) {
buffer.append("<DIV id=menu" + (k + 1) +
" onmouseout=menuBack(" + (k + 1) +
") onmouseover=menuOut(" + (k + 1) +
") style='HEIGHT: 20px; LEFT: 30px; POSITION: absolute; TOP: " +
t + "px; WIDTH: 130px; Z-INDEX: 1'>");
buffer.append("<DIV class=cardbottom id=Layer" + (k + 1) +
" style='HEIGHT: " + h +
"px; LEFT: 0px; POSITION: absolute; TOP: 17px; WIDTH: 100px; Z-INDEX: 1'>");
buffer.append(
"<TABLE align=center border=0 height='100%' width=75><TBODY>");
ArrayList qlist = (ArrayList) list.get(k);
for (int i = 1; i < qlist.size(); i++) {
HashMap map = (HashMap) qlist.get(i);
buffer.append("<TR><TD class='td1'><a class='a1' href='" +
map.get("url") + "' target='" +
map.get("target") + "'>" + map.get("name") +
"</a></TD></TR>");
}
buffer.append("<TR><TD> </TD></TR></TBODY></TABLE></DIV>");
buffer.append(
"<TABLE border=0 cellPadding=0 cellSpacing=0 width=100><TBODY><TR>");
buffer.append(
"<TD height=18 width=14><IMG height=18 src='images/stang.gif' width=14></TD>");
if (qlist.size() > 0) {
HashMap map = (HashMap) qlist.get(0);
buffer.append("<TD class=cardtitle>" + map.get("name") +
"</TD></TR></TBODY></TABLE></DIV>");
}
h -= 20;
t += 20;
}
out.print(buffer.toString());
buffer.delete(0, buffer.length());
list.clear();
bodyContent.clear();
} catch (Exception ex) {
ex.printStackTrace();
}
return EVAL_PAGE;
}
/**
* 初始化相关参数
* @throws JspException if an error occurred while processing this tag
* @todo Implement this javax.servlet.jsp.tagext.BodyTag method
*/
public void doInitBody() throws JspException {
InputStream is = this.getClass().getResourceAsStream(
"tymenu.properties");
try {
Properties prop = new Properties();
prop.load(is);
this.driver = prop.getProperty("driver");
this.uid = prop.getProperty("uid");
this.pwd = prop.getProperty("pwd");
this.url = prop.getProperty("url");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 获取连接
* @return Connection
*/
public Connection getConnection() {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, uid, pwd);
} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}
/**
* 获取父菜单总数
* @param connect Connection
* @return int
*/
public int getCount(Connection connect) {
String sql = "select distinct pid from tymenu";
int count = 0;
try {
stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
++count;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return count;
}
/**
* 获取父菜单ID
* @param connect Connection
* @return int[]
*/
public int[] getPid(Connection connect) {
String sql = "select distinct pid from tymenu order by pid";
int count = getCount(connect);
int pids[] = new int[count];
try {
stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
int i = 0;
while (rs.next()) {
pids[i] = rs.getInt("pid");
i++;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return pids;
}
/**
* 获取菜单所需数据
* @return ArrayList
*/
public ArrayList getMenuData() {
try {
conn = getConnection();
stmt = conn.createStatement();
String sql = "";
int ps[] = getPid(conn);
for (int i = 0; i < ps.length; i++) {
ArrayList qlist = new ArrayList();
sql = "select * from tymenu where pid=" + ps[i] +
" order by sid";
rs = stmt.executeQuery(sql);
while (rs.next()) {
HashMap map = new HashMap();
map.put("pid", rs.getObject("pid"));
map.put("sid", rs.getObject("sid"));
map.put("name", rs.getObject("name"));
map.put("url", rs.getObject("url"));
map.put("target", rs.getObject("target"));
qlist.add(map);
}
list.add(qlist);
rs.close();
rs = null;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
this.closeDBConfig();
}
return list;
}
/**
* 关闭数据库连接
*/
public void closeDBConfig() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
rs = null;
stmt = null;
conn = null;
}
}
/**
* 数据库脚本
* CREATE TABLE `tymenu` (
* `pid` int(4) default NULL,
* `sid` int(4) default NULL,
* `name` varchar(15) default NULL,
* `url` varchar(30) default NULL,
* `target` varchar(20) default NULL
* ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
* @return table tymenu
*/
/**
* tld 文件描述
* <?xml version="1.0" encoding="UTF-8"?>
* <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
*<taglib>
*<tlibversion>1.0</tlibversion>
*<jspversion>2.0</jspversion>
*<shortname>tymenu</shortname>
*<tag>
*<name>menu</name>
*<tagclass>com.tengyue.Tymenu</tagclass>
*<bodycontent>jsp</bodycontent>
*</tag>
*</taglib>
*/
/**
* tymenu.properties 文件
* driver = com.mysql.jdbc.Driver
* uid = root
* pwd = ********
* url = jdbc:mysql://localhost:3306/sampledb
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -