【SSH项目实战】国税协同平台-12.角色权限管理2

然后是action层:
[java]  view plain copy
  1. package cn.edu.hpu.tax.role.action;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.List;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import cn.edu.hpu.tax.core.action.BaseAction;  
  9. import cn.edu.hpu.tax.core.content.Constant;  
  10. import cn.edu.hpu.tax.role.entity.Role;  
  11. import cn.edu.hpu.tax.role.entity.RolePrivilege;  
  12. import cn.edu.hpu.tax.role.entity.RolePrivilegeId;  
  13. import cn.edu.hpu.tax.role.service.RoleService;  
  14.   
  15. import com.opensymphony.xwork2.ActionContext;  
  16.   
  17. public class RoleAction extends BaseAction{  
  18.     @Resource  
  19.     private RoleService roleService;  
  20.     private List<Role> roleList;  
  21.     private Role role;  
  22.     private String[] privilegeIds;  
  23.       
  24.     //列表页面  
  25.     public String listUI() throws Exception{  
  26.         try {  
  27.             //加载权限集合  
  28.             ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);  
  29.             roleList=roleService.findObjects();  
  30.         } catch (Exception e) {  
  31.             throw new Exception(e.getMessage());  
  32.         }  
  33.         return "listUI";  
  34.     }  
  35.     //跳转到新增页面  
  36.     public String addUI(){  
  37.         //加载权限集合  
  38.         ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);  
  39.         return "addUI";  
  40.     }  
  41.     //保存新增  
  42.     public String add(){  
  43.         if(role!=null ){  
  44.             //处理权限保存  
  45.             if(privilegeIds!=null){  
  46.                 HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();  
  47.                 for (int i = 0; i < privilegeIds.length; i++) {  
  48.                     set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));  
  49.                 }  
  50.                 role.setRolePrivileges(set);  
  51.             }  
  52.             roleService.save(role);  
  53.         }  
  54.         return "list";  
  55.     }  
  56.     //跳转到编辑界面  
  57.     public String editUI(){  
  58.         //加载权限集合  
  59.         ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);  
  60.         if(role!=null && role.getRoleId()!=null){  
  61.             role=roleService.findObjectById(role.getRoleId());  
  62.             //处理权限回显  
  63.             if(role.getRolePrivileges()!=null){  
  64.                 privilegeIds=new String[role.getRolePrivileges().size()];  
  65.                 int i=0;  
  66.                 for(RolePrivilege r:role.getRolePrivileges()){  
  67.                     privilegeIds[i++]=r.getId().getCode();  
  68.                 }  
  69.             }  
  70.         }  
  71.         return "editUI";  
  72.     }  
  73.     //保存编辑  
  74.     public String edit(){  
  75.           
  76.         //处理权限保存  
  77.         if(privilegeIds!=null){  
  78.             HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();  
  79.             for (int i = 0; i < privilegeIds.length; i++) {  
  80.                 set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));  
  81.             }  
  82.             role.setRolePrivileges(set);  
  83.         }     
  84.         roleService.update(role);  
  85.       
  86.         return "list";  
  87.     }  
  88.     //删除  
  89.     public String delete(){  
  90.         if(role!=null && role.getRoleId()!=null){  
  91.             roleService.delete(role.getRoleId());  
  92.         }  
  93.         return "list";  
  94.     }  
  95.     //批量删除  
  96.     public String deleteSelected(){  
  97.         if(selectedRow!=null){  
  98.             for(String id:selectedRow){  
  99.                 roleService.delete(id);  
  100.             }  
  101.         }  
  102.         return "list";  
  103.     }  
  104.       
  105.     public List<Role> getRoleList() {  
  106.         return roleList;  
  107.     }  
  108.     public void setRoleList(List<Role> roleList) {  
  109.         this.roleList = roleList;  
  110.     }  
  111.     public Role getRole() {  
  112.         return role;  
  113.     }  
  114.     public void setRole(Role role) {  
  115.         this.role = role;  
  116.     }  
  117.     public String[] getPrivilegeIds() {  
  118.         return privilegeIds;  
  119.     }  
  120.     public void setPrivilegeIds(String[] privilegeIds) {  
  121.         this.privilegeIds = privilegeIds;  
  122.     }  
  123.        
  124. }  

action层写完之后,写我们的配置文件:
role-spring.xml:
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  9.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  10.       
  11.     <!-- 继承了注入sessionFactory的抽象类,不用反复出入sessionFactory -->  
  12.     <bean id="roleDao" class="cn.edu.hpu.tax.role.dao.impl.RoleDaoImpl" parent="xDao"></bean>  
  13.       
  14.     <!-- 扫描Service -->  
  15.     <context:component-scan base-package="cn.edu.hpu.tax.role.service.impl"></context:component-scan>  
  16. </beans>  

role-struts.xml:
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6.   
  7. <struts>  
  8.     <package name="role-action" namespace="/tax" extends="base-default">  
  9.         <action name="role_*" class="cn.edu.hpu.tax.role.action.RoleAction" method="{1}">  
  10.             <result name="{1}">/WEB-INF/jsp/tax/role/{1}.jsp</result>  
  11.             <result name="list" type="redirectAction">  
  12.                 <param name="actionName">role_listUI</param>  
  13.             </result>  
  14.         </action>  
  15.     </package>  
  16. </struts>  


然后在struts的总配置文件中加入我们role的配置:

[html]  view plain copy
  1. <!-- 包含role的struts配置文件 -->  
  2. <include file="cn/edu/hpu/tax/role/conf/role-struts.xml"/>  

然后准备前端的jsp页面:
列表页面:
listUI.jsp:
[html]  view plain copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <html>  
  3. <head>  
  4.     <%@include file="/common/header.jsp"%>  
  5.     <title>角色管理</title>  
  6.     <script type="text/javascript">  
  7.       
  8.       
  9.     //全选、全反选  
  10.     function doSelectAll(){  
  11.         // jquery 1.6 前  
  12.         //$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked"));  
  13.         //prop jquery 1.6+建议使用  
  14.         $("input[name=selectedRow]").prop("checked", $("#selAll").is(":checked"));        
  15.     }  
  16.     //新增  
  17.     function doAdd(){  
  18.         document.forms[0].action = "${basePath}tax/role_addUI.action";  
  19.         document.forms[0].submit();  
  20.     }  
  21.     //编辑  
  22.     function doEdit(id){  
  23.         document.forms[0].action = "${basePath}tax/role_editUI.action?role.roleId=" + id;  
  24.         document.forms[0].submit();  
  25.     }  
  26.     //删除  
  27.     function doDelete(id){  
  28.         document.forms[0].action = "${basePath}tax/role_delete.action?role.roleId=" + id;  
  29.         document.forms[0].submit();  
  30.     }  
  31.     //批量删除  
  32.     function doDeleteAll(){  
  33.         document.forms[0].action = "${basePath}tax/role_deleteSelected.action";  
  34.         document.forms[0].submit();  
  35.     }  
  36.   
  37.   
  38.     </script>  
  39. </head>  
  40. <body class="rightBody">  
  41. <form name="form1" action="" method="post">  
  42.     <div class="p_d_1">  
  43.         <div class="p_d_1_1">  
  44.             <div class="content_info">  
  45.                 <div class="c_crumbs"><div><b></b><strong>角色管理 </strong></div> </div>  
  46.                 <div class="search_art">  
  47.                     <li>  
  48.                         角色名称:<s:textfield name="role.name" cssClass="s_text" id="roleName"  cssStyle="width:160px;"/>  
  49.                     </li>  
  50.                     <li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li>  
  51.                     <li style="float:right;">  
  52.                         <input type="button" value="新增" class="s_button" onclick="doAdd()"/>   
  53.                         <input type="button" value="删除" class="s_button" onclick="doDeleteAll()"/>   
  54.                     </li>  
  55.                 </div>  
  56.   
  57.   
  58.                 <div class="t_list" style="margin:0px; border:0px none;">  
  59.                     <table width="100%" border="0">  
  60.                         <tr class="t_tit">  
  61.                             <td width="30" align="center"><input type="checkbox" id="selAll" onclick="doSelectAll()" /></td>  
  62.                             <td width="120" align="center">角色名称</td>  
  63.                             <td align="center">权限</td>  
  64.                             <td width="80" align="center">状态</td>  
  65.                             <td width="120" align="center">操作</td>  
  66.                         </tr>  
  67.                             <s:iterator value="roleList" status="st">  
  68.                             <tr <s:if test="#st.odd">bgcolor="f8f8f8"</s:if> >  
  69.                                 <td align="center"><input type="checkbox" name="selectedRow" value="<s:property value='roleId'/>"/></td>  
  70.                                 <td align="center"><s:property value="name"/></td>  
  71.                                 <td align="center">  
  72.                                     <s:iterator value="rolePrivileges">  
  73.                                         <s:property value="#privilegeMap[id.code]"/>  
  74.                                     </s:iterator>   
  75.                                 </td>  
  76.                                 <td align="center"><s:property value="state==1?'有效':'无效'"/></td>  
  77.                                 <td align="center">  
  78.                                     <a href="javascript:doEdit('<s:property value='roleId'/>')">编辑</a>  
  79.                                     <a href="javascript:doDelete('<s:property value='roleId'/>')">删除</a>  
  80.                                 </td>  
  81.                             </tr>  
  82.                            </s:iterator>  
  83.                     </table>  
  84.                 </div>  
  85.             </div>  
  86.             <div class="c_pate" style="margin-top: 5px;">  
  87.         <table width="100%" class="pageDown" border="0" cellspacing="0"  
  88.             cellpadding="0">  
  89.             <tr>  
  90.                 <td align="right">  
  91.                     总共1条记录,当前第 1 页,共 1 页     
  92.                             <a href="#">上一页</a>  <a href="#">下一页</a>  
  93.                     到 <input type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1"  
  94.                     max="" value="1" />     
  95.                 </td>  
  96.             </tr>  
  97.         </table>    
  98.         </div>  
  99.         </div>  
  100.     </div>  
  101. </form>  
  102.   
  103.   
  104. </body>  
  105. </html>  

添加界面
addUI.action:
[html]  view plain copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <html>  
  3. <head>  
  4.     <%@include file="/common/header.jsp"%>  
  5.     <title>角色管理</title>  
  6. </head>  
  7. <body class="rightBody">  
  8. <form id="form" name="form" action="${basePath }tax/role_add.action" method="post" enctype="multipart/form-data">  
  9.     <div class="p_d_1">  
  10.         <div class="p_d_1_1">  
  11.             <div class="content_info">  
  12.     <div class="c_crumbs"><div><b></b><strong>角色管理</strong> - 新增角色</div></div>  
  13.     <div class="tableH2">新增角色</div>  
  14.     <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0"  >  
  15.         <tr>  
  16.             <td class="tdBg" width="200px">角色名称:</td>  
  17.             <td><s:textfield name="role.name" /></td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td class="tdBg" width="200px">角色权限:</td>  
  21.             <td>  
  22.                 <s:checkboxlist list="#privilegeMap" name="privilegeIds"></s:checkboxlist>  
  23.             </td>  
  24.         </tr>  
  25.         <tr>  
  26.             <td class="tdBg" width="200px">状态:</td>  
  27.             <td><s:radio list="#{'1':'有效','0':'无效'}" name="role.state"/></td>  
  28.         </tr>  
  29.     </table>  
  30.       
  31.     <div class="tc mt20">  
  32.         <input type="submit" class="btnB2" value="保存" />  
  33.               
  34.         <input type="button"  onclick="javascript:history.go(-1)" class="btnB2" value="返回" />  
  35.     </div>  
  36.     </div></div></div>  
  37.       
  38.      
  39. </form>  
  40. </body>  
  41. </html>  

编辑页面:
editUI.jsp
[html]  view plain copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <html>  
  3. <head>  
  4.     <%@include file="/common/header.jsp"%>  
  5.     <title>角色管理</title>  
  6. </head>  
  7. <body class="rightBody">  
  8. <form id="form" name="form" action="${basePath }tax/role_edit.action" method="post" enctype="multipart/form-data">  
  9.     <div class="p_d_1">  
  10.         <div class="p_d_1_1">  
  11.             <div class="content_info">  
  12.     <div class="c_crumbs"><div><b></b><strong>角色管理</strong> - 编辑角色</div></div>  
  13.     <div class="tableH2">编辑角色</div>  
  14.     <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0"  >  
  15.         <tr>  
  16.             <td class="tdBg" width="200px">角色名称:</td>  
  17.             <td><s:textfield name="role.name" /></td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td class="tdBg" width="200px">角色权限:</td>  
  21.             <td>  
  22.                 <s:checkboxlist list="#privilegeMap" name="privilegeIds"></s:checkboxlist>  
  23.             </td>  
  24.         </tr>  
  25.         <tr>  
  26.             <td class="tdBg" width="200px">状态:</td>  
  27.             <td><s:radio list="#{'1':'有效','0':'无效'}" name="role.state"/></td>  
  28.         </tr>  
  29.     </table>  
  30.     <s:hidden name="role.roleId"/>  
  31.     <div class="tc mt20">  
  32.         <input type="submit" class="btnB2" value="保存" />  
  33.               
  34.         <input type="button"  onclick="javascript:history.go(-1)" class="btnB2" value="返回" />  
  35.     </div>  
  36.     </div></div></div>  
  37.      
  38. </form>  
  39. </body>  
  40. </html>  

下面我们来测试,首先访问列表界面http://localhost/HpuTax/tax/role_listUI.action:

可以看到是空的,我们还没有添加数据,我们点击新增


添加管理员角色和棋权限,然后点击保存:

回到列表看到数据添加成功,



然后点击编辑



将权限更改


发现更改成功!


同样点击删除删除成功,这里不再演示。


至此我们的权限管理功能块完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值