java后台动态创建三级菜单

 

create table sys_moudle(
id varchar(32),--每四个为一个层级 0001|00010001|000100010001   0002 
levels int,  --表示是第几层
name varchar(32),--菜单的名字
createdate date,--创建日期
modifydate date,--更改日期
adduser varchar(32),--创建用户
isshow char(1) check isshow in('0','1'),--该页面是否显示
url varchar(300),--页面路径相对于WEB-INFO
indexes int);

select * from  sys_moudle order by id;
alter table sys_moudle add haschild char(1) check haschild in('0','1');--是否是叶子节点 0是叶子
alter table sys_moudle add parentid varchar(32); --父节点id
alter table sys_moudle add samecoluuid varchar(32);--同一列标识 

-- drop table sys_moudle;
-- delete from sys_moudle where id is null;
INSERT INTO `sys_moudle` VALUES ('0001', 1, 'Home', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', NULL, 100, '1', NULL, '0001');
INSERT INTO `sys_moudle` VALUES ('00010001', 2, 'HomeSub1', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', NULL, 100101, '1', '0001', '0001');
INSERT INTO `sys_moudle` VALUES ('000100010001', 3, 'HomeSub1Sub1', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', 'jsp/homesub1sub1.jsp', 100101101, '0', '00010001', '0001');
INSERT INTO `sys_moudle` VALUES ('00010002', 2, 'HomeSub2', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', NULL, 100101, '1', '0001', '0001');
INSERT INTO `sys_moudle` VALUES ('000100020001', 3, 'HomeSub2Sub1', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', 'jsp/homesub2sub1.jsp', 100101, '0', '00010002', '0001');
INSERT INTO `sys_moudle` VALUES ('000100020002', 3, 'HomeSub2Sub2', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', 'jsp/homesub2sub2.jsp', 100101, '0', '00010002', '0001');
INSERT INTO `sys_moudle` VALUES ('0002', 1, 'Item2', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', NULL, 200, '1', NULL, '0002');
INSERT INTO `sys_moudle` VALUES ('00020001', 2, 'Item2Sub1', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', '', 200, '1', '0002', '0002');
INSERT INTO `sys_moudle` VALUES ('000200010001', 3, 'Item2Sub1Sub1', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', 'jsp/item2sub1sub1.jsp', 200, '1', '00020001', '0002');
INSERT INTO `sys_moudle` VALUES ('0003', 1, 'Item3', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', NULL, 300, '0', NULL, '0003');

package com.ctl.sys.po;

import java.util.Map;

/**@author 		guolin
 *  @date    		2016-4-17上午10:00:18
 *  @package_name com.ctl.sys.po
 *  @project_name   Aa权限
 *  @version 		version.1.0
 */
public class HtmlMenu {
	private StringBuilder bufLevelOne=new StringBuilder();
	private StringBuilder bufLevelTwo=new StringBuilder();
	private StringBuilder bufLevelThird=new StringBuilder();
	

	/**
	 * @param sysMoudle
	 * @param map map中存放的是该节点的所有的子节点
	 */
	public void setBufLevelOne(SysMoudle sysMoudle,Map map){
//		if(bufLevelOne.length()==0){
//			bufLevelOne.append("
"); // } public StringBuilder getBufLevel1() { return bufLevelOne; } // 节点二按parentsid生成,生成后存储到map中 map //
// //
public StringBuilder getBufLevelTwo() { return bufLevelTwo.append("
"); } public StringBuilder getBufLevel2() { return bufLevelTwo; } /** * @param sysMoudle * @param map map中存放的是该节点的所有的子节点 */ public void setBufLevelTwo(SysMoudle sysMoudle,Map map) { //").append(href).append(" if(bufLevelTwo.length()==0){ bufLevelTwo.append("
    "); } if("0".equals(sysMoudle.getHaschild())){//如果没有子节点 bufLevelTwo.append("
  • ").append(sysMoudle.getName()).append("
  • "); }else{//有子节点 bufLevelTwo.append("
  • ").append(sysMoudle.getName()).append("").append(map.get(sysMoudle.getId()).getBufLevel3()).append("
  • "); } } //节点三按parentsid生成,生成后存储到map中 map // private StringBuilder getBufLevel3() { return bufLevelThird; } public void setBufLevelThird(SysMoudle sysMoudle) { if(bufLevelThird.length()>0){ bufLevelThird.append("
  • ").append(sysMoudle.getName()).append("
  • "); }else{ bufLevelThird.append("
    "); } }

    	List list=sysService.getAllSysMoudle();
    		Iterator iterator=list.iterator();
    		Map level1Map=new HashMap();
    		Map> level2Map=new HashMap>();
    		Map> level3Map=new HashMap>();
    		
    		Map menu1Map=new HashMap();
    		Map menu2Map=new HashMap();
    		Map menu3Map=new HashMap();
    		while(iterator.hasNext()){
    			SysMoudle sysMoudle=iterator.next();
         		String id=sysMoudle.getId();
    			String parentId=sysMoudle.getParentid();
    			int level=sysMoudle.getLevels();
    			if(level==1){//一级菜单
    					level1Map.put(id, sysMoudle);
    			}
    			
    			if(level==2){//所有的二级菜单
    				List listLeve2=(List) level2Map.get(parentId);
    				if(listLeve2==null){
    					listLeve2=new ArrayList();
    					listLeve2.add(sysMoudle);
    					level2Map.put(parentId, listLeve2);
    				}else{
    					listLeve2.add(sysMoudle);
    				}
    			}
    			
    			if(level==3){//所有的三级菜单
    				List listLeve3=(List) level3Map.get(parentId);
    				if(listLeve3==null){
    					listLeve3=new ArrayList();
    					listLeve3.add(sysMoudle);
    					level3Map.put(parentId, listLeve3);
    				}else{
    					listLeve3.add(sysMoudle);
    				}
    			}
    		}
    		
    		//处理所有的三级菜单生成html
    		Set level3Set=level3Map.keySet();
    		Iterator iterLevel3=level3Set.iterator();
    		while(iterLevel3.hasNext()){
    			String parentId=iterLevel3.next();
    			List listLevel3=level3Map.get(parentId);//相同父节点的三级菜单
    			HtmlMenu htmlMenu=null;
    			for(int i=0;i
闭合 } Set menu3MapSet=level3Map.keySet(); Iterator menu3MapLevel3=menu3MapSet.iterator(); while(menu3MapLevel3.hasNext()){ HtmlMenu menu=menu3Map.get(menu3MapLevel3.next()); System.out.println(menu.getBufLevelThird()); } //处理所有的二级级菜单生成html Set level2Set=level2Map.keySet(); Iterator iterLevel2=level2Set.iterator(); while(iterLevel2.hasNext()){ String parentId=iterLevel2.next(); List listLevel2=level2Map.get(parentId);//相同父节点的二级菜单 HtmlMenu htmlMenu=null; for(int i=0;i
闭合 } Set menu2MapSet=level2Map.keySet(); Iterator menu2MapLevel2=menu2MapSet.iterator(); while(menu2MapLevel2.hasNext()){ HtmlMenu menu=menu2Map.get(menu2MapLevel2.next()); System.out.println(menu.getBufLevelTwo()); } //处理所有的一级级菜单生成html Set level1Set=level1Map.keySet(); Iterator iterLevel1=level1Set.iterator(); while(iterLevel1.hasNext()){ String id=iterLevel1.next(); SysMoudle sysMoudle=level1Map.get(id);//相同父节点的二级菜单 HtmlMenu htmlMenu=menu1Map.get(id); if(htmlMenu==null){ htmlMenu=new HtmlMenu(); htmlMenu.setBufLevelOne(sysMoudle,menu2Map);//创建第一层菜单 menu1Map.put(id, htmlMenu); }else{ htmlMenu.setBufLevelOne(sysMoudle,menu2Map);//创建第一层菜单 } }//注意生成的一级菜单没有
闭合 Set menu1MapSet=level1Map.keySet(); Iterator menu1MapLevel1=menu1MapSet.iterator(); while(menu1MapLevel1.hasNext()){ HtmlMenu menu=menu1Map.get(menu1MapLevel1.next()); System.out.println(menu.getBufLevel1()); } StringBuilder bufMenu=new StringBuilder();//最终菜单 bufMenu.append("

你可能感兴趣的:(Java)