您的位置:首页 > 其它

OA权限模块根据用户权限显示不同的菜单

2015-10-07 20:54 851 查看
        权限模块中非常重要的一项就是根据用户的权限来显示不同的菜单选项,那么在项目中是如何实现的呢?这篇博文就为大家简单的介绍一下。

    一、设置主页的跳转页面

    我们在访问时候,不管是什么角色的用户,第一个呈现给用户的应该是主界面。我们在访问项目根目录的时候如何才能正确的跳转呢,很简单,重定向一下就可以了

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
response.sendRedirect(request.getContextPath()+"/home_index.action");
%>


            二、编写主界面

       我们的主界面主要分为上下左右四个部分,JSP分布如下:

       1.主界面

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>tgb</title>
<%@ include file="/WEB-INF/jsp/public/commons.jspf"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/script/jquery_treeview/jquery.cookie.js"></script>
</head>
<frameset rows="100,*,25" framespacing=0 border=0 frameborder="0">
<frame noresize name="TopMenu" scrolling="no" src="${pageContext.request.contextPath}/home_top.action">
<frameset cols="180,*" id="resize">
<frame noresize name="menu" scrolling="yes" src="${pageContext.request.contextPath}/home_left.action">
<frame noresize name="right" scrolling="yes" src="${pageContext.request.contextPath}/home_right.action">
</frameset>
<frame noresize name="status_bar" scrolling="no" src="${pageContext.request.contextPath}/home_bottom.action">
</frameset>

<noframes><body>
</body>
</noframes></html>
        

        2.左侧菜单栏

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>导航菜单</title>
<%@ include file="/WEB-INF/jsp/public/commons.jspf"%>
<link type="text/css" rel="stylesheet" href="style/blue/menu.css" />
<script type="text/javascript">
function menuClick(menu) {
$(menu).next().toggle();
}
</script>
</head>

<body style="margin: 0">
<div id="Menu">

<ul id="MenuUl">

<%-- 显示一级菜单 --%>
<s:iterator value="#topPrivilegeList">
<!-- 判断用户是否具有一级菜单权限 -->
<s:if test="#session.user.hasPrivilegeByName(name)">
<li class="level1">
<div onClick="menuClick(this);" class="level1Style">
<img src="style/images/MenuIcon/${id}.gif" class="Icon" />
${name}
</div>
<ul style="display: none;" class="MenuLevel2" id="aa">
<%-- 显示二级菜单 --%>
<s:iterator value="children">
<!-- 判断用户是否具有二级菜单权限 -->
<s:if test="#session.user.hasPrivilegeByName(name)">
<li class="level2">
<div class="level2Style">
<img src="style/images/MenuIcon/menu_arrow_single.gif" /> <a
target="right"
href="${pageContext.request.contextPath}${url}.action">
${name}</a>
</div>
</li>
</s:if>
</s:iterator>
</ul>
</li>
</s:if>
</s:iterator>
</ul>

</div>
</body>
</html>


        三、优化权限列表的加载

          考虑到权限的菜单不是经常变动的,为了提高访问效率,减少对数据库的操作,下面我们将权限列表放置到Application域中缓存起来。

        1.编写Servlet监听器类。

package cn.tgb.oa.uitl;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.tgb.oa.domain.Privilege;
import cn.tgb.oa.service.PrivilegeService;

/**
* Servlet监听器,起到了缓存的作用
* @author LUCKY
*
*/
public class InitListener implements ServletContextListener {

//初始化监听器,将topPrivilegeList放置到application中去
public void contextInitialized(ServletContextEvent sce) {
// 获取容器与相关的Service对象
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");

// 准备数据:topPrivilegeList,会出现懒加载异常,我们启动不经过过滤器,而且不是一个请求。请一个请求之内的懒加载用过滤器解决
List<Privilege> topPrivilegeList = privilegeService.findTopList();
sce.getServletContext().setAttribute("topPrivilegeList", topPrivilegeList);
System.out.println("------------> 已准备数据 <------------");
}

public void contextDestroyed(ServletContextEvent arg0) {

}
}


        2.在web.xml中配置监听器对象

<!-- 配置Spring的用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!-- 用于做初始化工作的监听器,一定要配置到Spring的ContextLoaderListener之后,因为要用到Spring的容器对象 -->
<listener>
<listener-class>cn.tgb.oa.uitl.InitListener</listener-class>
</listener>


        四、设置不同用户的不同访问权限

        在User实体中加入方法判断:

package cn.tgb.oa.domain;

import java.util.HashSet;
import java.util.Set;

import cn.tgb.oa.domain.Department;
import cn.tgb.oa.domain.Role;

public class User {
private Long id;
private Department department;
private Set<Role> roles = new HashSet<Role>();

private String loginName; // 登录名
private String password; // 密码
private String name; // 真实姓名
private String gender; // 性别
private String phoneNumber; // 电话号码
private String email; // 电子邮件
private String description; // 说明
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

/**
* 判断用户是否有指定名称的权限
*/
public boolean hasPrivilegeByName(String name){
//超级管理员有所有的权限
if(isAdmin()){
return true;
}
//判断普通用户是否有指定名称的权限
for(Role role:roles){
for(Privilege priv:role.getPrivileges()){
if(priv.getName().equals(name)){
return true;
};
}
}
return false;
}

/**
* 判断本用户是否是超级管理员
*/
public boolean isAdmin(){
return "admin".equals(loginName);
}

}

       总结:

       这部分主要有三个新的知识点:

       1.使用application作为缓存,存入权限实体列表,而且通过WebApplicationContextUtils来获得Spring容器;

       2.用<s:if>标签来控制界面的菜单是否显示,很好玩;

       3.对User对象进行修改,加入了判断用户权限的方法,第一次用到,很新鲜。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: