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

接上一篇 http://blog.csdn.net/acmman/article/details/49512903
然后是action层:
package cn.edu.hpu.tax.role.action;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import cn.edu.hpu.tax.core.action.BaseAction;
import cn.edu.hpu.tax.core.content.Constant;
import cn.edu.hpu.tax.role.entity.Role;
import cn.edu.hpu.tax.role.entity.RolePrivilege;
import cn.edu.hpu.tax.role.entity.RolePrivilegeId;
import cn.edu.hpu.tax.role.service.RoleService;

import com.opensymphony.xwork2.ActionContext;

public class RoleAction extends BaseAction{
	@Resource
	private RoleService roleService;
	private List<Role> roleList;
	private Role role;
	private String[] privilegeIds;
	
	//列表页面
	public String listUI() throws Exception{
		try {
			//加载权限集合
			ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);
			roleList=roleService.findObjects();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		return "listUI";
	}
	//跳转到新增页面
	public String addUI(){
		//加载权限集合
		ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);
		return "addUI";
	}
	//保存新增
	public String add(){
		if(role!=null ){
			//处理权限保存
			if(privilegeIds!=null){
				HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();
				for (int i = 0; i < privilegeIds.length; i++) {
					set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));
				}
				role.setRolePrivileges(set);
			}
			roleService.save(role);
		}
		return "list";
	}
	//跳转到编辑界面
	public String editUI(){
		//加载权限集合
		ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);
		if(role!=null && role.getRoleId()!=null){
			role=roleService.findObjectById(role.getRoleId());
			//处理权限回显
			if(role.getRolePrivileges()!=null){
				privilegeIds=new String[role.getRolePrivileges().size()];
				int i=0;
				for(RolePrivilege r:role.getRolePrivileges()){
					privilegeIds[i++]=r.getId().getCode();
				}
			}
		}
		return "editUI";
	}
	//保存编辑
	public String edit(){
		
		//处理权限保存
		if(privilegeIds!=null){
			HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();
			for (int i = 0; i < privilegeIds.length; i++) {
				set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));
			}
			role.setRolePrivileges(set);
		}	
		roleService.update(role);
	
		return "list";
	}
	//删除
	public String delete(){
		if(role!=null && role.getRoleId()!=null){
			roleService.delete(role.getRoleId());
		}
		return "list";
	}
	//批量删除
	public String deleteSelected(){
		if(selectedRow!=null){
			for(String id:selectedRow){
				roleService.delete(id);
			}
		}
		return "list";
	}
	
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public String[] getPrivilegeIds() {
		return privilegeIds;
	}
	public void setPrivilegeIds(String[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}
	 
}

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

role-struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>
	<package name="role-action" namespace="/tax" extends="base-default">
		<action name="role_*" class="cn.edu.hpu.tax.role.action.RoleAction" method="{1}">
			<result name="{1}">/WEB-INF/jsp/tax/role/{1}.jsp</result>
			<result name="list" type="redirectAction">
				<param name="actionName">role_listUI</param>
			</result>
		</action>
	</package>
</struts>


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

<!-- 包含role的struts配置文件 -->
<include file="cn/edu/hpu/tax/role/conf/role-struts.xml"/>

然后准备前端的jsp页面:
列表页面:
listUI.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="/common/header.jsp"%>
    <title>角色管理</title>
    <script type="text/javascript">
    
    
  	//全选、全反选
	function doSelectAll(){
		// jquery 1.6 前
		//$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked"));
		//prop jquery 1.6+建议使用
		$("input[name=selectedRow]").prop("checked", $("#selAll").is(":checked"));		
	}
  	//新增
  	function doAdd(){
  		document.forms[0].action = "${basePath}tax/role_addUI.action";
  		document.forms[0].submit();
  	}
  	//编辑
  	function doEdit(id){
  		document.forms[0].action = "${basePath}tax/role_editUI.action?role.roleId=" + id;
  		document.forms[0].submit();
  	}
  	//删除
  	function doDelete(id){
  		document.forms[0].action = "${basePath}tax/role_delete.action?role.roleId=" + id;
  		document.forms[0].submit();
  	}
  	//批量删除
  	function doDeleteAll(){
  		document.forms[0].action = "${basePath}tax/role_deleteSelected.action";
  		document.forms[0].submit();
  	}


    </script>
</head>
<body class="rightBody">
<form name="form1" action="" method="post">
    <div class="p_d_1">
        <div class="p_d_1_1">
            <div class="content_info">
                <div class="c_crumbs"><div><b></b><strong>角色管理 </strong></div> </div>
                <div class="search_art">
                    <li>
                        角色名称:<s:textfield name="role.name" cssClass="s_text" id="roleName"  cssStyle="width:160px;"/>
                    </li>
                    <li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li>
                    <li style="float:right;">
                        <input type="button" value="新增" class="s_button" onclick="doAdd()"/> 
                        <input type="button" value="删除" class="s_button" onclick="doDeleteAll()"/> 
                    </li>
                </div>


                <div class="t_list" style="margin:0px; border:0px none;">
                    <table width="100%" border="0">
                        <tr class="t_tit">
                            <td width="30" align="center"><input type="checkbox" id="selAll" onclick="doSelectAll()" /></td>
                            <td width="120" align="center">角色名称</td>
                            <td align="center">权限</td>
                            <td width="80" align="center">状态</td>
                            <td width="120" align="center">操作</td>
                        </tr>
                       		<s:iterator value="roleList" status="st">
                            <tr <s:if test="#st.odd">bgcolor="f8f8f8"</s:if> >
                                <td align="center"><input type="checkbox" name="selectedRow" value="<s:property value='roleId'/>"/></td>
                                <td align="center"><s:property value="name"/></td>
                                <td align="center">
                                	<s:iterator value="rolePrivileges">
                                		<s:property value="#privilegeMap[id.code]"/>
                                	</s:iterator>	
                                </td>
                                <td align="center"><s:property value="state==1?'有效':'无效'"/></td>
                                <td align="center">
                                    <a href="javascript:doEdit('<s:property value='roleId'/>')">编辑</a>
                                    <a href="javascript:doDelete('<s:property value='roleId'/>')">删除</a>
                                </td>
                            </tr>
                           </s:iterator>
                    </table>
                </div>
            </div>
			<div class="c_pate" style="margin-top: 5px;">
		<table width="100%" class="pageDown" border="0" cellspacing="0"
			cellpadding="0">
			<tr>
				<td align="right">
                 	总共1条记录,当前第 1 页,共 1 页   
                            <a href="#">上一页</a>  <a href="#">下一页</a>
					到 <input type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1"
					max="" value="1" />   
			    </td>
			</tr>
		</table>	
        </div>
        </div>
    </div>
</form>


</body>
</html>

添加界面
addUI.action:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="/common/header.jsp"%>
    <title>角色管理</title>
</head>
<body class="rightBody">
<form id="form" name="form" action="${basePath }tax/role_add.action" method="post" enctype="multipart/form-data">
    <div class="p_d_1">
        <div class="p_d_1_1">
            <div class="content_info">
    <div class="c_crumbs"><div><b></b><strong>角色管理</strong> - 新增角色</div></div>
    <div class="tableH2">新增角色</div>
    <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0"  >
        <tr>
            <td class="tdBg" width="200px">角色名称:</td>
            <td><s:textfield name="role.name" /></td>
        </tr>
        <tr>
            <td class="tdBg" width="200px">角色权限:</td>
            <td>
            	<s:checkboxlist list="#privilegeMap" name="privilegeIds"></s:checkboxlist>
            </td>
        </tr>
        <tr>
            <td class="tdBg" width="200px">状态:</td>
            <td><s:radio list="#{'1':'有效','0':'无效'}" name="role.state"/></td>
        </tr>
    </table>
    
    <div class="tc mt20">
        <input type="submit" class="btnB2" value="保存" />
            
        <input type="button"  onclick="javascript:history.go(-1)" class="btnB2" value="返回" />
    </div>
    </div></div></div>
    
   
</form>
</body>
</html>

编辑页面:
editUI.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="/common/header.jsp"%>
    <title>角色管理</title>
</head>
<body class="rightBody">
<form id="form" name="form" action="${basePath }tax/role_edit.action" method="post" enctype="multipart/form-data">
    <div class="p_d_1">
        <div class="p_d_1_1">
            <div class="content_info">
    <div class="c_crumbs"><div><b></b><strong>角色管理</strong> - 编辑角色</div></div>
    <div class="tableH2">编辑角色</div>
    <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0"  >
        <tr>
            <td class="tdBg" width="200px">角色名称:</td>
            <td><s:textfield name="role.name" /></td>
        </tr>
        <tr>
            <td class="tdBg" width="200px">角色权限:</td>
            <td>
            	<s:checkboxlist list="#privilegeMap" name="privilegeIds"></s:checkboxlist>
            </td>
        </tr>
        <tr>
            <td class="tdBg" width="200px">状态:</td>
            <td><s:radio list="#{'1':'有效','0':'无效'}" name="role.state"/></td>
        </tr>
    </table>
    <s:hidden name="role.roleId"/>
    <div class="tc mt20">
        <input type="submit" class="btnB2" value="保存" />
            
        <input type="button"  onclick="javascript:history.go(-1)" class="btnB2" value="返回" />
    </div>
    </div></div></div>
   
</form>
</body>
</html>

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

可以看到是空的,我们还没有添加数据,我们点击新增
【SSH项目实战】国税协同平台-12.角色权限管理2_第1张图片

添加管理员角色和棋权限,然后点击保存:
【SSH项目实战】国税协同平台-12.角色权限管理2_第2张图片
回到列表看到数据添加成功,
【SSH项目实战】国税协同平台-12.角色权限管理2_第3张图片


然后点击编辑



将权限更改
【SSH项目实战】国税协同平台-12.角色权限管理2_第4张图片

发现更改成功!
【SSH项目实战】国税协同平台-12.角色权限管理2_第5张图片

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


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

转载请注明出处:http://blog.csdn.net/acmman/article/details/49513077

你可能感兴趣的:(spring,Hibernate,mysql,struts,项目实战)