用Hibernate+Struts+Spring做了一个简单的树形菜单..通过dtree.js组件显示树形菜单。
具体Sql代码以及java后台的实现代码都贴出来.以供参考..
数据库表的代码:
--权限涉及的如下五个表;
drop table roles;
drop table user_roles;
drop table menus;
drop table rols_menus;
drop table users;
--用户表;
create table users(
--主键,
userid number primary key not null,
username varchar2(50)not null,
userpass varchar2(20)not null,
--外键,所属部门;
deptid number not null,
--生日;
birthday date not null,
sex varchar2(4)not null,
age number not null,
--手机;
combine varchar2(11)null,
email varchar2(50)null,
address varchar2(50)null,
zipcode varchar2(8)null,
remark varchar2(50)null,
--创建users表的所属部门的外键;
foreign key(deptid) references dept(deptid)
)
--创建序列;
drop sequence seq_users;
create sequence seq_users;
insert into users values(seq_users.nextval,'周海涛','123456',1,sysdate,'男',21,'13973312067','haitao7290783@126.com','湖南株洲县','412109','暂无备注');
--创建角色表;
create table roles(
rolesid number primary key not null, --主键角色编号;
rolename varchar2(20)not null --角色名称;
)
drop sequence seq_roles;
create sequence seq_roles;
insert into roles values(seq_roles.nextVal,'管理员');
insert into roles values(seq_roles.nextVal,'程序员');
insert into roles values(seq_roles.nextVal,'项目经理');
insert into roles values(seq_roles.nextVal,'公司总裁');
insert into roles values(seq_roles.nextVal,'副总经理');
insert into roles values(seq_roles.nextVal,'前台接待');
insert into roles values(seq_roles.nextVal,'产品销售');
insert into roles values(seq_roles.nextVal,'普通用户');
--用户角色中间表;
create table user_roles(
urId number references users(userid), --<外键>用户编号;
rolesid number references roles(rolesid) --<外键>角色编号;
);
insert into user_roles values(1,1);
insert into user_roles values(2,2);
insert into user_roles values(3,3);
insert into user_roles values(4,4);
insert into user_roles values(5,8);
--菜单表;
create table menus(
menusid number primary key not null, --菜单编号;
menusname varchar2(20)not null, --菜单姓名;
menusparent number not null, --父菜单编号;
menusurl varchar2(150) null, --菜单链接;
isshow number not null --是否显示;
)
drop sequence seq_menus;
create sequence seq_menus;
--'-1'代表根目录 ; --1代表根目录下; 2--代表1的跟目下, --3代表-同级目录;
insert into menus values(seq_menus.nextVal,'功能菜单',-1,'',1);
insert into menus values(seq_menus.nextVal,'系统管理',1,'',1);
insert into menus values(seq_menus.nextVal,'用户管理',2,'',1);
insert into menus values(seq_menus.nextVal,'考勤管理',3,'',1);
insert into menus values(seq_menus.nextVal,'个人设置',3,'',1);
insert into menus values(seq_menus.nextVal,'短信管理',3,'',1);
insert into menus values(seq_menus.nextVal,'通讯录管理',3,'',1);
insert into menus values(seq_menus.nextVal,'权限管理',3,'',1);
insert into menus values(seq_menus.nextVal,'公告管理',3,'',1);
insert into menus values(seq_menus.nextVal,'考勤查询',4,'timecard.do?p=findtimecardSelect',1);
insert into menus values(seq_menus.nextVal,'外出查询',4,'timecard.do?p=findpersonoutqin',1);
insert into menus values(seq_menus.nextVal,'上班考勤',4,'timecard.do?p=kaoqin',1);
insert into menus values(seq_menus.nextVal,'外出登记',4,'timecard.do?p=goout',1);
insert into menus values(seq_menus.nextVal,'密码修改',5,'geRenSheZhi.do?p=mimaxiugai',1);
insert into menus values(seq_menus.nextVal,'用户资料',5,'geRenSheZhi.do?p=yonghuziliao',1);
insert into menus values(seq_menus.nextVal,'查看用户',5,'geRenSheZhi.do?p=chakanyonghu',1);
insert into menus values(seq_menus.nextVal,'发送短信',6,'shortmessage.do?p=fasongduanxin',1);
insert into menus values(seq_menus.nextVal,'已收短信',6,'shortmessage.do?p=yishouduanxin',1);
insert into menus values(seq_menus.nextVal,'已发短信',6,'shortmessage.do?p=yifaduanxinxi',1);
insert into menus values(seq_menus.nextVal,'添加联系人',7,'linkman.do?p=tianjiailianxiren',1);
insert into menus values(seq_menus.nextVal,'添加新类型',7,'linkman.do?p=insertNewType',1);
insert into menus values(seq_menus.nextVal,'删除联系人',7,'linkman.do?p=shanchulianxiren',1);
insert into menus values(seq_menus.nextVal,'查找联系人',7,'linkman.do?p=chazhaolianxiren',1);
insert into menus values(seq_menus.nextVal,'查询权限',8,'popedom.do?p=select_grant',1);
insert into menus values(seq_menus.nextVal,'添加权限',8,'popedom.do?p=insert_grant',1);
insert into menus values(seq_menus.nextVal,'修改权限',8,'popedom.do?p=update_grant',1);
insert into menus values(seq_menus.nextVal,'发送公告',9,'proclaim.do?p=send_proclaim',1);
insert into menus values(seq_menus.nextVal,'已发公告',9,'proclaim.do?p=alreadysend',1);
insert into menus values(seq_menus.nextVal,'公告待审',9,'proclaim.do?p=proclaim_cognizance',1);
insert into menus values(seq_menus.nex tVal,'已收公告',9,'proclaim.do?p=receipt_receipt',1);
--角色菜单中间表;
create table rols_menus(
rolesid number references roles(rolesid), --<外键>角色编号;
menusid number references menus(menusid) --<外键>菜单编号;
)
drop sequence seq_rols_menus;
create sequence seq_rols_menus;
insert into rols_menus values(1,1);
insert into rols_menus values(1,2);
insert into rols_menus values(1,3);
insert into rols_menus values(1,4);
insert into rols_menus values(1,5);
insert into rols_menus values(1,6);
insert into rols_menus values(1,7);
insert into rols_menus values(1,8);
insert into rols_menus values(1,9);
insert into rols_menus values(1,10);
insert into rols_menus values(1,11);
insert into rols_menus values(1,12);
insert into rols_menus values(1,13);
insert into rols_menus values(1,14);
insert into rols_menus values(1,15);
insert into rols_menus values(1,16);
insert into rols_menus values(1,17);
insert into rols_menus values(1,18);
insert into rols_menus values(1,19);
insert into rols_menus values(1,20);
insert into rols_menus values(1,21);
insert into rols_menus values(1,22);
insert into rols_menus values(1,23);
insert into rols_menus values(1,24);
insert into rols_menus values(1,25);
insert into rols_menus values(1,26);
insert into rols_menus values(1,27);
insert into rols_menus values(1,28);
insert into rols_menus values(1,29);
insert into rols_menus values(1,30);
insert into rols_menus values(4,1);
insert into rols_menus values(4,2);
insert into rols_menus values(4,3);
insert into rols_menus values(4,4);
insert into rols_menus values(4,5);
insert into rols_menus values(4,6);
insert into rols_menus values(4,7);
insert into rols_menus values(4,10);
insert into rols_menus values(4,11);
insert into rols_menus values(4,12);
insert into rols_menus values(4,15);
insert into rols_menus values(4,16);
insert into rols_menus values(4,17);
insert into rols_menus values(4,18);
insert into rols_menus values(4,19);
BaseDAO中的查询方法.
/**
* 分页查找;
* @param hql
* @param page
* @param size
* @param objects
* @return
*/
public List pageQuery(final String hql,final Integer page,final Integer size,final Object...objects){
return super.getHibernateTemplate().executeFind(new HibernateCallback(){
//一个匿名内部类;
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
Query query=session.createQuery(hql);
if(objects!=null){
for(int i=0;i<objects.length;i++){
query.setParameter(i, objects[i]);
}
}
if(page!=null&& size!=null){
query.setFirstResult((page-1)*size).setMaxResults(size);
}
return query.list();
}
});
}
service中的方法,根据用户名获取所有菜单;
/**
* 根据用户名得到菜单 */
public Set selectByName(String name) {
// TODO Auto-generated method stub
String hql="From Menus as M join fetch M.roleses R join fetch R.userses U where U.username=?";
List list=iuser.pageQuery(hql, null, null,name);
//把list转成set集合;
Set set=new HashSet(list);
return set;
}
在Action中. 调用service中的方法。根据用户查找得到菜单;保存到session中。
Set menus=iser.selectByName(name);
System.out.println("菜单项:"+menus);
//把菜单项设置到session中;
request.getSession().setAttribute("MeunsList",menus);
在JSP也页面使用dTree的方式是:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="dtree/dtree.js"></script>
<link rel="styleSheet" href="dtree/dtree.css" type="text/css"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>左边的框架页面</title>
<!-- 定义树 -->
<script>
var d=new dTree('d');
d.config.check=false; //没有复选框
d.config.useIcons =true //有图片;
</script>
<style type="text/css">
*{
font-size:12px;
color:black;
}
</style>
</head>
<body>
<form method="post" name="myform">
<center>
</center>
<!-- 遍历,添加节点; -->
<c:forEach items="${MeunsList}" var="m">
<script>
//自己的菜单编号,父节点编号,名称,链接,提示,显示位置
d.add(${m.menusid},${m.menusparent},'${m.menusname}','${m.menusurl}','欢迎使用OA办公管理系统!','right');
</script>
</c:forEach>
<!--显示树-->
<script>
document.write(d);
d.openAll();
</script>
</form>
</body>
</html>