角色授權(quán):
菜單鏈接/admin/power/goPower
PowerController.java對應(yīng)的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:是初始化時(shí)請求的后臺地址 會返回對象集合,集合里有role對象,role對象里有rolename碌冶,roleid,通過如下形式進(jìn)行前后臺綁定
emptyText="請選擇..." 初始顯示的內(nèi)容
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)顯示菜單(權(quán)限)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:是初始化時(shí)請求的后臺地址 會返回對象集合,集合里有role對象,role對象里有id,text稻艰,通過如下形式進(jìn)行前后臺綁定
value="" 初始選中的內(nèi)容(多個(gè)選項(xiàng)值之間用逗號分隔扛禽》姹撸框架支持的格式是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代碼
-
實(shí)現(xiàn)級聯(lián)。選中某個(gè)角色時(shí)编曼,對應(yīng)的菜單權(quán)限變化
首選頁面部分宠默,第一部分里
onvaluechanged="onDeptChanged" 觸發(fā)一上事件<script type="text/javascript">
mini.parse();
//獲取roles,powers
var roles = mini.get("roles");
var powers = mini.get("powers");
var oldpowers;
//role為請選擇時(shí)灵巧,power不可編輯
powers.disable();
function onDeptChanged(e) {
//獲取選中的角色的id
var roleid = roles.getValue();
// 發(fā)送請求搀矫,查詢該角色已有的權(quán)限。查詢結(jié)果是多個(gè)權(quán)限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()" />
對應(yīng)的js
function setPowers() {
//獲取選中的權(quán)限值敏弃,逗號分隔
var newpowers=powers.getValue();
//獲取角色id
var roleid=roles.getValue();
//如果沒有變化 卦羡,提示一下
if(oldpowers==newpowers){
alert("請?jiān)O(shè)置后再保存")
}else{//如果權(quán)限的變更,發(fā)請求到后臺,更改權(quán)限列表
$.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實(shí)現(xiàn)類
@Override
public ResultMsg setPowers(String roleid, String oldpowers, String newpowers) {
//ResultMsg用來返回設(shè)置好的權(quán)限的
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);
}
//把添加的權(quán)限拼裝成map,為了foreach循環(huán)麦到,做批量添加
addPowers=DataComparison.getAddPowers(newpowers);
if(addPowers!=null){
adds=iPowerMapper.addPowers(roleid,addPowers);
}
if(adds==addPowers.size()){
msg.setCode(1);
msg.setMsg("設(shè)置成功");
}else{
msg.setCode(0);
msg.setMsg("設(shè)置失敗");
}
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 數(shù)據(jù)綁定</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>角色授權(quán)</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){
//該角色下的權(quán)限被選中
powers.setValue(rst.msg);
oldpowers=powers.getValue();
if(roleid=="1"||roleid==""){
powers.disable();
}else{
powers.enable();
}
}
})
}
function setPowers() {
//獲取選中的權(quán)限值,逗號分隔
var newpowers=powers.getValue();
//獲取角色id
var roleid=roles.getValue();
//如果沒有變化 瓶颠,提示一下
if(oldpowers==newpowers){
alert("請?jiān)O(shè)置后再保存")
}else{//如果權(quán)限的變更拟赊,發(fā)請求到后臺,更改權(quán)限列表
$.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>