OA项目实战学习(7)——初始化数据&权限配置显示

详细有哪些功能:

初始化数据

  • 权限数据。
  • 超级管理员。

Installer.java
package cn.xbmu.oa.install;

import javax.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import cn.xbmu.oa.domain.Privilege;
import cn.xbmu.oa.domain.User;
/**
 * 安装应用程序的时候,就初始化好数据
 * @author Administrator
 *
 */
@Component
public class Installer {
	@Resource
	private SessionFactory sessionFactory;
	@Transactional
	public void install() {
		Session session = sessionFactory.getCurrentSession();

		// =====================================
		// 1.超级管理员
		User user = new User();
		user.setName("超级管理员");
		user.setLoginName("admin");
		user.setPassword(DigestUtils.md5Hex("admin"));// 使用MD5摘要信息 加密
		session.save(user);// 保存
		// 2.权限数据
		Privilege menu, menu1, menu2, menu3, menu4, menu5;
		menu = new Privilege("系统管理", null, "FUNC20082.gif", null);
		menu1 = new Privilege("岗位管理", "roleAction_list", null, menu);
		menu2 = new Privilege("部门管理", "departmentAction_list", null, menu);
		menu3 = new Privilege("用户管理", "userAction_list", null, menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);

		session.save(new Privilege("岗位列表", "roleAction_list", null, menu1));
		session.save(new Privilege("岗位加入", "roleAction_add", null, menu1));
		session.save(new Privilege("岗位删除", "roleAction_delete", null, menu1));
		session.save(new Privilege("岗位改动", "roleAction_edit", null, menu1));

		session.save(new Privilege("部门列表", "departmentAction_list", null, menu2));
		session.save(new Privilege("部门加入", "departmentAction_add", null, menu2));
		session.save(new Privilege("部门删除", "departmentAction_delete", null,menu2));
		session.save(new Privilege("部门改动", "departmentAction_edit", null, menu2));

		session.save(new Privilege("用户列表", "userAction_list", null, menu3));
		session.save(new Privilege("用户加入", "userAction_add", null, menu3));
		session.save(new Privilege("用户删除", "userAction_delete", null, menu3));
		session.save(new Privilege("用户改动", "userAction_edit", null, menu3));
		session.save(new Privilege("用户初始化password", "userAction_initPassword", null,menu3));

		// -------------------------

		menu = new Privilege("网上交流", null, "FUNC20064.gif", null);
		menu1 = new Privilege("论坛管理", "forumManageAction_list", null, menu);
		menu2 = new Privilege("论坛", "forumAction_list", null, menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);

		// -------------------------

		menu = new Privilege("审批流转", null, "FUNC20057.gif", null);
		menu1 = new Privilege("审批流程管理", "processDefinitionAction_list", null,
				menu);
		menu2 = new Privilege("申请模板管理", "applicationTemplateAction_list", null,
				menu);
		menu3 = new Privilege("起草申请", "flowAction_applicationTemplateList",
				null, menu);
		menu4 = new Privilege("待我审批", "flowAction_myTaskList", null, menu);
		menu5 = new Privilege("我的申请查询", "flowAction_myApplicationList", null,
				menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		session.save(menu5);

	}

	public static void main(String[] args) {
		System.out.println("正在执行安装");
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Installer installer = (Installer) ac.getBean("installer");
		installer.install();
		System.out.println("安装完毕");
	}
}
能够执行该main方法,就能够将数据初始化的数据加入到数据库中。


可是这样的方式,不适合用户使用,因此我们能够在window环境下 ,写一个批处理文件 install.bat ,让用户双击就可以进行一键安装应用时并初始化数据。
install.bat
假设在Linux系统下。

project结构:
  

完毕设置权限页面:

/xbmuoa/WebRoot/WEB-INF/jsp/roleAction/list.jsp
<!--显示数据列表-->
        <tbody id="TableData" class="dataContainer" datakey="roleList">
        	<s:iterator value="roleList">
				<tr class="TableDetail1 template">
					<td>${name} </td>
					<td>${description} </td>
					<td><s:a action="roleAction_delete.action?id=%{id}" onClick="return delConfirm()">删除</s:a>
						<s:a action="roleAction_editUI.action?

id=%{id}">改动</s:a> <s:a action="roleAction_setPrivilegeUI?

id=%{id}" >设置权限</s:a> </td> </tr> </s:iterator> </tbody>

RoleAction.java
package cn.xbmu.oa.view.action;

import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.xbmu.oa.base.BaseAction;
import cn.xbmu.oa.domain.Privilege;
import cn.xbmu.oa.domain.Role;
import com.opensymphony.xwork2.ActionContext;
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role>{
	private Long[] privilegeIds;
	
	/**查询*/
	public String list() throws Exception {
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "list";
	}
	/**删除*/
	public String delete() throws Exception {
		roleService.delete(model.getId());
		return "toList";
	}
	/**加入页面*/
	public String addUI() throws Exception {
		return "saveUI";
	}
	/**加入*/
	public String add() throws Exception {
		//保存到数据库中
		roleService.save(model);
		return "toList";
	}
	/**改动页面*/
	public String editUI() throws Exception {
		Role role = roleService.getById(model.getId());
		//放到栈顶
		ActionContext.getContext().getValueStack().push(role);
		return "saveUI";
	}
	/**改动*/
	public String edit() throws Exception {
		//从数据库中取出原对象
		Role role = roleService.getById(model.getId());
		
		//设置要改动的属性
		role.setName(model.getName());
		role.setDescription(model.getDescription());
		
		//更新到数据库中
		roleService.update(role);
		
		return "toList";
	}
	/**设置权限界面*/
	public String setPrivilegeUI(){
		List<Privilege> privilegeList = privilegeService.findAll();
		ActionContext.getContext().put("privilegeList", privilegeList);
		return "setPrivilegeUI";
	}
	//=================================================
	public Long[] getPrivilegeIds() {
		return privilegeIds;
	}
	public void setPrivilegeIds(Long[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}
	
}
struts.xml
<!-- 岗位管理 -->
		<action name="roleAction_*" class="roleAction" method="{1}">
			<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
			<result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result>
			<result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result>
			<result name="toList" type="redirectAction">roleAction_list</result>
		</action>
PrivilegeService.java
package cn.xbmu.oa.service;

import cn.xbmu.oa.base.BaseDao;
import cn.xbmu.oa.domain.Privilege;

public interface PrivilegeService extends BaseDao<Privilege>{

}
PrivilegeServiceImpl.java
package cn.xbmu.oa.service.impl;

import org.springframework.stereotype.Service;

import cn.xbmu.oa.base.BaseDaoImpl;
import cn.xbmu.oa.domain.Privilege;
import cn.xbmu.oa.service.PrivilegeService;

@Service
public class PrivilegeServiceImpl extends BaseDaoImpl<Privilege> implements
		PrivilegeService {	
	
}
setPrivilegeUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>配置权限</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
	<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
	<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/file.css" />

</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>
    <form action="list.html">
        <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" /> 正在为【部门经理】配置权限 </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">
							<!-- //TODO:显示权限树 -->
							<td>
								<s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id" listValue="name" />
							</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>
    </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>

执行效果:


数据回显:

RoleAction.java
/**设置权限界面*/
	public String setPrivilegeUI(){
		
		//准备数据
		Role role = roleService.getById(model.getId());
		ActionContext.getContext().put("role", role);
		
		List<Privilege> privilegeList = privilegeService.findAll();
		ActionContext.getContext().put("privilegeList", privilegeList);
		//回显数据
		privilegeIds = new Long[role.getPrivileges().size()];
		int index = 0;
		for(Privilege privilege : role.getPrivileges()){
			privilegeIds[index++] = privilege.getId();
		}
		
		return "setPrivilegeUI";
	}
	/**设置权限*/
	public String setPrivilege(){
		//1.取出源对象
		Role role = roleService.getById(model.getId());
		//2.设置权限属性
		List<Privilege> privileges = privilegeService.getByIds(privilegeIds);
		role.setPrivileges(new HashSet<Privilege>(privileges));
		//3.保存到数据库
		roleService.update(role);
		return "toList";
	}
setPrivilegeUI.jsp
<s:form action="roleAction_setPrivilege">
    	<s:hidden name="id"/>
        <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" /> 正在为【部门经理】配置权限 </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">
							<!-- //TODO:显示权限树 -->
							<td>
								<s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id" listValue="name" />
							</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>
执行效果:

打开数据库。里面已经有数据


posted on 2018-01-31 16:57  yjbjingcha  阅读(173)  评论(0编辑  收藏  举报

导航