RoleAction:
package com.xushuai.bos.web.action;
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.xushuai.bos.entity.Role;
import com.xushuai.bos.service.RoleService;
import com.xushuai.bos.utils.BOSUtils;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
@Controller("roleAction")
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {
@Autowired
@Qualifier("roleService")
private RoleService roleService;
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
//接收权限ID
private String functionIds;
public String getFunctionIds() {
return functionIds;
}
public void setFunctionIds(String functionIds) {
this.functionIds = functionIds;
}
//接收要删除的角色的ID
private String ids;
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
/**
* 新增角色
* @return
*/
public String add(){
roleService.save(model,functionIds);
return LIST;
}
/**
* 分页查询角色信息
* @return
*/
public String pageQuery(){
roleService.pageQuery(pageBean);
BOSUtils.writerJson(pageBean, new String[]{"currentPage","pageSize","criteria","functions","users"});
return NONE;
}
/**
* 删除指定角色(可以为多个)
* @return
*/
public String delete(){
roleService.delete(ids);
return LIST;
}
/**
* 按角色查询其权限
* @return
* @throws IOException
*/
public String findFunctionById() throws IOException{
Role role = roleService.findById(model.getId());
Set functions = role.getFunctions();
HttpServletResponse response = BOSUtils.getResponse();
response.setContentType("text/json;charset=UTF-8");
JsonConfig jsonConfig = new JsonConfig();
//去除不需要返回的值
jsonConfig.setExcludes(new String[]{"currentPage","pageSize","criteria","parentFunction","roles","children","users"});
//将查询结果转换为json串
String json = JSONArray.fromObject(functions, jsonConfig).toString();
response.getWriter().print(json);
return NONE;
}
/**
* 修改角色信息
* @return
*/
public String edit(){
roleService.edit(model, functionIds);
return LIST;
}
}
RoleService、RoleServiceImpl:
package com.xushuai.bos.service;
import com.xushuai.bos.entity.Role;
import com.xushuai.bos.utils.PageBean;
public interface RoleService {
/**
* 新增角色
* @param model
*/
void save(Role model, String functionIdS);
/**
* 分页查询角色
* @param pageBean
*/
void pageQuery(PageBean pageBean);
/**
* 删除指定角色(可以为多个)
* @param ids
*/
void delete(String ids);
/**
* 按ID查询角色
* @param id
* @return
*/
Role findById(String id);
/**
* 修改角色信息
* @param model
* @param functionIds
*/
void edit(Role model, String functionIds);
}
package com.xushuai.bos.service.impl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xushuai.bos.dao.RoleDao;
import com.xushuai.bos.entity.Function;
import com.xushuai.bos.entity.Role;
import com.xushuai.bos.service.RoleService;
import com.xushuai.bos.utils.PageBean;
@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {
@Autowired
@Qualifier("roleDao")
private RoleDao roleDao;
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
@Override
public void save(Role model, String functionIdS) {
roleDao.save(model);
if(StringUtils.isNotBlank(functionIdS)){
String[] functionIds = functionIdS.split(",");
Function function = null;
for (String functionId : functionIds) {
function = new Function(functionId);
model.getFunctions().add(function);
}
}
}
@Override
public void pageQuery(PageBean pageBean) {
roleDao.findByPage(pageBean);
}
@Override
public void delete(String ids) {
if(StringUtils.isNotBlank(ids)){
String[] deleteIds = ids.split(",");
Role role = null;
for (String id : deleteIds) {
role = roleDao.findById(id);
roleDao.delete(role);
}
}
}
@Override
public Role findById(String id) {
return roleDao.findById(id);
}
@Override
public void edit(Role model, String functionIds) {
//查询出数据库中的role对象
Role _role = roleDao.findById(model.getId());
_role.setCode(model.getCode());
_role.setDescription(model.getDescription());
_role.setName(model.getName());
_role.getFunctions().clear();
if(StringUtils.isNotBlank(functionIds)){
String[] fIds = functionIds.split(",");
Function function = null;
for (String functionId : fIds) {
function = new Function(functionId);
_role.getFunctions().add(function);
}
}
roleDao.update(_role);
}
}
struts.xml(新增配置):
<!-- 角色管理模块 -->
<action name="RoleAction_*" class="roleAction" method="{1}">
<result name="list">/WEB-INF/pages/admin/role.jsp</result>
</action>
页面(Role.jsp、Role_add.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 导入jquery核心类库 -->
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<!-- 导入easyui类库 -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/ext/portal.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/css/default.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script>
<script
src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"
type="text/javascript"></script>
<!-- 导入ztree类库 -->
<link rel="stylesheet"
href="${pageContext.request.contextPath }/js/ztree/zTreeStyle.css"
type="text/css" />
<script
src="${pageContext.request.contextPath }/js/ztree/jquery.ztree.all-3.5.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function(){
// 数据表格属性
$("#grid").datagrid({
toolbar : [
{
id : 'add',
text : '添加角色',
iconCls : 'icon-add',
handler : function(){
location.href='${pageContext.request.contextPath}/page_admin_role_add.action';
}
},
{
id : 'delete',
text : '删除角色',
iconCls : 'icon-cancel',
handler : function(){
var rows = $("#grid").datagrid('getSelections');
if(rows.length == 0){
$.messager.alert('提示信息','请选择您要删除的角色','warning');
}else{
$.messager.confirm('提示信息','确定要执行删除操作吗?',function(r){
if(r){
//定义一个数组,存放要删除功能的ID值
Array :arr = new Array();
for(var i = 0; i < rows.length; i++){
//将需要删除的权限ID添加到数组中
arr.push(rows[i].id);
}
//将数组格式化成一个字符串,用逗号分隔每个ID值
var ids = arr.join(",");
location.href = "RoleAction_delete.action?ids=" + ids;
}
});
}
}
},
{
id : 'clear',
text : '取消选中',
handler : function(){
$("#grid").datagrid('clearSelections');
}
}
],
url : 'RoleAction_pageQuery.action',
pageList: [15,20,28],
pageSize: 28,
pagination : true,
fit:true,
onDblClickRow : doDblClickRow,
columns : [[
{
field : 'id',
title : '编号',
checkbox : true
},
{
field : 'code',
title : '关键字',
width : 200
},
{
field : 'name',
title : '名称',
width : 200
},
{
field : 'description',
title : '描述',
width : 200
}
]]
});
// 修改权限窗口
$('#editRoleWindow').window({
title: '编辑权限',
width: 800,
modal: true,
shadow: true,
closed: true,
height: 600,
resizable:false
});
$("#edit").click(function(){
if($("#editRoleForm").form('validate')){
var arr = new Array();
var treeObj = $.fn.zTree.getZTreeObj("functionTree");
var nodes = treeObj.getCheckedNodes(true);
if(nodes.length != 0){
for(var i = 0; i < nodes.length; i++){
arr.push(nodes[i].id);
}
}
var functionIds = arr.join(",");
$("input[name=functionIds]").val(functionIds);
$("#editRoleForm").submit();
}
});
});
//编辑角色信息
function doDblClickRow(rowIndex, rowData){
$('#editRoleWindow').window('open');
$('#editRoleWindow').form('load',rowData);
// 授权树初始化
var setting = {
data : {
key : {
title : "t"
},
simpleData : {
enable : true
}
},
check : {
enable : true,
}
};
$.ajax({
url : '${pageContext.request.contextPath}/FunctionAction_ajaxList.action',
type : 'POST',
dataType : 'json',
success : function(data) {
$.fn.zTree.init($("#functionTree"), setting, data);
$.post('RoleAction_findFunctionById.action',{'id':rowData.id},function(success){
var treeObj = $.fn.zTree.getZTreeObj("functionTree");
for(var i=0; i < success.length; i++){
var node = treeObj.getNodesByParam("id", success[i].id, null)[0];
treeObj.checkNode(node, true, false);
}
});
},
error : function(msg) {
alert('树加载异常!');
}
});
}
</script>
</head>
<body class="easyui-layout">
<div data-options="region:'center'">
<table id="grid"></table>
</div>
<!-- 编辑权限信息 -->
<div class="easyui-window" title="对权限信息进行修改" id="editRoleWindow"
collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
<div class="datagrid-toolbar">
<a id="edit" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >修改</a>
</div>
</div>
<div region="center" style="overflow:auto;padding:5px;" border="false">
<form id="editRoleForm" action="RoleAction_edit.action" method="post">
<input type="hidden" name="functionIds">
<input type="hidden" name="id">
<table class="table-edit" width="80%" align="center">
<tr class="title">
<td colspan="2">角色信息</td>
</tr>
<tr>
<td>关键字</td>
<td><input type="text" name="code" class="easyui-validatebox" data-options="required:true" /></td>
</tr>
<tr>
<td>名称</td>
<td><input type="text" name="name" class="easyui-validatebox" data-options="required:true" /></td>
</tr>
<tr>
<td>描述</td>
<td>
<textarea name="description" rows="4" cols="60"></textarea>
</td>
</tr>
<tr>
<td>授权</td>
<td>
<ul id="functionTree" class="ztree"></ul>
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 导入jquery核心类库 -->
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<!-- 导入easyui类库 -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/ext/portal.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/css/default.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script>
<script
src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"
type="text/javascript"></script>
<!-- 导入ztree类库 -->
<link rel="stylesheet"
href="${pageContext.request.contextPath }/js/ztree/zTreeStyle.css"
type="text/css" />
<script
src="${pageContext.request.contextPath }/js/ztree/jquery.ztree.all-3.5.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function(){
// 授权树初始化
var setting = {
data : {
key : {
title : "t"
},
simpleData : {
enable : true
}
},
check : {
enable : true,
}
};
$.ajax({
url : '${pageContext.request.contextPath}/FunctionAction_ajaxList.action',
type : 'POST',
dataType : 'json',
success : function(data) {
$.fn.zTree.init($("#functionTree"), setting, data);
},
error : function(msg) {
alert('树加载异常!');
}
});
// 点击保存
$('#save').click(function(){
if($("#roleForm").form('validate')){
var arr = new Array();
var treeObj = $.fn.zTree.getZTreeObj("functionTree");
var nodes = treeObj.getCheckedNodes(true);
if(nodes.length != 0){
for(var i = 0; i < nodes.length; i++){
arr.push(nodes[i].id);
}
}
var functionIds = arr.join(",");
$("input[name=functionIds]").val(functionIds);
$("#roleForm").submit();
}
});
});
</script>
</head>
<body class="easyui-layout">
<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
<div class="datagrid-toolbar">
<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
</div>
</div>
<div region="center" style="overflow:auto;padding:5px;" border="false">
<form id="roleForm" action="RoleAction_add.action" method="post">
<input type="hidden" name="functionIds">
<table class="table-edit" width="80%" align="center">
<tr class="title">
<td colspan="2">角色信息</td>
</tr>
<tr>
<td>关键字</td>
<td><input type="text" name="code" class="easyui-validatebox" data-options="required:true" /></td>
</tr>
<tr>
<td>名称</td>
<td><input type="text" name="name" class="easyui-validatebox" data-options="required:true" /></td>
</tr>
<tr>
<td>描述</td>
<td>
<textarea name="description" rows="4" cols="60"></textarea>
</td>
</tr>
<tr>
<td>授权</td>
<td>
<ul id="functionTree" class="ztree"></ul>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>