今天我们来讲的是Easyui的权限设置
权限目的:
是为了让不同的用户可以操作系统中不同资源
直接点说就是不同的用户可以看到左侧不同的菜单!!
1.建立实体类
这个建立实体类我们前面就说了需要建立一个能够与tree菜单一致的实体类就不多说了
2.创建dao
package com.chenkang.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.chenkang.util.JsonBaseDao;
import com.chenkang.util.JsonUtils;
import com.chenkang.util.PageBean;
import com.chenkang.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_usermenu where true ";
String uid = JsonUtils.getParamVal(paMap, "uid");
if(StringUtils.isNotBlank(uid)) {
sql += " and uid = "+uid;
}
return super.executeQuery(sql, pageBean);
}
}
3.修改原有的dao
public List<Map<String, Object>> listMapAuth(Map<String , String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_easyui_menu where true";
String menuId = JsonUtils.getParamVal(paMap, "Menuid");
// 为什么将parentid改为menuId?
// 原因在于之前的方法只能查询当前节点的所有子节点集合,不能将当前节点给查询出来
// 002 --》002001,002002,002003...
// 002,002001,002002,002003...
//
if(StringUtils.isNotBlank(menuId)) {
sql +=" and menuId in ("+menuId+")";
}else {
sql +=" and menuId=000";
}
// 存放的是数据库中的菜单信息
List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
return listMap;
}
4.新增web的方法
package com.chenkang.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chenkang.dao.UserDao;
import com.chenkang.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao userDao=new UserDao();
/**
* 登录成功后跳转index.jsp
* @param req
* @param resp
* @return
* @throws Exception
*/
public String login(HttpServletRequest req,HttpServletResponse resp) throws Exception {
//系统中是否有当前登陆用户
// 有
Map<String, Object> map=null;
// 查询用户菜单中间表,获取对应menuid的集合
try {
map = this.userDao.list(req.getParameterMap(), null).get(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
req.setAttribute("msg", "用户不存在");
return "login";
}
if(map!=null && map.size()>0) {
// [{Menuid:002,...},{Menuid:003,...}]
// 002,003
StringBuilder sb=new StringBuilder();
List<Map<String,Object>> menuIdArr = this.userDao.getMenuByUid(req.getParameterMap(), null);
for (Map<String, Object> m : menuIdArr) {
sb.append(","+m.get("menuId"));
}
// ,002,003
req.setAttribute("menuIds", sb.substring(1));
return "index";
}else {
// 没有
req.setAttribute("msg", "用户不存在");
return "login";
}
}
}
5.新增登入界面,跳入前端树形菜单
我们需要新增的就是一个登陆的界面
<%@ 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">
upwd:<input type="text" name="upwd">
<input type="submit">
</form>
<span style="color:red;">${msg }</span>
</body>
</html>
还有就是我们需要在界面添加一个隐藏域不然js里面就会出错
这是index.jsp页面
<input type="hidden" id="menuIds" value="${menuIds }">
index.js界面
url:'menuAction.action?methodName=menuTree&&Menuid='+$("#menuIds").val(),
最后我们需要的是新建了web层以及jsp页面我们需要配置文件
<action path="/userAction" type="com.chenkang.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false" />
</action>
测试结果
登陆
这样就完成了!!!