liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.jeeplus.common.tag;
 
import java.io.IOException;
 
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
 
import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.sys.entity.Menu;
 
/**
 * 
 * 类描述:菜单标签
 * 
 * 刘高峰
 * 
 * @date: 日期:2015-1-23 时间:上午10:17:45
 * @version 1.0
 */
public class AceMenuTag extends TagSupport {
    private static final long serialVersionUID = 1L;
    protected Menu menu;// 菜单Map
 
    public Menu getMenu() {
        return menu;
    }
 
    public void setMenu(Menu menu) {
        this.menu = menu;
    }
 
    public int doStartTag() throws JspTagException {
        return EVAL_PAGE;
    }
 
    public int doEndTag() throws JspTagException {
        try {
            JspWriter out = this.pageContext.getOut();
            String menu = (String) this.pageContext.getSession().getAttribute(
                    "menu");
            if (menu != null) {
                out.print(menu);
            } else {
                menu = end().toString();
                out.print(menu);
 
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }
 
    public StringBuffer end() {
        StringBuffer sb = new StringBuffer();
        sb.append(getChildOfTree(menu, 0));
 
        System.out.println(sb);
        return sb;
 
    }
 
    private static String getChildOfTree(Menu parent, int level) {
        StringBuffer menuString = new StringBuffer();
        String href = "";
        if (!parent.hasPermisson())
            return "";
 
        ServletContext context = SpringContextHolder
                .getBean(ServletContext.class);
        if (parent.getHref() != null && parent.getHref().length() > 0) {
 
            if (parent.getHref().endsWith(".html")
                    && !parent.getHref().endsWith("ckfinder.html")) {// 如果是静态资源并且不是ckfinder.html,直接访问不加adminPath
                href = context.getContextPath() + parent.getHref();
            } else {
                href = context.getContextPath() + Global.getAdminPath()
                        + parent.getHref();
            }
 
        }
 
        if (level > 0) {// level 为0是功能菜单
            menuString.append("<li>");
            if (parent.hasChildren()) {
                menuString.append("<a  href=\"" + href
                        + "\" class=\"dropdown-toggle\">");
            } else {
                menuString.append("<a class=\"J_menuItem\" href=\"" + href
                        + "\">");
            }
            menuString.append("<i class=\"menu-icon fa " + parent.getIcon()
                    + "\"></i>");
            menuString.append("<span class=\"menu-text\">"+parent.getName()+"</span>");
            if (parent.hasChildren()) {
                menuString.append("<b class=\"arrow fa fa-angle-down\"></b>");
            }
            menuString.append("</a>");
            menuString.append("<b class=\"arrow\"></b>");
        }
        if (parent.hasChildren()) {
            if (level == 0) {
                menuString.append("<ul class=\"nav nav-list\">");
            } else {
                menuString.append("<ul class=\"submenu\">");
            }
 
            for (Menu child : parent.getChildren()) {
 
                if (child.getIsShow().equals("1")) {
                    menuString.append(getChildOfTree(child, level + 1));
                }
 
            }
            menuString.append("</ul>");
        }
        if (level > 0) {
            menuString.append("</li>");
        }
        return menuString.toString();
    }
 
}