Extjs 动态菜单

//左边放一个手风琴菜单
	var cascadeMenu = new Ext.Panel({
		region: 'west',
	    resizable:true,
	    title: '功能区',
	    id:'westMenu',
	    layout:'accordion',
	    collapsible:true,
	    collapseMode:'mini',
	    collapseFirst:true,
	    split:true,
	    width:250,
	    minSize: 175,
      	maxSize: 500,
	    layoutConfig: {	     
	        titleCollapse: true,
	        animate: false,
	        activeOnTop: false
	    }
	}); //手风琴菜单结束
	
	// 构建左边的菜单
	Ext.Ajax.request({
				url : 'MenuServlet?action=main',
				success : function(response, options) {
					var arr = Ext.decode(response.responseText);
					for (var i = 0; i < arr.length; i++) {
						var panel = new Ext.tree.TreePanel({
									id : arr[i].id,
									title : arr[i].text,
									iconCls : arr[i].iconCls,
									autoScroll : true,
									border : false,
									loader : new MenuLoader({
												dataUrl : 'MenuServlet?action=sub&id='+ arr[i].id
											}),
									root : new Ext.tree.AsyncTreeNode({
												expanded : true
											}),
									listeners:{
										'click':function(node){
								  			Ext.get('center_iframe').dom.src = node.attributes.hrefsrc;
								  			if(node.attributes.text!=null){
	
							  				}
										}
									},
									rootVisible : false
								});
						cascadeMenu.add(panel);
						panel.on('expand', function(p) {
									var expires = new Date();
									expires.setDate(expires.getDate() + 30);
								});
					}
					cascadeMenu.doLayout();
				}
			});


后台servlet
if(StringUtils.equals(action, "main")){
					Document doc = XMLUtils.load(filePath);
					StringBuffer sb = new StringBuffer("[");
					if (doc != null) {
						Element root = doc.getRootElement();
						List els = root.elements();
						for (int i = 0; i < els.size(); i++) {
							Element el = (Element) els.get(i);
							Attribute id = el.attribute("id");
							Attribute text = el.attribute("text");
							Attribute iconCls = el.attribute("iconCls");
							sb.append("{id:'").append(id == null ? "" : id.getValue()).append("',");
							sb.append("text:'").append(text == null ? "" : text.getValue()).append("',");
							sb.append("iconCls:'").append(iconCls == null ? "" : iconCls.getValue()).append("'},");
						}
						if(els.size()>0){
							sb.deleteCharAt(sb.length()-1);
						}
					}
					sb.append("]");
					out.println(sb.toString());
				}else if(StringUtils.equals(action, "sub")){
					response.setContentType("text/xml");
					Document doc=XMLUtils.load(filePath);
					String id=request.getParameter("id");
					StringBuffer sb=new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
					List services = bean.findAllService();
					if(doc!=null){
						sb.append("<Menus>");
						
							Element el=doc.getRootElement();
							List nodes=el.selectNodes("/Menus/Items[@id='"+id+"']/*");
							for(int i=0;i<nodes.size();i++){
								Node node=(Node)nodes.get(i);
								sb.append(node.asXML());
							}
						
						sb.append("</Menus>");
					}
					out.println(sb.toString());
					
				}

你可能感兴趣的:(bean,Ajax,xml,servlet,ext)