權限模塊_使用權限_實現主頁面的效果_顯示左側菜單&只顯示有權限的菜單項

權限模塊__使用權限__實現主頁面的效果

HomeAction.javajavascript

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.jspcss

<%@ 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.jsphtml

 

bottom.jspjava

<%@ 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.jspjquery

 

right.jspweb

<%@ 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.jspspring

<%@ 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.actioncookie

權限模塊_使用權限_顯示左側菜單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.javasession

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.javaapp

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);
    }
相關文章
相關標籤/搜索