zoukankan      html  css  js  c++  java
  • 权限模块_使用权限_实现主页面的效果_显示左侧菜单&只显示有权限的菜单项

    权限模块__使用权限__实现主页面的效果

    HomeAction.java

    public class HomeAction extends ActionSupport {
        public String index() {
            return "index";
        }
        public String top() {
            return "top";
        }
        public String bottom() {
            return "bottom";
        }
        public String leaf() {
            return "leaf";
        }
        public String right() {
            return "right";
        }
    }

    index.jsp

    <%@ page language="java" pageEncoding="UTF-8"%>
    <html>
        <head>
            <title>Itcast OA</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>

    top.jsp

     

    bottom.jsp

    <%@ page language="java" pageEncoding="UTF-8"%>
    <html>
    <head>
        <title></title>
        <LINK href="${pageContext.request.contextPath}/style/blue/statusbar.css" type=text/css rel=stylesheet>
    </head>
    
    <body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>
    
    <div id="StatusBar">
        <div id="Online">
            在线人员:共 <span class="OnlineUser" id="onlineUserNum"></span><span class="OnlineView">
            <a href="javascript:void(0)">[查看在线名单]</a>
    </span></div>
    
        <div id="Info">
            <a href="http://www.itcast.cn" title = "传智播客首页" target=_blank >传智播客首页</a> |
            <a href="http://bbs.itcast.cn" title = "传智播客BBS" target=_blank >传智播客BBS</a>
        </div>
    
        <DIV id=DesktopText>
            <a href="javascript:void(0)"><img border="0" src="${pageContext.request.contextPath}/style/images/top/text.gif"/> 便笺</a>
    
            <span id=TryoutInfo>
    
            </span>
            <span id="Version">
                <a href="javascript:void(0)">
                    <img border="0" width="11" height="11" src="${pageContext.request.contextPath}/style/images/top/help.gif" />
                    <img border="0" width="40" height="11" src="${pageContext.request.contextPath}/style/blue/images/top/version.gif" />
                </a>
            </span>
        </DIV>
    </div>
    
    </body>
    </html>

    left.jsp

     

    right.jsp

    <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>无标题文档</title>
        </head>
    
        <body>
        </body>
    </html>

    WebRoot下的index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%-- 重定向到指定的页面,访问另外一个地址,request.getContextPath()加上当前应用程序名称
        转发:相当于方法调用,在一个应用程序内执行
    --%>
    <%
        response.sendRedirect(request.getContextPath() + "/home_index.action");
    %>

    再次访问首页http://localhost:8080/ItcastOA重定向到了http://localhost:8080/ItcastOA/home_index.action

    权限模块_使用权限_显示左侧菜单1

    <%@ 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" />
        </head>
        <body style="margin: 0">
            <div id="Menu">
                <ul id="MenuUl">
                    <%--显示一级菜单 --%>
                    <s:iterator value="#application.topPrivilegeList">
                        <li class="level1">
                            <div onClick="menuClick(this);" class="level1Style">
                                <img src="style/images/MenuIcon/FUNC20082.gif" class="Icon" />
                                ${name}
                            </div>
                            <ul style="display: none;" class="MenuLevel2">
                                <%-- 显示二级菜单 --%>
                                <s:iterator value="children">
                                <li class="level2">
                                    <div class="level2Style">
                                        <img src="style/images/MenuIcon/menu_arrow_single.gif" />
                                        <a target="right" href="System_Role/list.html"> 岗位管理</a>
                                    </div>
                                </li>
                                </s:iterator>
                            </ul>
                        </li>
                    </s:iterator>
                </ul>
            </div>
        </body>
    </html>

    InitListener.java

    public class InitListener implements ServletContextListener {
        
        //获取容器与相关的Service对象
        
        public void contextInitialized(ServletContextEvent sce) {
            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) {
            
        }
    
    }

    PrivilegeService.java

    public interface PrivilegeService extends DaoSupport<Privilege> {
        //查询所有顶级的权限
        List<Privilege> findTopList();
    }

    PrivilegeServiceImpl.java

    @Service
    @Transactional
    public class PrivilegeServiceImpl extends DaoSupportImpl<Privilege> implements PrivilegeService{
    
        public List<Privilege> findTopList() {
            return getSession().createQuery(//
                    "FROM Privilegea p WHERE p.parent IS NULL")//
                    .list();
        }
    }

    web.xml增加

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

    懒加载

    登录时加载了用户,用户关联的对象都没有加载,但在第二个请求又访问了用户关联的对象,抛懒加载异常

    Privilege.hbm.xml

    User.hbm.xml

    Role.hbm.xml

    访问http://localhost:8080/ItcastOA/home_index.action

    全部显示

    图标问题

    打开收回

    权限模块__使用权限__显示左侧菜单2__只显示有权限的菜单项

    leaf.jsp

    <%@ 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){
                    //$("#aa").hide();
                    //$("#aa").show();
                    $(menu).next().toggle();
                }
            
            </script>
            
        </head>
        <body style="margin: 0">
            <div id="Menu">
                <ul id="MenuUl">
                    <%--显示一级菜单 --%>
                    <s:iterator value="#application.topPrivilegeList">
                        <s:if test="#session.user.hasPrivilegeByName(name)"><!-- ognl表达式中调用方法 -->
                            <li class="level1">
                                <div onClick="menuClick(this);" class="level1Style">
                                    <img src="style/images/MenuIcon/${id}.gif" class="Icon" />
                                    ${name}
                                </div>
                                <ul style="" 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>

    User.java中增加

    /**
         * 判定本用户是否有指定名称的权限
         * @param name
         * @return
         */
        public boolean hasPrivilegeByName(String name) {
            //超级管理员有所有的权限
            if(ifAdmin()) {
                return true;
                
            }
            //普通用户要判断是否含有这个权限
            for(Role role : roles) {
                for(Privilege priv : role.getPrivileges()) {
                    if(priv.getName().equals(name)) {
                        return true;
                    }
                }
            }
            return false;
        } 
        
        /**
         * 判断本用户是否是超级管理员
         */
        public boolean ifAdmin() {
            return "admin".equals(loginName);
        }
  • 相关阅读:
    WPF 关于拖拽打开文件的注意事项
    asp.net core 3.1中对Mongodb BsonDocument的序列化和反序列化支持
    用百度webuploader分片上传大文件
    多线程学习笔记
    web.config数据库连接字符串加密
    Visual Studio 2010 常用快捷方式
    Team Foundation Server 2013 日常使用使用手册(四)分支与合并
    Team Foundation Server 2013 日常使用使用手册(三)上传新工程、创建任务、创建bug、设置预警
    Team Foundation Server 2013 日常使用使用手册(二)修改、签入、撤销、回滚、对比代码变更
    Team Foundation Server 2013 日常使用使用手册(一)-本地连接TFS、查看任务
  • 原文地址:https://www.cnblogs.com/justdoitba/p/7820353.html
Copyright © 2011-2022 走看看