用java实现动态菜单的显示--数据库实现与菜单参数初始化
2006年12月31日 星期日 11:24
数据库实现如下
第一、创建数据库表如下:
create table HierarchyMenu
(menuid varchar2(20) not null, menuname varchar(100) not null,
url varchar(200) null,
order numeric not null,
parentmenuid varchar2(20) null);
order numeric not null,
parentmenuid varchar2(20) null);
第二、建立数据库连接接口Con,代码如下:
package cn.com.dgzq.framework.menu.connection;
import java.sql.Connection;
/*
* 功能:
* 数据库连接的接口,返回数据库连接Connection
* 具体的实现参考
* DriverCon.java
* PoolCon.java
*
* */
public interface Conn {
public Connection getConn();
}
/*
* 功能:
* 数据库连接的接口,返回数据库连接Connection
* 具体的实现参考
* DriverCon.java
* PoolCon.java
*
* */
public interface Conn {
public Connection getConn();
}
第三、使用装载驱动来连接数据库
package cn.com.dgzq.framework.menu.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
* 功能:
* 实现使用装载驱动来连接数据库
*
* */
public class DriverCon implements Conn {
private String driver;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
* 功能:
* 实现使用装载驱动来连接数据库
*
* */
public class DriverCon implements Conn {
private String driver;
private String url;
private String username;
private String password;
public Connection getConn() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException SQLex) {
SQLex.printStackTrace();
}
return con;
}
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException SQLex) {
SQLex.printStackTrace();
}
return con;
}
public DriverCon(String driver, String url, String username, String password) {
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
}
第四、使用连接池来连接数据库
package cn.com.dgzq.framework.menu.connection;
import java.sql.Connection;
import cn.com.dgzq.db.DBProxy;
/*
* 功能:
* 实现使用数据库连接池来连接数据库
* */
public class PoolCon implements Conn{
public Connection getConn(){
Connection con=null;
DBProxy db=new DBProxy();//DBProxy中使用了数据库连接池
con=db.getConnection();
return con;
}
}
/*
* 功能:
* 实现使用数据库连接池来连接数据库
* */
public class PoolCon implements Conn{
public Connection getConn(){
Connection con=null;
DBProxy db=new DBProxy();//DBProxy中使用了数据库连接池
con=db.getConnection();
return con;
}
}
菜单参数初始化如下:
第一、初始化菜单参数
package cn.com.dgzq.framework.menu.param;
/*
* 功能:
* 初始化菜单参数,考虑到菜单初始化以后一般不再修改参数,所以全部都定义为final
* @levels //表示顶级菜单有多少个
* @height //表示每层菜单的高度
* @width //表示每个菜单项的宽度
* @delay //表示层叠颜色显示的延迟时间
* @type //表示菜单的放置方式,一般有水平与垂直两种(horizontal以及vertical)
*
* */
public class InitMenuPara {
private final int levels;//表示顶级菜单有多少个
private final int height;//表示每层菜单的高度
private final int width;//表示每个菜单项的宽度
private final int delay;//表示层叠颜色显示的延迟时间
private final String type;//表示菜单的放置方式,一般有水平与垂直两种(horizontal以及vertical)
public InitMenuPara(){
this.levels=8;
this.height=25;
this.width=80;
this.delay=200;
this.type="horizontal";
}
public InitMenuPara(int levels,int height,int width,int delay,String type){
this.levels=levels;
this.height=height;
this.width=width;
this.delay=delay;
this.type=type;
}
public int getDelay() {
return delay;
}
public int getHeight() {
return height;
}
public int getLevels() {
return levels;
}
public String getType() {
return type;
}
public int getWidth() {
return width;
}
}
第二、
/*
* 功能:
* 初始化菜单参数,考虑到菜单初始化以后一般不再修改参数,所以全部都定义为final
* @levels //表示顶级菜单有多少个
* @height //表示每层菜单的高度
* @width //表示每个菜单项的宽度
* @delay //表示层叠颜色显示的延迟时间
* @type //表示菜单的放置方式,一般有水平与垂直两种(horizontal以及vertical)
*
* */
public class InitMenuPara {
private final int levels;//表示顶级菜单有多少个
private final int height;//表示每层菜单的高度
private final int width;//表示每个菜单项的宽度
private final int delay;//表示层叠颜色显示的延迟时间
private final String type;//表示菜单的放置方式,一般有水平与垂直两种(horizontal以及vertical)
public InitMenuPara(){
this.levels=8;
this.height=25;
this.width=80;
this.delay=200;
this.type="horizontal";
}
public InitMenuPara(int levels,int height,int width,int delay,String type){
this.levels=levels;
this.height=height;
this.width=width;
this.delay=delay;
this.type=type;
}
public int getDelay() {
return delay;
}
public int getHeight() {
return height;
}
public int getLevels() {
return levels;
}
public String getType() {
return type;
}
public int getWidth() {
return width;
}
}
第二、
package cn.com.dgzq.framework.menu.param;
/*
* 功能:
* 菜单的元素参数初始化,参数如下:
* @onmouseovrtxtcol;//菜单条选中时的文字颜色
* @onmouseovrbgndcol;//鼠标经过菜单条时的背景颜色
* @onmouseouttxtcol;//菜单条未选中时的文字颜色
* @onmouseoutbgndcol;//菜单的背景颜色,这个参数如果设置为""参数就是表示菜单没有颜色,此时可以设置菜单所在表的背景颜色background=/"/images/index_08.jpg/"来表示菜单的背景颜色
* @onmousedowntxtcol;//菜单条选中时的文字颜色
* @onmousedownbgndcol;//鼠标按下选中菜单条时的背景颜色
* @fontstyles;//字体styles
* */
public class MenuItemPara {
/*
* 功能:
* 菜单的元素参数初始化,参数如下:
* @onmouseovrtxtcol;//菜单条选中时的文字颜色
* @onmouseovrbgndcol;//鼠标经过菜单条时的背景颜色
* @onmouseouttxtcol;//菜单条未选中时的文字颜色
* @onmouseoutbgndcol;//菜单的背景颜色,这个参数如果设置为""参数就是表示菜单没有颜色,此时可以设置菜单所在表的背景颜色background=/"/images/index_08.jpg/"来表示菜单的背景颜色
* @onmousedowntxtcol;//菜单条选中时的文字颜色
* @onmousedownbgndcol;//鼠标按下选中菜单条时的背景颜色
* @fontstyles;//字体styles
* */
public class MenuItemPara {
private final String onmouseovrtxtcol;//菜单条选中时的文字颜色
private final String onmouseovrbgndcol;//鼠标经过菜单条时的背景颜色
private final String onmouseouttxtcol;//菜单条未选中时的文字颜色
private final String onmouseoutbgndcol;//菜单的背景颜色,这个参数如果设置为""参数就是表示菜单没 有颜色,此时可以设置菜单所在表的背景颜色background=/"/images/index_08.jpg/"来表示菜单的背景颜色
private final String onmousedowntxtcol;//菜单条选中时的文字颜色
private final String onmousedownbgndcol;//鼠标按下选中菜单条时的背景颜色
private final String fontstyles;//字体styles
private final String onmouseovrbgndcol;//鼠标经过菜单条时的背景颜色
private final String onmouseouttxtcol;//菜单条未选中时的文字颜色
private final String onmouseoutbgndcol;//菜单的背景颜色,这个参数如果设置为""参数就是表示菜单没 有颜色,此时可以设置菜单所在表的背景颜色background=/"/images/index_08.jpg/"来表示菜单的背景颜色
private final String onmousedowntxtcol;//菜单条选中时的文字颜色
private final String onmousedownbgndcol;//鼠标按下选中菜单条时的背景颜色
private final String fontstyles;//字体styles
public MenuItemPara(String onmouseovrtxtcol,String onmouseovrbgndcol,String onmouseouttxtcol,String onmouseoutbgndcol,String onmousedowntxtcol,String onmousedownbgndcol,String fontstyles){
this.onmouseovrtxtcol=onmouseovrtxtcol;
this.onmouseovrbgndcol=onmouseovrbgndcol;
this.onmouseouttxtcol=onmouseouttxtcol;
this.onmouseoutbgndcol=onmouseoutbgndcol;
this.onmousedowntxtcol=onmousedowntxtcol;
this.onmousedownbgndcol=onmousedownbgndcol;
this.fontstyles=fontstyles;
}
public MenuItemPara(){
this.onmouseovrtxtcol="yellow";
this.onmouseovrbgndcol="red";
this.onmouseouttxtcol="black";
this.onmouseoutbgndcol="";
this.onmousedowntxtcol="yellow";
this.onmousedownbgndcol="green";
this.fontstyles="font-family:Tahoma, Verdana, Arial; font-size:12px;font-weight:normal,text-decoration:none;padding: 4px";
}
public String getFontstyles() {
return fontstyles;
}
public String getOnmousedownbgndcol() {
return onmousedownbgndcol;
}
public String getOnmousedowntxtcol() {
return onmousedowntxtcol;
}
public String getOnmouseoutbgndcol() {
return onmouseoutbgndcol;
}
public String getOnmouseouttxtcol() {
return onmouseouttxtcol;
}
public String getOnmouseovrbgndcol() {
return onmouseovrbgndcol;
}
public String getOnmouseovrtxtcol() {
return onmouseovrtxtcol;
}
}
第三、菜单的位置参数初始化
/*
* 功能:
* 菜单的位置参数初始化,参数的参考体是Document,参数如下:
* @block_left;//表示整个菜单离左边的距离,目前参数是170
* @block_top;//表示整个菜单离顶端的距离,目前参数是100
* */
public class MenuPosPara {
private final int block_left;//表示整个菜单离左边的距离,目前的参数调整是177=108(网页的宽度距离左边的宽度)+(778[网页的宽度]-640[整个菜单的宽度=80*8[每个菜单宽度80,共8个菜单]])/2
private final int block_top;//表示整个菜单离顶端的距离,目前参数是100
public MenuPosPara(){
this.block_left=177;
this.block_top=100;
}
public MenuPosPara(int block_left,int block_top){
this.block_left=block_left;
this.block_top=block_top;
}
public int getBlock_left() {
return block_left;
}
public int getBlock_top() {
return block_top;
}
}