easyUI高级控件(1)
程序员文章站
2024-02-03 22:01:16
...
权限功能的简介
一星权限:
1、通过账号密码查询用户表,为了获取菜单的id
2、获取到菜单id后,可以加载对应的菜单以及子菜单
不利于:
这种权限只能加载一组父子关系的菜单、就是一对一的关系
看下图(图解)
二星权限:
设计思路
1、用户登陆查询用户表,有数据则代表已经注册好了
2、通过UID查询中间表,获得一个权限集合对于多个数据
3、获取到一个menuid的集合,通过集合,用in关键字的到多组父子关系的菜单
如下图:
权限功能代码案例
一星权限:
首先创建两个数据库表, 用户表中需要有一个menuId,当你登陆时获取到这个menuId,在菜单表中查询是否有这个值;如果有就将查询到的菜单表的数据以json的格式展示在easyui中,最难的部分是转换这里。
代码如下:
public class MenuDao extends JsonBaseDao{
public List<Map<String, Object>> menuList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_menu where true";
String menuId=JsonUtil.getparamMap(paramMap, "Menuid");
//判断值的存在性
if (StringUtils.isNotBlank(menuId)) {
sql+=" and parentid="+menuId;
}
else{
sql+=" and parentid=-1";
}
return super.executeQuery(sql, null);
}
private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
//treeNode.setChildren(children);
Map<String, String[]> paramMap=new HashMap<>();
//把当前节点当作父id,查出所有子节点
paramMap.put("Menuid", new String[] {treeNode.getId()});
List<Map<String, Object>> menuList=this.menuList(paramMap, null);
List<TreeNode> treeNodeList=new ArrayList<>();
mapListToTreeNodeList(menuList, treeNodeList);
treeNode.setChildren(treeNodeList);
}
public void mapListToTreeNodeList(List<Map<String, Object>> list,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode=null;
for (Map<String, Object> map : list) {
treeNode=new TreeNode();
mapToTreeNode(map, treeNode);
treeNodeList.add(treeNode);
}
}
/**
* 这个方法才是符合easyui树形的组建所需要的json格式
*
* @param paramMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<TreeNode> getEndList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Map<String, Object>> menuList=this.menuList(paramMap, null);
List<TreeNode> treeNodeList=new ArrayList<>();
mapListToTreeNodeList(menuList, treeNodeList);
return treeNodeList;
}
}
编写Action
//一星权限
public String logina(HttpServletRequest req,HttpServletResponse resp) {
try {
List<Map<String, Object>> userList = ud.userLista(req.getParameterMap(), null);
Map<String, Object> userMap = userList.get(0);
if(userMap!=null) {
String menuID = (String) userMap.get("Menuid");
req.setAttribute("menuID", menuID);
}else {
return "tologin";
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toindex";
}
书写一个通用的关流、刷新方法类
public static void write(HttpServletResponse resp,Object obj) throws IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println(obj.toString());
out.flush();
out.close();
}
调用时的方法
List<TreeNode> endList = this.mtDao.getEndList(req.getParameterMap(), null);
ObjectMapper om=new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(endList));
二星权限
1、编写二星权限MenuDao
/**
* 二星权限
* @param paramMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> userListb(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String uid = JsonUtil.getparamVal(paramMap, "uid");
String upwd = JsonUtil.getparamVal(paramMap, "upwd");
String sql=" select * from t_easyui_user_version2 where true ";
if(StringUtils.isNotBlank(uid)) {
sql+=" and uid="+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql+=" and upwd="+upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 根据UID查询权限中间表
*
* @param paramMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> getMenuid(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String uid = JsonUtil.getparamVal(paramMap, "uid");
String sql=" select * from t_easyui_usermenu where true ";
if(StringUtils.isNotBlank(uid)) {
sql+=" and uid="+uid;
}
return super.executeQuery(sql, pageBean);
}
按思路,编写Action代码:
//二星权限
public String loginb(HttpServletRequest req,HttpServletResponse resp) {
try {
List<Map<String, Object>> userList = ud.userListb(req.getParameterMap(), null);
Map<String, Object> userMap = userList.get(0);
if(userMap!=null) {
Map<String, String[]> paramMap=new HashMap<>();
paramMap.put("uid", new String[] {userMap.get("uid").toString()});
List<Map<String, Object>> menuids = ud.getMenuid(paramMap, null);
StringBuffer sb=new StringBuffer();
for (Map<String, Object> map : menuids) {
sb.append(",").append(map.get("menuId"));
}
req.setAttribute("menuID", sb.toString().substring(1));
}else {
return "tologin";
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toindex";
}
最后编写index.js
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=treeMenu&menuHid='+$("#menuHid").val(),
onClick:function(node){
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURl+'" width="99%" height="99%"></iframe>';
if($('menuTabs').tabs('exists',note.text)){
$('menuTabs').tabs('exists',note.text)
}
else{
$('#menuTabs').tabs('add',{
title:note.text,
content:content,
closable:true,
tools:[{
iconCls:'icon-mini-refresh',
handler:function(){
alert('refresh');
}
}]
});
}
}
});
})
以上就时一星与二星权限的思路与用法,如有不懂可以在下方咨询我噢!!!!