角色授权:
菜单链接/admin/power/goPower
PowerController.java对应的action
@RequestMapping("/goPower")
public String goPower() {
return "power";
}
四部分:
1)显示角色下拉列表框
step1 页面部分新增下拉列表power.jsp
<input id="roles" name="Country" showNullItem="true" class="mini-combobox"
url="<%=request.getContextPath()%>/admin/power/getRole"
emptyText="请选择..."
nullItemText="请选择..."
textField="rolename" valueField="roleid" onvaluechanged="onDeptChanged" />
其中:
class="mini-combobox" (下拉列表固定写法)
url:是初始化时请求的后台地址 会返回对象集合,集合里有role对象,role对象里有rolename,roleid,通过如下形式进行前后台绑定
emptyText="请选择..." 初始显示的内容
textField="rolename" valueField="roleid"
step 2后台PowerController.java
@RequestMapping("/getRole")
public @ResponseBody List<Role> getRole(){
//查询所有的角色
List<Role> roles=iPowerSercive.getRole();
System.out.println(roles);
return roles;
}
//省略service mapper代码
2)显示菜单(权限)checkbox
step1 页面部分新增下拉列表power.jsp
<div id="powers" class="mini-checkboxlist" repeatItems="1"
repeatLayout="flow"
url="<%=request.getContextPath()%>/admin/power/getPower"
textField="text" valueField="id"
value=""
enable="false"
style="width: 725px; height: 250px;"></div>
其中:
class="mini-checkboxlist" (checkbox固定写法)
url:是初始化时请求的后台地址 会返回对象集合,集合里有role对象,role对象里有id,text,通过如下形式进行前后台绑定
value="" 初始选中的内容(多个选项值之间用逗号分隔。框架支持的格式是1,2,3)
textField="text" valueField="id"
step 2后台PowerController.java
@RequestMapping("/getPower")
public @ResponseBody List<Menu> getPower(){
List<Menu> power=iPowerSercive.getPower();
return power;
}
//省略service mapper代码
-
实现级联。选中某个角色时,对应的菜单权限变化
首选页面部分,第一部分里
onvaluechanged="onDeptChanged" 触发一上事件<script type="text/javascript">
mini.parse();
//获取roles,powers
var roles = mini.get("roles");
var powers = mini.get("powers");
var oldpowers;
//role为请选择时,power不可编辑
powers.disable();
function onDeptChanged(e) {
//获取选中的角色的id
var roleid = roles.getValue();
// 发送请求,查询该角色已有的权限。查询结果是多个权限id用逗号分隔。1,2,3
$.ajax({
type:"get",
url:"<%=request.getContextPath()%>/admin/power/getPowerById?roleid="+roleid,
dataType:"json",
success:function(rst){
powers.setValue(rst.msg);
oldpowers=powers.getValue();
if(roleid=="1"||roleid==""){
powers.disable();
}else{
powers.enable();
}}
})
}...
</script>
controller
@RequestMapping("/getPowerById")
public @ResponseBody ResultMsg getPowerById(String roleid){
ResultMsg msg=iPowerSercive.getPowerById(roleid);
return msg;
}
IPowerSercive
ResultMsg getPowerById(String roleid);
PowerServiceImpl
@Override
public ResultMsg getPowerById(String roleid) {
ResultMsg rst=new ResultMsg();
Role role=new Role();
role.setRoleid(roleid);
List<String> s=iPowerMapper.getPowerById(role);
String powers=String.join(",",s);
rst.setMsg(powers);
return rst;
}
4)添加
<input type="button" value="保存" onclick="setPowers()" />
对应的js
function setPowers() {
//获取选中的权限值,逗号分隔
var newpowers=powers.getValue();
//获取角色id
var roleid=roles.getValue();
//如果没有变化 ,提示一下
if(oldpowers==newpowers){
alert("请设置后再保存")
}else{//如果权限的变更,发请求到后台,更改权限列表
$.ajax({
type:"get",
url: "<%=request.getContextPath()%>/admin/power/setPowers?roleid="+roleid+"&oldpowers="+oldpowers+"&newpowers="+newpowers,
dataType:"json",
success : function(rst) {
alert(rst.msg)
roles.setValue("");
powers.setValue("");
powers.disable();
}
});
}
}
controller
@RequestMapping("/setPowers")
public @ResponseBody ResultMsg setPowers(String roleid,String oldpowers,String newpowers){
ResultMsg msg=iPowerSercive.setPowers(roleid,oldpowers,newpowers);
return msg;
}
IPowerSercive接口
ResultMsg setPowers(String roleid, String oldpowers, String newpowers);
PowerServiceImpl实现类
@Override
public ResultMsg setPowers(String roleid, String oldpowers, String newpowers) {
//ResultMsg用来返回设置好的权限的
ResultMsg msg=new ResultMsg();
Map<String,String> addPowers;
int dels=0;
int adds=0;
boolean flagdel=DataComparison.getDelPowers(oldpowers);
if(flagdel){
dels=iPowerMapper.delPowers(roleid);
}
//把添加的权限拼装成map,为了foreach循环,做批量添加
addPowers=DataComparison.getAddPowers(newpowers);
if(addPowers!=null){
adds=iPowerMapper.addPowers(roleid,addPowers);
}
if(adds==addPowers.size()){
msg.setCode(1);
msg.setMsg("设置成功");
}else{
msg.setCode(0);
msg.setMsg("设置失败");
}
return msg;
}
IPowerMapper
@Delete("delete from t_role_menu where roleid=#{roleid} ")
int delPowers(@Param("roleid") String rolieid);
@Insert({
"<script>"
+ "insert into t_role_menu (id,roleid,menuid)"
+ "values "
+ "<foreach item='item' index='index' collection='addPowers' separator=','>"
+ "(#{index},#{roleid},#{item})"
+ "</foreach>"
+ "</script>"
})
int addPowers(@Param("roleid") String roleid,@Param("addPowers") Map<String,String> addPowers);
DataComparison类
package com.neuedu.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataComparison {
public static boolean getDelPowers(String oldpowers) {
List<String> powerList = new ArrayList<String>();
if (oldpowers.equals("") || oldpowers.equals(null)) {
return false;
} else {
return true;
}
}
public static Map<String, String> getAddPowers(String newpowers) {
Map<String, String> powerMap = new HashMap<>();
if (newpowers.equals("") || newpowers.equals(null)) {
return null;
} else {
String[] addpowers = newpowers.split(",");
for (int i = 0; i < addpowers.length; i++) {
String uuid = UuidGenerateUtil.getUUID();
powerMap.put(uuid, addpowers[i]);
}
return powerMap;
}
}
}
完整界面代码power.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Binding 数据绑定</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="<%=request.getContextPath()%>/css/demo.css" rel="stylesheet"
type="text/css" />
<script src="<%=request.getContextPath()%>/js/boot.js"
type="text/javascript"></script>
</head>
<body>
<h1>角色授权</h1>
<!-- 下拉列表框,用class="mini-combobox"属性控制 -->
<input id="roles" name="Country" showNullItem="true" class="mini-combobox"
url="<%=request.getContextPath()%>/admin/power/getRole"
emptyText="请选择..."
nullItemText="请选择..."
textField="rolename" valueField="roleid" onvaluechanged="onDeptChanged" />
<div id="powers" class="mini-checkboxlist" repeatItems="1"
repeatLayout="flow"
url="<%=request.getContextPath()%>/admin/power/getPower"
textField="text" valueField="id"
value=""
enable="false"
style="width: 725px; height: 250px;"></div>
<div style="padding-top: 5px; padding-bottom: 5px;">
<input type="button" value="保存" onclick="setPowers()" />
</div>
<div class="description">
<h3>Description</h3>
</div>
<script type="text/javascript">
mini.parse();
/* var roles = document.getElementById("roles"); */
var roles = mini.get("roles");
var powers = mini.get("powers");
var oldpowers;
powers.disable();
function onDeptChanged(e) {
var roleid = roles.getValue();
$.ajax({
type:"get",
url:"<%=request.getContextPath()%>/admin/power/getPowerById?roleid="+roleid,
dataType:"json",
success:function(rst){
//该角色下的权限被选中
powers.setValue(rst.msg);
oldpowers=powers.getValue();
if(roleid=="1"||roleid==""){
powers.disable();
}else{
powers.enable();
}
}
})
}
function setPowers() {
//获取选中的权限值,逗号分隔
var newpowers=powers.getValue();
//获取角色id
var roleid=roles.getValue();
//如果没有变化 ,提示一下
if(oldpowers==newpowers){
alert("请设置后再保存")
}else{//如果权限的变更,发请求到后台,更改权限列表
$.ajax({
type:"get",
url: "<%=request.getContextPath()%>/admin/power/setPowers?roleid="+roleid+"&oldpowers="+oldpowers+"&newpowers="+newpowers,
dataType:"json",
success : function(rst) {
alert(rst.msg)
roles.setValue("");
powers.setValue("");
powers.disable();
}
});
}
}
</script>
</body>
</html>