Problem-solving idea 1: Overloading the Connection class
due to extjs and the server side is all data interaction in the json format. The server side will not control page jumps. Page jumps and prompt functions are all completed by extjs and extjs. All interaction methods on the server side are
inherited from Ext.data.Connection. This class can intercept all methods of interaction with the server side. Permission control on the server side is done by acegi. If it is not passed by acegi. Verification, if there is no authorization, it will return to 403.jsp
. If you need to log in again, it will return to login.jsp. Therefore, reload the Connection class and rewrite the handleResponse method. , determine whether the returned result is 403.jsp, login.jsp, and if so, perform corresponding control. If the returned data is normal, continue to execute downwards
I personally use 403.jsp and login.jsp. One line is added with comment code
, which relies on this keyword to work The code is as follows:
Problem-solving idea 2 : The server side returns menu json data
My menu is made with tree. When initializing the main page, initialize the menu first.//此处重载了Cunnection方法,用来拦截client与Server的交 互, //后台acegi拦截用户请求后,如果无权限,返回403.jsp;如果没登录,返回login.jsp; //通过Acegi拦截 后,才返回用户想要的Json结果 Ext.override(Ext.data.Connection, { handleResponse : Ext.data.Connection.prototype.handleResponse.createInterceptor( function(response) { var resText=response.responseText; if (resText.length>10) { resText=resText.substr(0,9); } if (resText=='<!--login'){ window.top.location.href = topURL+"/login.jsp"; } else if (resText=='<!--deny-'){ if (resText=='<!--deny-'){ Ext.Msg.show({ title : '错误提示', msg : '禁止访问此功能,请和系统管理员联系', buttons : Ext.Msg.OK, icon : Ext.Msg.INFO }); }; } else if (resText=='<!--404--'){ Ext.Msg.show({ title : '错误提示', msg : '页面未找到', buttons : Ext.Msg.OK, icon : Ext.Msg.INFO }); } }) });
The code is as follows:
This'getJsonMenus. do' returns the menu json data. The configuration in strut2 is:
loader : new Ext.tree.TreeLoader({ dataUrl : 'getJsonMenus.do' }),
menus is a
<action name="getJsonMenus" class="jsonSystemAction" method="getJsonMenus"> <result type="json"> <param name="root">menus</param> </result> </action>
JsonMenu’s attribute is:
private String text;
private boolean expanded; private String id; private boolean leaf;
private List
The format returned by getJsonMenus.do can meet the format requirements of tree.
The js code is as follows
In this way, the menu is obtained. Some netizens have proposed asynchronous menu solutions, and I have also included them. It is listed below
Ext.onReady(function() { setTimeout(function() { Ext.get('loading').remove(); Ext.getDom('header').style.visibility = 'visible'; var vp = new Ext.Viewport({ layout : 'border', defaults : { collapsible : true, split : true }, items : [{ xtype : 'box', region : 'north', applyTo : 'header', height : 30, split : false }, { title : currentUser, id : 'accordion-panel', layout : 'border', region : 'west', margins : '2 0 5 5', width : 200, minSize : 200, maxSize : 250, bodyStyle : 'background-color:#DFE8F6', defaults : { border : false }, bbar : [{ text : '开始', iconCls : 'icon-plugin', menu : new Ext.menu.Menu({ items : [{ text : '关于系统', iconCls : 'icon-info', handler : function() { new Ext.Window({ closeAction : 'close', resizable : false, bodyStyle : 'padding: 7', modal : true, title : '关于本系统', html : '本系统采用目前较为流行的技术实现,
前台使用了ExtJs技术,所以实现了跨浏览器
' + ' 本程序在IE6,IE7,FireFox3均测试通过!
主要技术: Struts2 + Spring + iBatis + ExtJs
' + '数 据 库: Oracle 9i', width : 300, height : 200 }).show(); } }, { text : '退出系统', iconCls : 'icon-delete', handler : function() { Ext.Msg.confirm('操作提示', '您确定要退出本系统?', function(btn) { if ('yes' == btn) { Ext.Ajax.request({ url : 'logout.do', success : function() { location = '/'; }, failure : function() { Ext.Msg.show({ title : '错误提示', msg : '退出系统失败!', icon : Ext.Msg.ERROR, buttons : Ext.Msg.OK }); } }); } }); } }] }) }], items : [{ layout : 'accordion', region : 'center', items : [{ title : '导航菜单', iconCls : 'icon-nav', border : false, items : [{ xtype : 'treepanel', border : false, rootVisible : false, autoScroll : true, loader : new Ext.tree.TreeLoader({ dataUrl : 'getJsonMenus.do' }), root : new Ext.tree.AsyncTreeNode(), listeners : { 'click' : function(n) { try { var sn = this.selModel.selNode || {}; if (n.leaf && n.id != sn.id) { Ext.getCmp('content-panel').layout.setActiveItem(n.id.substring(0, n.id .indexOf('-')) + '-panel'); } } catch (e) { } } } }] },{ title : '系统设置', iconCls : 'icon-nav' }] }] }, { id : 'content-panel', region : 'center', layout : 'card', margins : '2 5 5 0', activeItem : 0, border : false, items : [start, p_company, p_user, p_dept, p_system, p_subject, p_category, p_resource] }] }); }, 250); });
The method of synchronous loading is as follows:
The usage of TAG in js is as follows:
//FUTURE_TAG全局的TAG控制类, 控制的组件的hidden属性,key=TAG的名字,value=true(组件隐藏),false(组件显示) var FUTURE_TAG={tbar1: false, tbar2: true}; var conn = Ext.lib.Ajax.getConnectionObject().conn; conn.open("GET", 'getJsonTags.do',false); conn.send(null); future_tag= eval('(' + conn.responseText + ')');
getJsonTags.do returns a
var btn_add_system = new Ext.Button({ text : '添加', iconCls : 'icon-add', hidden: FUTURE_TAG.system_add, handler : function() { window_add_system.show(); } });
Object , key is the TAG name, value is boolean Java is written as follows:
Strut2 configuration is as follows:
tagMap=new HashMap<String,Boolean>(); for (int i=0;i<allTagList.size();i++){ tagMap.put(allTagList.get(i).getResString(), true); }
This way You can control whether the foreground components are displayed in the background, thereby achieving our purpose.
<action name="getJsonTags" class="jsonSystemAction" method="getJsonTags"> <result type="json"> <param name="root">tagMap</param> </result> </action>
{tbar1: false, tbar2: true}
Then in extjs:
var vResult = eval('(' ajaxText ')'); //Get {tbar1: false, tbar2: true}
In this way, you can directly assign a value to tbar
disabled: vResult.tbar1
disabled: vResult.tbar2
Solution 5:
Setting module permissions is used to set the permissions that users can operate. Allows users to set the operability and inoperability of modules.
A subform for setting permissions pops up
A user must be selected before setting permissions.
Js code
Create a tree, which is placed in the center of the pop-up form.
var row = grid_user.getSelectionModel().getSelected(); if(!row){ alert('对不起,您还未选择数据!'); return; } var row = grid_user.getSelectionModel().getSelected(); if(!row){ alert('对不起,您还未选择数据!'); return; }
Create a pop-up subform.
var root=new Ext.tree.TreeNode({ id:"root", text:"所有操作", checked:false, iconCls:'task-folder' }); var tree=new Ext.tree.TreePanel({ frame:false, region:'center', root:root, animate:true, enableDD:false, border:false, rootVisible:true, autoScroll:true }); var root=new Ext.tree.TreeNode({ id:"root", text:"所有操作", checked:false, iconCls:'task-folder' }); var tree=new Ext.tree.TreePanel({ frame:false, region:'center', root:root, animate:true, enableDD:false, border:false, rootVisible:true, autoScroll:true });
Give prompts during data loading.
var win = new Ext.Window({ title:'设置模块权限', closable:true, width:300, height:500, plain:true, layout:'border', modal:true, items:[tree] }); win.show(this); var win = new Ext.Window({ title:'设置模块权限', closable:true, width:300, height:500, plain:true, layout:'border', modal:true, items:[tree] }); win.show(this);
Call the method with the root node, the selected user row, and the parent node flag as parameters.
Ext.MessageBox.show({ title:'请稍候', msg:'正在加载数据,请耐心等待...', progress:true }); Ext.MessageBox.show({ title:'请稍候', msg:'正在加载数据,请耐心等待...', progress:true });
obtains data from the background and displays the
getNodes(row,root,'root'); getNodes(row,root,'root');
Definition of JSON data.
function getNodes(row,root,parent){ //... } function getNodes(row,root,parent){ //...}
var record_pri = new Ext.data.Record.create([ {name:'modelId'}, {name:'modelName'}, {name:'sort'}, {name:'canUse'}, {name:'privilegeId'} ]); var store_pri = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({url:'../'}), reader: new Ext.data.JsonReader({root:'rows'},record_pri) }); var record_pri = new Ext.data.Record.create([ {name:'modelId'}, {name:'modelName'}, {name:'sort'}, {name:'canUse'}, {name:'privilegeId'} ]); var store_pri = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({url:'../'}), reader: new Ext.data.JsonReader({root:'rows'},record_pri) });
无刷新请求,获取数据并展现出来;并添加事件监听。当点击树某一节点时,判断是否已经从后台取得数据,如果还没有取则从后台获取数据,再根据返回的数据判断是叶子节点还是非叶子节点。然后以不同的方式展现与处理。
叶子节点和非叶子节点展现时,使用的图标不同。叶子节点没有添加单击事件,而非叶子节点添加了单击事件。
Js代码
Ext.Ajax.request({ url:'http://www.cnblogs.com/../privilegeAction.do?method=list', params:{ userId:row.get('userId'), parentId:parent }, success:function(response, request){ Ext.MessageBox.hide(); var res = Ext.util.JSON.decode(response.responseText); store_pri.loadData(res); for(var i=0;i<store_pri.getCount();i++){ var rec = store_pri.getAt(i); var canuse = (rec.get('canUse')=='是'?true:false); var modid = rec.get('privilegeId') + '-id-' + rec.get('modelId'); var node; if(rec.get('sort')=='菜单'){ node = new Ext.tree.TreeNode({ text:rec.get('modelName'), id:modid, checked:canuse, iconCls:'task-folder' }); node.on('click',function(node){ if(node.firstChild==null){ getNodes(row,node,get_mod_id(node.id)); } }); } else { node = new Ext.tree.TreeNode({ text:rec.get('modelName'), id:modid, checked:canuse, iconCls:'task' }); } node.on('checkchange',function(node,check){ Ext.Ajax.request({ url:'http://www.cnblogs.com/../privilegeAction.do?method=save2', params:{ privilegeId:get_rec_id(node.id), canuse:(check?'是':'否') }, success:function(response, request){ }, failure:function(){ Ext.MessageBox.hide(); alert('sorry!'); } }); }); root.appendChild(node); } root.expand(); }, failure:function(){ Ext.MessageBox.hide(); alert('sorry!'); } }); Ext.Ajax.request({ url:'http://www.cnblogs.com/../privilegeAction.do?method=list', params:{ userId:row.get('userId'), parentId:parent }, success:function(response, request){ Ext.MessageBox.hide(); var res = Ext.util.JSON.decode(response.responseText); store_pri.loadData(res); for(var i=0;i<store_pri.getCount();i++){ var rec = store_pri.getAt(i); var canuse = (rec.get('canUse')=='是'?true:false); var modid = rec.get('privilegeId') + '-id-' + rec.get('modelId'); var node; if(rec.get('sort')=='菜单'){ node = new Ext.tree.TreeNode({ text:rec.get('modelName'), id:modid, checked:canuse, iconCls:'task-folder' }); node.on('click',function(node){ if(node.firstChild==null){ getNodes(row,node,get_mod_id(node.id)); } }); } else { node = new Ext.tree.TreeNode({ text:rec.get('modelName'), id:modid, checked:canuse, iconCls:'task' }); } node.on('checkchange',function(node,check){ Ext.Ajax.request({ url:'http://www.jb51.net/../privilegeAction.do?method=save2', params:{ privilegeId:get_rec_id(node.id), canuse:(check?'是':'否') }, success:function(response, request){ }, failure:function(){ Ext.MessageBox.hide(); alert('sorry!'); } }); }); root.appendChild(node); } root.expand(); }, failure:function(){ Ext.MessageBox.hide(); alert('sorry!'); } });
当非叶子节点被点击时,递归地调用方法来获取孩子节点。
获取行的ID和模块的ID。树的节点将行的ID和模块的ID一起取出来了。不然的话,如果只取得模块ID,而不取行ID,那么在修改的时候,则不能进行正确的修改。
Js代码
function get_rec_id(str){ var arr = str.split('-id-'); return arr[0]; } function get_mod_id(str){ var arr = str.split('-id-'); return arr[1]; }