赞
踩
CREATE TABLE `t_auth` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(200) DEFAULT NULL, `title` varchar(200) DEFAULT NULL, `category_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(1,'','用户模块',NULL); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(2,'user:delete','删除',1); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(3,'user:get','查询',1); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(4,'','角色模块',NULL); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(5,'role:delete','删除',4); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(6,'role:get','查询',4); INSERT INTO t_auth(id,`name`,title,category_id) VALUES(7,'role:add','新增',4);
CREATE TABLE `inner_role_auth` (
`role_id` int(11) ,
`auth_id` int(11) ,
PRIMARY KEY (`role_id`,`auth_id`)
);
0、
<table tableName="t_auth" domainObjectName="Auth" />
1、选择pom.xml----》 Run as —》 Maven build … —》将Goal的值设置为:mybatis-generator:generate----》Run,然后刷新
2、Auth.java添加有参,无参构造函数以及toString函数
3、逆向资源各就各位
1、/atcrowdfunding-admin-2-component/src/main/java/com/atguigu/crowd/funding/handler/AuthHandler.java
@Controller public class AuthHandler { @Autowired private AuthService authService; @ResponseBody @RequestMapping("/assign/get/all/auth") public ResultEntity<List<Auth>> getAllAuth() { List<Auth> authList = authService.getAllAuth(); return ResultEntity.successWithData(authList); } }
2、/atcrowdfunding-admin-2-component/src/main/java/com/atguigu/crowd/funding/service/impl/AuthServiceImpl.java
@Service
public class AuthServiceImpl implements AuthService{
@Autowired
private AuthMapper authMapper;
@Override
public List<Auth> getAllAuth() {
return authMapper.selectByExample(new AuthExample());
}
}
3、http://localhost:8080/atcrowdfunding-admin-1-webui/assign/get/all/auth.json
1、/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/include-modal-assign-auth.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <div id="roleAssignAuthModal" class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> <h4 class="modal-title">尚筹网系统弹窗</h4> </div> <div class="modal-body"> <ul id="treeDemo" class="ztree"></ul> </div> <div class="modal-footer"> <button id="roleAssignAuthBtn" type="button" class="btn btn-primary">分配</button> </div> </div> </div> </div>
2、/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/role-page.jsp最下面的body标签的上面引用
<%@ include file="/WEB-INF/include-modal-assign-auth.jsp" %>
</body>
1、/atcrowdfunding-admin-1-webui/src/main/webapp/script/my-role.js
var checkBtn = "<button roleId='"+role.id+"' type='button' class='btn btn-success btn-xs checkBtn'><i class=' glyphicon glyphicon-check'></i></button>";
2、/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/role-page.jsp
$("#roleTableBody").on("click",".checkBtn",function(){
// 将角色id存入全局变量
window.roleId = $(this).attr("roleId");
$("#roleAssignAuthModal").modal("show");
});
3、运行测试:点击权限管理—角色维护----编辑,跳出模态框了
/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/role-page.jsp
<link rel="stylesheet" href="ztree/zTreeStyle.css" />
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
zTree一定要在jQuery后面引入。如果A.js中用到了B.js里面的代码,那么B必须在A前面引入,否则A无法使用B中的代码。
var setting = {
"data": {
"simpleData": {
"enable": true,
"pIdKey": "categoryId"
},
"key": {
"name": "title"
}
},
"check": {
"enable": true
}
};
/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/role-page.jsp
$("#roleTableBody").on("click",".checkBtn",function(){ // 打开模态框 $("#roleAssignAuthModal").modal("show"); // 初始化模态框中显示的树形结构 // 1.创建setting对象 var setting = { "data": { "simpleData": { "enable": true, "pIdKey": "categoryId" }, "key": { "name": "title" } }, "check": { "enable": true } }; // 2.获取JSON数据 var ajaxResult = $.ajax({ "url": "assign/get/all/auth.json", "type": "post", "dataType": "json", "async": false }); if(ajaxResult.responseJSON.result == "FAILED") { layer.msg(ajaxResult.responseJSON.message); return ; } var zNodes = ajaxResult.responseJSON.data; // 3.初始化树形结构 $.fn.zTree.init($("#treeDemo"), setting, zNodes); });
@ResponseBody
@RequestMapping("/assign/get/assigned/auth/id/list")
public ResultEntity<List<Integer>> getAssignedAuthIdList(@RequestParam("roleId") Integer roleId) {
List<Integer> authIdList = authService.getAssignedAuthIdList(roleId);
return ResultEntity.successWithData(authIdList);
}
@Override
public List<Integer> getAssignedAuthIdList(Integer roleId) {
return authMapper.selectAssignedAuthIdList(roleId);
}
<select id="selectAssignedAuthIdList" resultType="int">
select auth_id from inner_role_auth where role_id=#{roleId}
</select>
$("#roleTableBody").on("click",".checkBtn",function(){ // 将角色id存入全局变量 window.roleId = $(this).attr("roleId"); $("#roleAssignAuthModal").modal("show"); // 1.创建setting对象 var setting = { "data": { "simpleData": { "enable": true, "pIdKey": "categoryId" }, "key": { "name": "title" } }, "check": { "enable": true } }; // 2.获取JSON数据 var ajaxResult = $.ajax({ "url": "assign/get/all/auth.json", "type": "post", "dataType": "json", "async": false }); if(ajaxResult.responseJSON.result == "FAILED") { layer.msg(ajaxResult.responseJSON.message); return ; } var zNodes = ajaxResult.responseJSON.data; // 3.初始化树形结构 $.fn.zTree.init($("#treeDemo"), setting, zNodes); // 4.将树形结构展开 $.fn.zTree.getZTreeObj("treeDemo").expandAll(true); // 5.查询以前已经分配过的authId ajaxResult = $.ajax({ "url": "assign/get/assigned/auth/id/list.json", "type": "post", "data": { "roleId": $(this).attr("roleId"), "random": Math.random() }, "dataType": "json", "async": false }); if(ajaxResult.responseJSON.result == "FAILED") { layer.msg(ajaxResult.responseJSON.message); return ; } var authIdList = ajaxResult.responseJSON.data; // 6.使用authIdList勾选对应的树形节点 // ①遍历authIdList for (var i = 0; i < authIdList.length; i++) { // ②在遍历过程中获取每一个authId var authId = authIdList[i]; // ③根据authId查询到一个具体的树形节点 // key:查询节点的属性名 // value:查询节点的属性值,这里使用authId var key = "id"; var treeNode = $.fn.zTree.getZTreeObj("treeDemo").getNodeByParam(key, authId); // ④勾选找到的节点 // treeNode:当前要勾选的节点 // true:表示设置为勾选状态 // false:表示不联动 $.fn.zTree.getZTreeObj("treeDemo").checkNode(treeNode, true, false); } });
※为什么不能联动:在联动模式下,子菜单A勾选会导致父菜单勾选,父菜单勾选会根据“联动”效果,把子菜单B、子菜单C也勾选,可是实际上B、C不应该勾选,这就会产生错误。
/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/role-page.jsp
// 给在模态框中的分配按钮绑定单击响应函数 $("#roleAssignAuthBtn").click(function(){ var authIdArray = new Array(); // 调用zTreeObj的方法获取当前已经被勾选的节点 var checkedNodes = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(); // 遍历checkedNodes for(var i = 0; i < checkedNodes.length; i++) { // 获取具体的一个节点 var node = checkedNodes[i]; // 获取当前节点的id属性 var authId = node.id; // 将authId存入数组 authIdArray.push(authId); } // 在handler方法中使用@RequestBody接收 // 方便使用的数据类型是:@RequestBody Map<String, List<Integer>> // {"roleIdList":[2],"authIdList":[2,3,5,7]} // 封装要发送给handler的JSON数据 var requestBody = {"roleIdList":[window.roleId], "authIdList": authIdArray}; // 发送请求 var ajaxResult = $.ajax({ "url": "assign/do/assign.json", "type": "post", "data": JSON.stringify(requestBody), "contentType": "application/json;charset=UTF-8", "dataType": "json", "async": false }); if(ajaxResult.responseJSON.result == "SUCCESS") { layer.msg("操作成功!"); } if(ajaxResult.responseJSON.result == "FAILED") { layer.msg(ajaxResult.responseJSON.message); } $("#roleAssignAuthModal").modal("hide"); });
@ResponseBody
@RequestMapping("/assign/do/assign")
public ResultEntity<String> doRoleAssignAuth(
@RequestBody Map<String, List<Integer>> assignDataMap) {
authService.updateRelationShipBetweenRoleAndAuth(assignDataMap);
return ResultEntity.successWithoutData();
}
@Override public void updateRelationShipBetweenRoleAndAuth(Map<String, List<Integer>> assignDataMap) { // 1.获取两部分List数据 List<Integer> roleIdList = assignDataMap.get("roleIdList"); List<Integer> authIdList = assignDataMap.get("authIdList"); // 2.取出roleId Integer roleId = roleIdList.get(0); // 3.删除旧数据 authMapper.deleteOldRelationship(roleId); // 4.保存新数据 if(CrowdFundingUtils.collectionEffective(authIdList)) { authMapper.insertNewRelationship(roleId, authIdList); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。