简单权限概念
权限目的:
是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单
1.一星权限设计(用户权限多对一)
执行数据库脚本
建立实体类
创建dao
Web层创建
更改展示的树形菜单
思考:
我们想一个用户对应多个菜单
然后一个菜单可以对应多个用户
其实这就是user与menu的多对多的关系
思路:
1、菜单不同的原因在于,利用不同menuid进行查询,原本默认查询的是所有菜单,是通过-1去查的;
2、menuid由来:是登录用户id查询中间表数据所得来的
2、二星权限设计(用户权限多对多)
2.1、执行数据库脚本
2.2、修改原有的实体类
2.3、建立实体类
2.4、创建dao
2.5、修改原有的dao
2.6、新增web的方法
代码:
UserDao.java
package com.bk201.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.bk201.util.JsonBaseDao;
import com.bk201.util.JsonUtils;
import com.bk201.util.PageBean;
import com.bk201.util.StringUtils;
public class UserDao extends JsonBaseDao{
/**
* 用户登陆或者查询用户分页信息的方法
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> list(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user_version2 where true";
String uid =JsonUtils.getParamVal(paMap, "uid");
String upwd=JsonUtils.getParamVal(paMap, "upwd");
if(StringUtils.isNotBlank(uid)) {
sql+= " and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql+= " and upwd = "+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 根据当前用户登陆的id去查询对应的所有菜单
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> getMenuByUid(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user_version2 where true";
String uid =JsonUtils.getParamVal(paMap, "uid");
if(StringUtils.isNotBlank(uid)) {
sql+= " and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
}
web下
UserAction.java
package com.bk201.web;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bk201.dao.UserDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao userDao = new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) {
String code = "index";
// 登录
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
if (list.size() == 1 && list != null) {
// 用户存在
List<Map<String, Object>> menusList = this.userDao.getMenuByUid(req.getParameterMap(), null);
StringBuilder sb = new StringBuilder();
for (Map<String, Object> map : menusList) {
sb.append("," + map.get("menuId"));
System.out.println(sb.toString());
}
// ,001,002
req.setAttribute("menuIds", sb.substring(1));
} else {
// 用户不存在
req.setAttribute("msg", "用户不存在");
code = "login";
}
} catch (Exception e) {
e.printStackTrace();
code = "login";
}
return code;
}
}
mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action> -->
<action path="/menuAction" type="com.bk201.web.MenuAction">
</action>
<action path="/userAction" type="com.bk201.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false" />
</action>
</config>
js文件
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree&&Menuid'+$("#menuIds").val(),
onClick: function(node){
//alert(node.attributes.menuURL); // 在用户点击的时候提示
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
if($("#menuTab").tabs('exists',node.text)){
//alert("存在")
$("#menuTab").tabs('select',node.text)
}else{
//alert("不存在")
// add a new tab panel
$('#menuTab').tabs('add',{
title:node.text,
content:content,
closable:true
});
}
}
});
})
页面文件代码
index.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">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/index.js"></script>
<title>后台管理主界面</title>
</head>
<body class="easyui-layout">
<input type="hidden" id="menuIds" value="${menuIds }">
<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">north region</div>
<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
左侧的菜单栏加载区域
<ul id="tt"></ul>
</div>
<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
<div data-options="region:'center',title:'Center'">
<div id="menuTab" class="easyui-tabs" style="height: 900px">
<div title="首页" style="padding:20px;display:none;">
首页擦擦擦
</div>
</div>
</div>
</body>
</html>
login.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>
</head>
<body>
<form action="${pageContext.request.contextPath }/userAction.action?methodName=login" method="post">
uid:<input type="text" name="uid"><br>
upwd:<input type="text" name="upwd"><br>
<input type="submit">
</form>
${msg }
<%-- <span style="color:red;">${msg }</span> --%>
</body>
</html>
运行结果ID=001
运行结果ID=003
运行结果ID=005