• 82.角色管理Extjs 页面


    1.

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>角色权限分配</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22     <script type="text/javascript">
    23         var perAssign = new PerAssign();
    24         var tabId = Ext.getCmp('mainTab').getActiveTab().id.split('_')[1];
    25         juage(tabId,"role",perAssign,"perAssign");
    26         var width = Ext.getCmp('mainTab').getActiveTab().getInnerWidth();
    27          var height = Ext.getCmp('mainTab').getActiveTab().getInnerHeight();
    28         var activeTab = Ext.getCmp('perAssignId');
    29          if(activeTab){
    30                activeTab.setWidth(width);
    31                activeTab.setHeight(height);
    32          }
    33     </script>
    34     
    35   </head>
    36   <body>
    37       <div id="perAssign" ></div>
    38   </body>
    39 </html>

    2.

      1 PerAssign = Ext.extend(Ext.Panel,{
      2     id: 'perAssignId',
      3     constructor: function(){
      4         //创建树形面板
      5         var roleTreePanel = new Ext.tree.TreePanel({
      6             id: 'roleTree',
      7             //支持对树的单选
      8             //只允许选择一个结点
      9             //使用时,只需在声明树时,加上属性 checkModel: "single" 既可
     10             checkModel: 'single',
     11             rootVisible: false,
     12             //这是个重要的参数,用于方便的构建树形菜单,用于远程调用树状数据
     13             loader: new Ext.tree.TreeLoader({
     14                 dataUrl: 'role_list.action',
     15                 ////必须有该项,不然树节点无法出现选择框 
     16                 baseAttrs: { uiProvider: Ext.ux.TreeCheckNodeUI }//添加TreeCheckNodeUI插件
     17             }),
     18             //根节点
     19             root: new Ext.tree.AsyncTreeNode({
     20                 id: null
     21             }),
     22             //单击选中事件
     23             listeners: {'check':{
     24                     fn: function(){
     25                         Ext.getCmp('treegrid').remove();
     26                         setTimeout(this.refreshTree, 1000);
     27                     }
     28                 }
     29             },
     30             refreshTree: function(){
     31                 //获取所有选中的节点代码如下所示
     32                 var roleNode = Ext.getCmp('roleTree').getChecked();
     33                         if(roleNode == ""){
     34                         }
     35                         var roleId =  roleNode[0].id;
     36                         ///得到元素的加载对象(Ext.ElementLoader)  
     37                         // var loader=Ext.get('content-div').getLoader();  
     38                         Ext.getCmp('treegrid').getLoader().dataUrl='permission_list.action?roleId='+roleId;
     39                         //获取根节点;treeGrid.getRootNode()                                    
     40                         Ext.getCmp('treegrid').getLoader().load(treeGrid.getRootNode(), function(){});
     41             }
     42         });
     43         PerAssign.superclass.constructor.call(this,{
     44             layout: 'border',
     45             items:[{
     46                 region: 'west',
     47                  200,
     48                 layout: 'fit',//· fit 一个子元素将充满整个容器(如果多个子元素则只有一个元素充满整个容器)
     49                 border: true,
     50                 frame: true,
     51                 title: '角色列表',
     52                 collapsible: true,
     53                 //左边树形面板
     54                 items: [roleTreePanel]
     55             },{
     56                 region: 'center',
     57                 layout: 'fit',
     58                 items: [{
     59                     title: '角色权限分配',
     60                     // 500,
     61                     frame: 'true',
     62                     id: 'treegridPanel',
     63                     autoScroll: false,
     64                     xtype: 'panel',
     65                     items: [treeGrid],
     66                     buttonAlign: 'center',
     67                     buttons: [{
     68                         text: '保存',
     69                         handler: saveRoleFn
     70                     },{
     71                         text: '取消',
     72                         handler: function(){
     73 //                            var obj = Ext.query('*[name$=_rs]');
     74 //                            for(var i = 0; i < obj.length; i++){
     75 //                                var id = obj[i].id;
     76 //                                Ext.get(obj[i].id).on('click', function(event,obj){
     77 //                                    var flag = false;
     78 //                                    var menu_id = obj.id.split('_')[0];
     79 //                                    var node = Ext.getCmp("perTree").getNodeById(menu_id);
     80 //                                    //alert(node);
     81 //                                    //alert(node.attributes.checked);
     82 //                                     Ext.getCmp("perTree").getNodeById(menu_id).attributes.checked = true;
     83 //                                })
     84 //                            }
     85                         }
     86                     }]
     87                     
     88                 }]
     89             }],
     90             tbar: new Ext.Toolbar({
     91                     items: [{
     92                         text: '新建角色',
     93                         iconCls: 'add',
     94                         id: 'role_add',
     95                         hidden: 'true',
     96                         handler: newRoleFn
     97                     },{
     98                         text: '修改角色',
     99                         iconCls: 'update',
    100                         id: 'role_update',
    101                         hidden: 'true',
    102                         handler: updateRoleFn
    103                     },{
    104                         text: '删除角色',
    105                         iconCls: 'delete',
    106                         id: 'role_delete',
    107                         hidden: 'true',
    108                         handler: deleteRoleFn
    109                     }]                    
    110                 })
    111         });
    112     }
    113 });
    114 saveRoleFn = function(){
    115             var roleNode = Ext.getCmp('roleTree').getChecked();
    116             if(roleNode == ""){
    117                 Ext.Msg.alert('提示','请先选择角色');
    118                 return;
    119             }
    120             var checkedNodesIds = roleNode[0].id;
    121             var objs = Ext.query('*[name$=_rs]');
    122             Ext.each(objs, function(obj){
    123                 if(obj.checked){
    124                     checkedNodesIds +=","+obj.id;
    125                 }
    126             });
    127             if(checkedNodesIds == "") {
    128                 Ext.Msg.alert('提示','没有为角色分配权限');
    129                 return;
    130             }
    131             Ext.Ajax.request({
    132                 url: 'rolePer_save.action',
    133                 params: {
    134                     checkedNodesIds: checkedNodesIds
    135                 },
    136                 success: saveRoleSuccessFn,
    137                 failure: saveRoleFailureFn
    138             });
    139 };
    140 saveRoleSuccessFn = function(response, options){
    141     var datas = Ext.util.JSON.decode(response.responseText);
    142     Ext.Msg.confirm('提示',datas.msg,function(){
    143         
    144     })
    145 };
    146 saveRoleFailureFn = function(response, options){
    147     Ext.Msg.alert('提示','连接后台失败');
    148 };
    149 newRoleFn = function(){
    150     var roleAddWin = new RoleAddWin();
    151     roleAddWin.show();
    152 };
    153 updateRoleFn = function(){
    154     var roleNode = Ext.getCmp('roleTree').getChecked();
    155     if(roleNode == ""){
    156         Ext.Msg.alert('提示','请先选择角色!');
    157         return;
    158     }
    159     var roleId =  roleNode[0].id;
    160     var roleAddWin = new RoleAddWin();
    161     roleAddWin.text = '修改角色';
    162     roleAddWin.show();
    163     Ext.get('role_roleId').dom.value = roleId;
    164     Ext.get('role_roleName').dom.value = roleNode[0].text;
    165 };
    166 deleteRoleFn = function(){
    167     var roleNode = Ext.getCmp('roleTree').getChecked();
    168     if(roleNode == ""){
    169         Ext.Msg.alert('提示','请先选择角色!');
    170         return;
    171     }
    172     Ext.Msg.confirm('提示','将删除所有具有该角色的用户!',function(button, text){
    173         if(button == 'yes'){
    174             var roleId =  roleNode[0].id;
    175             Ext.Ajax.request({
    176                 url: 'role_delete.action',
    177                 params: {
    178                     id : roleId
    179                 },
    180                 success: function(response, options){
    181                     var datas = Ext.util.JSON.decode(response.responseText);
    182                     Ext.Msg.alert('提示',datas.msg,function(){
    183                         Ext.getCmp('roleTree').getLoader().dataUrl='role_list.action';
    184                         Ext.getCmp('roleTree').getLoader().load(Ext.getCmp('roleTree').getRootNode(), function(){});
    185                     });
    186                 },
    187                 failure: saveRoleFailureFn
    188             })
    189         }
    190     });
    191 }
    192 var treeGrid = new Ext.ux.tree.TreeGrid({
    193      900,
    194     height:400,
    195     id: 'treegrid',
    196     autoScroll: false,
    197     enableColumnMove: false,
    198     enableColumnResize: false,
    199     columns: [{
    200         header: '模块',
    201         dataIndex: 'text',
    202          190,
    203         fixed:true,
    204         resizable: false,
    205         align: 'center'
    206     },{
    207         header: '功能',
    208          700,
    209         resizable: false,
    210         fixed:true,
    211         dataIndex: 'functions',
    212         align: 'center'
    213     }],
    214     root: new Ext.tree.AsyncTreeNode({
    215         expanded :true,    
    216         id: '0'
    217     }),
    218     //获取子节点的URL地址
    219     dataUrl: 'permission_list.action'
    220 });
    221 /**
    222  * 传入roleId显示出checked出已有权限
    223  */

    3

     1 package com.hrmsys.action;
     2 
     3 import java.util.HashMap;
     4 import java.util.List;
     5 import java.util.Map;
     6 
     7 import com.hrmsys.model.Menu;
     8 import com.hrmsys.model.User;
     9 import com.hrmsys.service.MenuService;
    10 import com.hrmsys.service.PermissionService;
    11 
    12 public class PermissionAction extends BaseAction{
    13     private PermissionService permissionService;
    14     private MenuService menuService;
    15     private String node;//树形传入的id
    16     private String menuId;
    17     private String roleId;
    18 
    19     public void list(){
    20         String permissionJson = permissionService.list(node, roleId);
    21         this.setRoleId(null);
    22         this.out(permissionJson);
    23     }
    24     
    25     public void permission(){
    26         Map permission = (Map)this.getSession().get("permission");
    27         String per = (String) permission.get(Integer.parseInt(menuId));
    28         //String per = permissionService.getPermission(roleId, menuId);
    29         this.out(per);
    30     }
    31     
    32     public void initPermission(){
    33         List<Menu> menus = menuService.getAllLeaf();
    34         User user = (User)this.getSession().get("user");
    35         int roleId = user.getRole().getRoleId();
    36         Map map = new HashMap();
    37         for(Menu menu : menus){
    38             int menuId = menu.getMenuId();
    39             String permission = permissionService.getPermission(roleId, String.valueOf(menuId));
    40             map.put(menuId, permission);
    41         }
    42         this.getSession().put("permission", map);
    43         this.out("{success: true}");
    44     }
    45     
    46     public PermissionService getPermissionService() {
    47         return permissionService;
    48     }
    49 
    50     public void setPermissionService(PermissionService permissionService) {
    51         this.permissionService = permissionService;
    52     }
    53 
    54     public String getNode() {
    55         return node;
    56     }
    57 
    58     public void setNode(String node) {
    59         this.node = node;
    60     }
    61     public String getMenuId() {
    62         return menuId;
    63     }
    64     public void setMenuId(String menuId) {
    65         this.menuId = menuId;
    66     }
    67     public String getRoleId() {
    68         return roleId;
    69     }
    70     public void setRoleId(String roleId) {
    71         this.roleId = roleId;
    72     }
    73 
    74     public MenuService getMenuService() {
    75         return menuService;
    76     }
    77 
    78     public void setMenuService(MenuService menuService) {
    79         this.menuService = menuService;
    80     }
    81 }

    4.

      1 package com.hrmsys.service.impl;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 
      6 import net.sf.json.JSONArray;
      7 
      8 import com.hrmsys.bean.MenuBean;
      9 import com.hrmsys.dao.MenuDAO;
     10 import com.hrmsys.dao.PermissionDAO;
     11 import com.hrmsys.dao.RolePermissionDAO;
     12 import com.hrmsys.model.Menu;
     13 import com.hrmsys.model.Permission;
     14 import com.hrmsys.model.RolePermission;
     15 import com.hrmsys.service.PermissionService;
     16 import com.hrmsys.util.ConditionValidate;
     17 
     18 public class PermissionServiceImpl implements PermissionService{
     19     private PermissionDAO permissionDAO;
     20     private MenuDAO menuDAO;
     21     private RolePermissionDAO rolePerDAO;
     22     
     23     @Override
     24     public synchronized String list(String id, String roleId) {
     25         //取出节点为0的总根节点
     26         List<Menu> menus = menuDAO.findNoeById(Integer.parseInt(id));
     27         List<MenuBean> menuBeans = new ArrayList<MenuBean>();
     28         for(Menu menu: menus){
     29             MenuBean menuBean = new MenuBean();
     30             menuBean.setId(menu.getMenuId());
     31             menuBean.setLeaf(menu.getMenuLeaf());
     32             menuBean.setMenuParentId(menu.getMenuParentId());
     33             menuBean.setMenuUrl(menu.getMenuUrl());
     34             menuBean.setText(menu.getMenuTitle());
     35             menuBean.setMenuIcon(menu.getMenuIcon());
     36             menuBean.setExpanded(true);
     37             //若为叶子结点查找其功能点
     38             //1查询  2添加 3修改 4删除 5详情 6保存或修改
     39             if(menu.getMenuLeaf() == 1){
     40                 List<Permission> pers = permissionDAO.findByMnueId(menu.getMenuId());
     41                 StringBuffer funcs = new StringBuffer("<table style='margin-left: 100px;'><tr>");
     42                 int menuId = menu.getMenuId();
     43                 for(Permission per : pers){
     44                     int perId = per.getPerId();
     45                     RolePermission rolePer = null;
     46                     if(ConditionValidate.isEmpty(roleId)){
     47                         rolePer = rolePerDAO.findByPerIdAndRoleId(perId, roleId);
     48                     }
     49                     switch(per.getPerFunction()){
     50                     case 1:
     51                         funcs.append("<td><input type='checkbox' name='"+menuId+"_rs' id='"+perId+"'");
     52                         if(null != rolePer){
     53                             funcs.append(" checked ");
     54                         }
     55                         funcs.append("/>查询&nbsp;&nbsp;</td>");break;
     56                     case 2:
     57                         funcs.append("<td><input type='checkbox' name='"+menuId+"_rs' id='"+perId+"'");
     58                         if(null != rolePer){
     59                             funcs.append(" checked ");
     60                         }
     61                         funcs.append("/>添加&nbsp;&nbsp;</td>");break;
     62                     case 3:
     63                         funcs.append("<td><input type='checkbox' name='"+menuId+"_rs' id='"+perId+"'");
     64                         if(null != rolePer){
     65                             funcs.append(" checked ");
     66                         }
     67                         funcs.append("/>修改&nbsp;&nbsp;</td>");break;
     68                     case 4:
     69                         funcs.append("<td><input type='checkbox' name='"+menuId+"_rs' id='"+perId+"'");
     70                         if(null != rolePer){
     71                             funcs.append(" checked ");
     72                         }
     73                         funcs.append("/>删除&nbsp;&nbsp;</td>");break;
     74                     case 5:
     75                         funcs.append("<td><input type='checkbox' name='"+menuId+"_rs' id='"+perId+"'");
     76                         if(null != rolePer){
     77                             funcs.append(" checked ");
     78                         }
     79                         funcs.append("/>详情&nbsp;&nbsp;</td>");break;
     80                     case 6:
     81                         funcs.append("<td><input type='checkbox' name='"+menuId+"_rs' id='"+perId+"'");
     82                         if(null != rolePer){
     83                             funcs.append(" checked ");
     84                         }
     85                         funcs.append("/>保存或修改&nbsp;&nbsp;</td>");break;
     86                     }
     87                 }
     88                 funcs.append("</tr></table>");
     89                 menuBean.setFunctions(funcs.toString());
     90             }
     91             menuBeans.add(menuBean);
     92         }
     93         return JSONArray.fromObject(menuBeans).toString();
     94     }
     95     
     96     @Override
     97     public String getPermission(int roleId, String menuId) {
     98         List<RolePermission> rolePers = rolePerDAO.findByRoleId(roleId);
     99         StringBuffer sb = new StringBuffer();
    100         for(RolePermission rolePer : rolePers){
    101             int perId = rolePer.getPermission().getPerId();
    102             Permission per = permissionDAO.findByPer(new Integer[]{perId, Integer.parseInt(menuId)});
    103             if(null!= per && per.getPerFunction() > 0){
    104                 switch(per.getPerFunction()){
    105                     case 1: sb.append(" query"); break;
    106                     case 2: sb.append(" add"); break;
    107                     case 3: sb.append(" update"); break;
    108                     case 4: sb.append(" delete"); break;
    109                     case 5: sb.append(" detail"); break;
    110                     case 6: sb.append(" saveorupdate"); break;
    111                 }
    112             }
    113         }
    114         return sb.toString().trim();
    115     }
    116     
    117     public PermissionDAO getPermissionDAO() {
    118         return permissionDAO;
    119     }
    120 
    121     public void setPermissionDAO(PermissionDAO permissionDAO) {
    122         this.permissionDAO = permissionDAO;
    123     }
    124 
    125     public MenuDAO getMenuDAO() {
    126         return menuDAO;
    127     }
    128 
    129     public void setMenuDAO(MenuDAO menuDAO) {
    130         this.menuDAO = menuDAO;
    131     }
    132 
    133     public RolePermissionDAO getRolePerDAO() {
    134         return rolePerDAO;
    135     }
    136 
    137     public void setRolePerDAO(RolePermissionDAO rolePerDAO) {
    138         this.rolePerDAO = rolePerDAO;
    139     }
    140 }
  • 相关阅读:
    node.js 与java 的主要的区别是什么
    基于Node.js+MySQL开发的开源微信小程序B2C商城(页面高仿网易严选)
    Linux下SVN安装配置
    Linux查看CPU和内存使用情况
    Linux添加/删除用户和用户组
    java远程调试
    springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能
    客户端、服务端,跨域访问设置
    HTTP和HTTPS协议
    KMP算法代码
  • 原文地址:https://www.cnblogs.com/sharpest/p/7667764.html
Copyright © 2020-2023  润新知