权限分配-用户分配角色
①概要设计-建表,建实体对象
②用户列表页面增加一个分配角色的按钮,触发事件函数时,传递当前行的用户id和name
<a href="#" class="layui-btn layui-btn layui-btn-normal layui-btn-xs" onclick="toSetRole('${user.uid}','${user.uname}')">
<i class="layui-icon"></i>分配角色
</a>
请注意:#必须加上,不然后续点击时页面无法正常跳转
③AJAX请求“设置角色”网页模板内容,准备将内容展示在弹出层中,弹出对话框 layui-layer弹出层
function toSetRole(uid,uname) {
layui.use(['layer'],function() {
var layer = layui.layer;
//使用AJAX方式去请求“分配角色”网页模板
//js中有3中常用的请求方式,location.href window.open() ajax
//1.创建xml对象
var xml=new XMLHttpRequest();
//2.设置请求信息
xml.open('get','setRole.jsp?uid='+uid+'&uname=',true);
//3.提供回调函数,告诉AJAX接收响应后,就调用该函数,该函数处理响应
function doBack(responseText) {
//具体的处理逻辑,和AJAX无关
layer.open({
type:1,
area:[600,500],
title:'分配角色',
content:responseText //需要弹出层展示的网页模板内容,请求
})
}
xml.onreadystatechange=function () {
if(xml.readyState==4){
if(xml.status==200) {
//接受了完整的正确的响应,可以将响应交给指定的回调函数处理
doBack(xml.responseText);
}else {
alert('出现未知错误,请重试!');
}
}
}
//发送请求
xml.send();
});
}
④经过思考,我们需要先访问服务器,查询角色列表,以供分配,所以我们将第10行代码xml.open('get','setRole.jsp?uid='+uid+'&uname=',true);
改为xml.open('get','toSetRole?uid='+uid+'&uname='+uname,true);
如果当前用户已经分配过角色,展示时需要默认勾选
可以自定义domain类装载上述查询结果
package controller;
import myweb.ModelAndView;
import myweb.annotation.Controller;
import myweb.annotation.RequestMapping;
import myweb.annotation.RequestParam;
import service.UserService;
import java.util.List;
import java.util.Map;
@Controller
public class UserController {
private UserService userService=new UserService();
@RequestMapping("/toSetRole")
public ModelAndView findRole(@RequestParam("uid") int uid){
//此次请求需要一个设置角色网页模板
//在模板中需要展示所有角色列表(携带角色信息)
//在模板中需要默认勾选上一次分配的角色(携带上一次的角色信息)
List<Map> setRoleInfoList= userService.findRoleAllByUser(uid);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("setRoleInfoList",setRoleInfoList);
modelAndView.setViewName("setRole.jsp");
return modelAndView;
}
}
UserService部分
/*
查询所有角色信息(List),uid这个用户上一次分配的信息(List)
角色信息携带到网页后,需要展示,需要所有信息(List<Role>)
上一次分配的角色信息携带到网页后,需要根据信息默认勾选(List<rid>)
将上述所需要的信息整合在一起,List中每一条记录都包含既有角色信息+该角色是否勾选信息
*/
public List<Map> findRoleAllByUser(int uid){
return roleDao.findAllByUser(uid);
}
RoleDao部分
@Select("select r.* , if(ur.rid is null,0,1) flag from t_role r left join " +
"(select rid from t_user_role where uid = #{uid}) ur on r.rid = ur.rid")
public List<Map> findAllByUser(int uid);
页面数据处理
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%--设计分配角色的页面--%>
<html>
<head>
</head>
<body>
<div class="layui-card">
<div class="layui-card-header">
用户编号:<input class="layui-input layui-input-inline" value="${param.uid}" id="uid" name="uid" style="width:150px;" />
用户名称:<input class="layui-input layui-input-inline" value="${param.uname}" id="uname" name="uname" style="width:150px;" />
</div>
<div class="layui-card-body">
<table class="layui-table">
<thead>
<tr>
<th><input type="checkbox" /></th>
<th>角色编号</th>
<th>角色名称</th>
<th>角色描述</th>
</tr>
</thead>
<tbody>
<c:forEach var="role" items="${setRoleInfoList}">
<tr>
<th><input type="checkbox" value="${role.rid}" <c:if test="${role.flag==1}" >checked</c:if> /></th>
<th>${role.rid}</th>
<th>${role.rname}</th>
<th>${role.rdescription}</th>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>