权限模块_分配权限_实现分配权限的基本功能
选哪个就能存起来那个,能回显。有依赖关系
把一组相关功能放在一起
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 /> a,应该选中 他的所有直系上级。<br /> b,应该选中他的所有直系下级。<br /> 2,取消选择一个权限时:<br /> a,应该取消选择 他的所有直系下级。<br /> 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)">© 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