權限模塊_分配權限_實現分配權限(登錄與注銷)的基本功能


權限模塊_分配權限_實現分配權限的基本功能

選哪個就能存起來那個,能回顯。有依賴關系

把一組相關功能放在一起

RoleAction.java

    /**
     * 設置權限頁面
     */
    public String setPrivilegeUI() {
        
        //准備回顯的數據
        Role role = roleService.getById(model.getId());
        ActionContext.getContext().getValueStack().push(role);//放到棧頂,從棧頂找對象
        
        if(role.getPrivileges() != null) {
            privilegeIds = new Long[role.getPrivileges().size()];//Long數組放擁有的權限Id
            int index = 0;
            for(Privilege priv : role.getPrivileges()) {
                privilegeIds[index++] = priv.getId();//從0開始把權限Id找出來
                
            }
        }
        return "setPrivilegeUI";
    }
    /**
     * 設置權限
     */
    public String setPrivilege() {
        //1.從數據庫中獲取原對象
        Role role = roleService.getById(model.getId());//role是根據id來的
        
        //2.設置要修改的屬性
        List<Privilege> privilegeList = privilegeService.getByIds(privilegeIds);
        role.setPrivileges(new HashSet<Privilege>(privilegeList));
        
        //3.更新到數據庫
        roleService.update(role);
        
        return "toList";
    }
    private Long[] privilegeIds;

    public void setPrivilegeIds(Long[] privilegeIds) {
        this.privilegeIds = privilegeIds;
    }

    public Long[] getPrivilegeIds() {
        return privilegeIds;
    }

PrivilegeService.java

public interface PrivilegeService extends DaoSupport<Privilege> {
}

PrivilegeServiceImpl.java

@Service
@Transactional
public class PrivilegeServiceImpl extends DaoSupportImpl<Privilege> implements PrivilegeService{
}

BaseAction.java中增加

@Resource 
protected PrivilegeService privilegeService;

在struts.xml中崗位管理增加一個屬性

<result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result>

在roleAction文件夾中增加一個setPrivilegeUI.jsp頁面

setPrivilegeUI.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>配置權限</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
    <script language="javascript" src="${pageContext.request.contextPath}/script/jquery_treeview/jquery.treeview.js"></script>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/file.css" />
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/script/jquery_treeview/jquery.treeview.css" />

    <script type="text/javascript">
         // 選擇所有
        function selectAll(checkedValue){
            $("input[type=checkbox][name=resourceIdList]").attr("checked", checkedValue);
        }
        
        function doChecked( liID, checkedValue ){
            // 當前點擊的checkbox元素所在的li元素
            var jLi = $("#" + liID);

            // 選中所有的直屬下級。(children()方法是篩選下一級,find()是篩選所有后代)
            jLi.children("ul").find("input[type=checkbox]").attr("checked", checkedValue);
            
            // 選中或取消選中直屬上級
            if( checkedValue ){ // checkedValue是boolean型,表示是否選中了當前復選框
                // 如果當前是選中,則選中所有的直屬上級
                jLi.parents("li").children("input[type=checkbox]").attr("checked", checkedValue);
            }else{
                // 如果當前是取消選中,並且同級中沒有被選中的項,則也取消上級的選中狀態
                var jCheckedSibingCB = jLi.siblings("li").children("input[type=checkbox]:checked");
                if(jCheckedSibingCB.size() == 0){
                    var jCheckboxInput = jLi.parent("ul").prev("label").prev("input[type=checkbox]");
                    jCheckboxInput.attr("checked", checkedValue);
                    
                    // 遞歸操作每一層直屬上級
                    var jParentLi = jCheckboxInput.parent("li");
                    if(jParentLi.size() > 0){
                        doChecked(jParentLi.attr("id"), checkedValue);
                    }
                }
            }
        } 
        
        $(function(){
            $("#tree").treeview();
        });
    </script>
</head>
<body>

<!-- 標題顯示 -->
<div id="Title_bar">
    <div id="Title_bar_Head">
        <div id="Title_Head"></div>
        <div id="Title"><!--頁面標題-->
            <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 配置權限
        </div>
        <div id="Title_End"></div>
    </div>
</div>

<!--顯示表單內容-->
<div id=MainArea>
    <s:form action="role.setPrivilege">
    
    <s:hidden name="id"></s:hidden>
        <div class="ItemBlock_Title1"><!-- 信息說明 --><div class="ItemBlock_Title1">
            <img border="0" width="4" height="7" src="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 正在為【${name}】配置權限 </div> 
        </div>
        
        <!-- 表單內容顯示 -->
        <div class="ItemBlockBorder">
            <div class="ItemBlock">
                <table cellpadding="0" cellspacing="0" class="mainForm">
                    <!--表頭-->
                    <thead>
                        <tr align="LEFT" valign="MIDDLE" id="TableTitle">
                            <td width="300px" style="padding-left: 7px;">
                                <!-- 如果把全選元素的id指定為selectAll,並且有函數selectAll(),就會有錯。因為有一種用法:可以直接用id引用元素 -->
                                <input type="CHECKBOX" id="cbSelectAll" onClick="selectAll(this.checked)"/>
                                <label for="cbSelectAll">全選</label>
                            </td>
                        </tr>
                    </thead>
                   
                       <!--顯示數據列表-->
                    <tbody id="TableData">
                        <tr class="TableDetail1">
                            <!-- 顯示權限樹 -->
                            <td>
                            <s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id" listValue="name"></s:checkboxlist>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- 表單操作 -->
        <div id="InputDetailBar">
            <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/>
            <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a>
        </div>
    </s:form>
</div>

<div class="Description">
    說明:<br />
    1,選中一個權限時:<br />
    &nbsp;&nbsp;&nbsp;&nbsp; a,應該選中 他的所有直系上級。<br />
    &nbsp;&nbsp;&nbsp;&nbsp; b,應該選中他的所有直系下級。<br />
    2,取消選擇一個權限時:<br />
    &nbsp;&nbsp;&nbsp;&nbsp; a,應該取消選擇 他的所有直系下級。<br />
    &nbsp;&nbsp;&nbsp;&nbsp; b,如果同級的權限都是未選擇狀態,就應該取消選中他的直接上級,並遞歸向上做這個操作。<br />

    3,全選/取消全選。<br />
    4,默認選中當前崗位已有的權限。<br />
</div>

</body>
</html>

全選

權限模塊__使用權限__實現登錄與注銷的功能

UserAction.java

/** 登錄頁面 */
    public String loginUI() {
        
        return "loginUI";
    }
    /** 登錄 */
    public String login() {
        User user = userService.findByLoginNameAndPassword(model.getLoginName(),model.getPassword());
        if(user == null) {
            addFieldError("login","用戶名或密碼不正確");
            return "loginUI";
        }else {
            //登錄用戶
            ActionContext.getContext().getSession().put("user", user);
            return "toIndex";
        }
        
    }
    /** 注銷 */
    public String logout() {
        ActionContext.getContext().getSession().remove("user");
        return "logout";
    }

Struts.xml中用戶管理新增3個屬性

 

   <result name="loginUI">/WEB-INF/jsp/userAction/loginUI.jsp</result>
    <result name="logout">/WEB-INF/jsp/userAction/logout.jsp</result>
    <result name="toIndex" type="redirectAction">/index.jsp</result

Action中

  addFieldError("name", "有錯了!");

JSP中:

  <s:fielderror fieldName="name"/>

  <s:fielderror/>顯示全部錯誤消息

UserService.java

 

public interface UserService extends DaoSupport<User>{
    /**
     * 根據用戶名與密碼查詢用戶
     * @param loginName
     * @param password
     * @return
     */
    User findByLoginNameAndPassword(String loginName, String password);

}

UserServiceImpl.java

 

@Service
@Transactional    //可以不用寫了,因為父類有
public class UserServiceImpl extends DaoSupportImpl<User> implements UserService{

    public User findByLoginNameAndPassword(String loginName, String password) {
        
        //使用密碼的MD5摘要進行對比
        String md5Digest = DigestUtils.md5Hex(password);
        return (User) getSession().createQuery(//
                "FROM User u WHERE u.longinName=? AND u.password=?")//
                .setParameter(0, loginName)//
                .setParameter(1, md5Digest)//
                .uniqueResult();
    }
}

loginUI.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
    <title>Itcast OA</title>
    <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
    <link href="${pageContext.request.contextPath}/style/blue/login.css" type=text/css rel=stylesheet>
    <script type="text/javascript">
        $(function(){
            document.forms[0].loginName.focus();
        });
        
        // 在被嵌套時就刷新上級窗口
        if(window.parent != window){
            window.parent.location.reload(true);
        }
    </script>
</head>

<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 class=PageBody >



<!-- 顯示表單 -->
<s:form action="user_login" focusElement="loginNameInput">
    <div id="CenterAreaBg"> 
        <div id="CenterArea">
            <div id="LogoImg"><img border="0" src="${pageContext.request.contextPath}/style/blue/images/logo.png" /></div>
            <div id="LoginInfo">
                <table BORDER=0 CELLSPACING=0 CELLPADDING=0 width=100%>
                    <tr>
                        <td colspan="3"><!-- 顯示錯誤 -->
                            <font color="red"><s:fielderror/></font>
                        </td>
                    </tr>
                    <tr>
                        <td width=45 class="Subject"><img border="0" src="${pageContext.request.contextPath}/style/blue/images/login/userId.gif" /></td>
                        <td>
                            <s:textfield name="loginName" size="20" tabindex="1" cssClass="TextField required" id="loginNameInput" />
                        </td>
                        <td rowspan="2" style="padding-left:10px;">
                            <input type="image" tabindex="3" src="${pageContext.request.contextPath}/style/blue/images/login/userLogin_button.gif" />
                        </td>
                    </tr>
                    <tr>
                        <td class="Subject"><img border="0" src="${pageContext.request.contextPath}/style/blue/images/login/password.gif" /></td>
                        <td><s:password name="password" id="aa" size="20" tabindex="2" showPassword="false" cssClass="TextField required" /></td>
                    </tr>
                </table>
            </div>
            <div id="CopyRight"><a href="javascript:void(0)">&copy; 2010 版權所有 yejing</a></div>
        </div>
    </div>
    </s:form>
</body>

</html>

Logout.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
    <title>您已退出Itcast OA系統</title>
     <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
    <link href="${pageContext.request.contextPath}/style/blue/logout.css" rel="stylesheet" type="text/css">
</head>

<body>
    <table border=0 cellspacing=0 cellpadding=0 width=100% height=100%>
        <tr>
            <td align=center>
                <div id=Logout>
                    <div id=AwokeMsg>
                        <img id=LogoutImg src="${pageContext.request.contextPath}/style/blue/images/logout/logout.gif" border=0>
                        <img id=LogoutTitle src="${pageContext.request.contextPath}/style/blue/images/logout/logout1.gif" border=0>
                    </div>
                    <div id=LogoutOperate>
                        <img src="${pageContext.request.contextPath}/style/blue/images/logout/logout2.gif" border=0> 
                        <a href="${pageContext.request.contextPath}/user_loginUI.action">重新進入系統</a> 
                        <img src="${pageContext.request.contextPath}/style/blue/images/logout/logout3.gif" border=0> 
                        <a href="javascript: window.open('','_self');window.close();">關閉當前窗口</a>
                    </div>
                </div>
            </td>
        </tr>
    </table>
</body>
</html>

Index.jsp

訪問http://localhost:8080/ItcastOA/user_login.action

輸入密碼1234登錄成功后

http://localhost:8080/ItcastOA/user_login.action


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM