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<String,HtmlMenu> map){
//		if(bufLevelOne.length()==0){
//			bufLevelOne.append("<div><ul>");
//		}
		if("0".equals(sysMoudle.getHaschild())){//如果没有子节点
			bufLevelOne.append("<li><a href='").append(sysMoudle.getUrl()).append("'><span>").append(sysMoudle.getName()).append("</span></a></li>");
		}else{//有子节点
			bufLevelOne.append("<li><a href='#' class='parent'><span>").append(sysMoudle.getName()).append("</span></a>").append(map.get(sysMoudle.getId()).getBufLevel2()).append("</li>");
		}
	}
//	public StringBuilder getBufLevelOne(){
//		return bufLevelOne.append("</ul></div>");
//	}
	
	public StringBuilder getBufLevel1() {
		return bufLevelOne;
	}
	
	//  节点二按parentsid生成,生成后存储到map中 map<parentId,menuLevelTwo>
	//	<div>
	//    <ul>	
	//  <li><a href='#' class='parent'><span>Sub Item 2</span></a>
	//     <div>
	//       <ul>
	//         <li><a href='#'><span>Sub Item 2.1</span></a></li>
	//         <li><a href='#'><span>Sub Item 2.2</span></a></li>
	//       </ul>
	//     </div>
	//   </li>
	//   <li><a href='#'><span>Sub Item 3</span></a></li>
	//    </ul>
	//    </div>
	public StringBuilder getBufLevelTwo() {
		return bufLevelTwo.append("</ul></div>");
	}
	
	public StringBuilder getBufLevel2() {
		return bufLevelTwo;
	}
	/**
	 * @param sysMoudle
	 * @param map  map中存放的是该节点的所有的子节点
	 */
	public void setBufLevelTwo(SysMoudle sysMoudle,Map<String,HtmlMenu> map) {
		//").append(href).append("
		if(bufLevelTwo.length()==0){
			bufLevelTwo.append("<div><ul>");
		}
		if("0".equals(sysMoudle.getHaschild())){//如果没有子节点
			bufLevelTwo.append("<li><a href='").append(sysMoudle.getUrl()).append("'><span>").append(sysMoudle.getName()).append("</span></a></li>");
		}else{//有子节点
			bufLevelTwo.append("<li><a href='#' class='parent'><span>").append(sysMoudle.getName()).append("</span></a>").append(map.get(sysMoudle.getId()).getBufLevel3()).append("</li>");
		}
	}
	
	

	//节点三按parentsid生成,生成后存储到map中 map<parentId,menuLevelThird>
	//   <div>
	//     <ul>
	//       <li><a href='#'><span>Sub Item 1.7.1</span></a></li>
	//       <li><a href='#'><span>Sub Item 1.7.2</span></a></li>
	//     </ul>
	//   </div>
		
	private StringBuilder getBufLevel3() {
		return bufLevelThird;
	}
	
	public void setBufLevelThird(SysMoudle sysMoudle) {
		if(bufLevelThird.length()>0){
			bufLevelThird.append("<li><a href='").append(sysMoudle.getUrl()).append("'><span>").append(sysMoudle.getName()).append("</span></a></li>");
		}else{
			bufLevelThird.append("<div><ul>");
			bufLevelThird.append("<li><a href='").append(sysMoudle.getUrl()).append("'><span>").append(sysMoudle.getName()).append("</span></a></li>");
		}
	}
	
	public StringBuilder getBufLevelThird() {
		return bufLevelThird.append("</ul></div>");
	}
}

	List<SysMoudle> list=sysService.getAllSysMoudle();
		Iterator<SysMoudle> iterator=list.iterator();
		Map<String,SysMoudle> level1Map=new HashMap<String,SysMoudle>();
		Map<String,List<SysMoudle>> level2Map=new HashMap<String, List<SysMoudle>>();
		Map<String,List<SysMoudle>> level3Map=new HashMap<String, List<SysMoudle>>();
		
		Map<String,HtmlMenu> menu1Map=new HashMap<String,HtmlMenu>();
		Map<String,HtmlMenu> menu2Map=new HashMap<String,HtmlMenu>();
		Map<String,HtmlMenu> menu3Map=new HashMap<String,HtmlMenu>();
		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<SysMoudle> listLeve2=(List<SysMoudle>) level2Map.get(parentId);
				if(listLeve2==null){
					listLeve2=new ArrayList<SysMoudle>();
					listLeve2.add(sysMoudle);
					level2Map.put(parentId, listLeve2);
				}else{
					listLeve2.add(sysMoudle);
				}
			}
			
			if(level==3){//所有的三级菜单
				List<SysMoudle> listLeve3=(List<SysMoudle>) level3Map.get(parentId);
				if(listLeve3==null){
					listLeve3=new ArrayList<SysMoudle>();
					listLeve3.add(sysMoudle);
					level3Map.put(parentId, listLeve3);
				}else{
					listLeve3.add(sysMoudle);
				}
			}
		}
		
		//处理所有的三级菜单生成html
		Set<String> level3Set=level3Map.keySet();
		Iterator<String> iterLevel3=level3Set.iterator();
		while(iterLevel3.hasNext()){
			String parentId=iterLevel3.next();
			List<SysMoudle> listLevel3=level3Map.get(parentId);//相同父节点的三级菜单
			HtmlMenu htmlMenu=null;
			for(int i=0;i<listLevel3.size();i++){
				SysMoudle sysMoudle=listLevel3.get(i);
				htmlMenu=menu3Map.get(listLevel3.get(i).getParentid());
				if(htmlMenu==null){
					htmlMenu=new HtmlMenu();
					htmlMenu.setBufLevelThird(sysMoudle);//创建第三层菜单
					menu3Map.put(sysMoudle.getParentid(), htmlMenu);
				}else{
					htmlMenu.setBufLevelThird(sysMoudle);
				}
			}//注意生成的三级菜单没有  </ul></div>闭合
		}
		
		Set<String> menu3MapSet=level3Map.keySet();
		Iterator<String> menu3MapLevel3=menu3MapSet.iterator();
		while(menu3MapLevel3.hasNext()){
			HtmlMenu menu=menu3Map.get(menu3MapLevel3.next());
			System.out.println(menu.getBufLevelThird());
		}
		
		//处理所有的二级级菜单生成html
		Set<String> level2Set=level2Map.keySet();
		Iterator<String> iterLevel2=level2Set.iterator();
		while(iterLevel2.hasNext()){
			String parentId=iterLevel2.next();
			List<SysMoudle> listLevel2=level2Map.get(parentId);//相同父节点的二级菜单
			HtmlMenu htmlMenu=null;
			for(int i=0;i<listLevel2.size();i++){
				SysMoudle sysMoudle=listLevel2.get(i);
				htmlMenu=menu2Map.get(listLevel2.get(i).getParentid());
				if(htmlMenu==null){
					htmlMenu=new HtmlMenu();
					htmlMenu.setBufLevelTwo(sysMoudle,menu3Map);//创建第二层菜单
					menu2Map.put(sysMoudle.getParentid(), htmlMenu);
				}else{
					htmlMenu.setBufLevelTwo(sysMoudle,menu3Map);//创建第二层菜单
				}
			}//注意生成的二级菜单没有  </ul></div>闭合
		}
 	
	 	Set<String> menu2MapSet=level2Map.keySet();
	 	Iterator<String> menu2MapLevel2=menu2MapSet.iterator();
	 	while(menu2MapLevel2.hasNext()){
	 		HtmlMenu menu=menu2Map.get(menu2MapLevel2.next());
	 		System.out.println(menu.getBufLevelTwo());
	 	}
	 	
	 	
	 	//处理所有的一级级菜单生成html
		Set<String> level1Set=level1Map.keySet();
		Iterator<String> 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);//创建第一层菜单
				}
		}//注意生成的一级菜单没有  </ul></div>闭合
 	
	 	Set<String> menu1MapSet=level1Map.keySet();
	 	Iterator<String> menu1MapLevel1=menu1MapSet.iterator();
	 	while(menu1MapLevel1.hasNext()){
	 		HtmlMenu menu=menu1Map.get(menu1MapLevel1.next());
	 		System.out.println(menu.getBufLevel1());
	 	}

	 	StringBuilder bufMenu=new StringBuilder();//最终菜单
	 	bufMenu.append("<div id='menu'>");
	 	bufMenu.append("<ul class='menu'>");
	 	Iterator<String> menuSet=menu1Map.keySet().iterator();
	 	while(menuSet.hasNext()){
	 		bufMenu.append(menu1Map.get(menuSet.next()).getBufLevel1());
	 	}
	 	bufMenu.append("<div><ul>");
	 	

package com.ctl.sys.po;

import java.util.Date;

public class SysMoudle {
    private String id;

    private Integer levels;

    private String name;

    private Date createdate;

    private Date modifydate;

    private String adduser;

    private String isshow;

    private String url;

    private Integer indexes;

    private String haschild;

    private String parentid;
    
    private String sameColUuid;
    

	public String getSameColUuid() {
		return sameColUuid;
	}

	public void setSameColUuid(String sameColUuid) {
		this.sameColUuid = sameColUuid;
	}

	public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public Integer getLevels() {
        return levels;
    }

    public void setLevels(Integer levels) {
        this.levels = levels;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public Date getCreatedate() {
        return createdate;
    }

    public void setCreatedate(Date createdate) {
        this.createdate = createdate;
    }

    public Date getModifydate() {
        return modifydate;
    }

    public void setModifydate(Date modifydate) {
        this.modifydate = modifydate;
    }

    public String getAdduser() {
        return adduser;
    }

    public void setAdduser(String adduser) {
        this.adduser = adduser == null ? null : adduser.trim();
    }

    public String getIsshow() {
        return isshow;
    }

    public void setIsshow(String isshow) {
        this.isshow = isshow == null ? null : isshow.trim();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url == null ? null : url.trim();
    }

    public Integer getIndexes() {
        return indexes;
    }

    public void setIndexes(Integer indexes) {
        this.indexes = indexes;
    }

    public String getHaschild() {
        return haschild;
    }

    public void setHaschild(String haschild) {
        this.haschild = haschild == null ? null : haschild.trim();
    }

    public String getParentid() {
        return parentid;
    }

    public void setParentid(String parentid) {
        this.parentid = parentid == null ? null : parentid.trim();
    }

	@Override
	public String toString() {
		return "SysMoudle [id=" + id + ", levels=" + levels + ", name=" + name
				+ ", createdate=" + createdate + ", modifydate=" + modifydate
				+ ", adduser=" + adduser + ", isshow=" + isshow + ", url="
				+ url + ", indexes=" + indexes + ", haschild=" + haschild
				+ ", parentid=" + parentid + ", sameColUuid=" + sameColUuid
				+ "]";
	}

    
}

你可能感兴趣的:(java,三级菜单,java后台动态创建三级菜单)