樹型菜單(JAVA)

java 代碼
  1. <%@ page contentType="text/html; charset=GBK" %>   
  2. <%@ page import="java.util.*"%>   
  3. <%!   
  4.     public void addMenu(List list, Map map, HttpServletRequest request) throws Exception   
  5.     {   
  6.         //判斷map的url屬性是否爲空,若爲空則認爲是最終子菜單   
  7.         if (map.get("url") != null && !"".equals(map.get("url")))   
  8.         {   
  9.             //判斷權限號是否被允許   
  10.             String power = (String) map.get("power");   
  11.             boolean b =  true;//hasPerm(request, power);   
  12.                
  13.             //允許訪問   
  14.             if (b)   
  15.             {   
  16.                 map.put("display""true"); //若權限號被允許則設置display爲true,並添加到list中   
  17.                 list.add(map);   
  18.                 //將父菜單的display屬性設置爲true   
  19.                 String father = (String) map.get("father");   
  20.                 if (father != null)   
  21.                 {   
  22.                     setParentDisplay(list, father);   
  23.                 }   
  24.             }   
  25.         }    
  26.         //非最終子菜單   
  27.         else  
  28.         {   
  29.             if ("".equals(map.get("father"))) //默認頂級菜單的display爲true   
  30.                 map.put("display""true");   
  31.             //若url爲空則直接添加到list中   
  32.             list.add(map);   
  33.         }   
  34.     }   
  35.   
  36.     //用於設置父菜單的display屬性爲true   
  37.     public void setParentDisplay(List list, String father)   
  38.     {   
  39.         Iterator it = list.listIterator();   
  40.         Map map = null;   
  41.   
  42.         while (it.hasNext())   
  43.         { //循環list中的元素   
  44.             map = (HashMap) it.next();   
  45.             if (father.equals(map.get("name")))   
  46.             { //判斷所獲得的元素的name屬性是否與指定的值相等   
  47.                 if (map.get("display") == null)   
  48.                 {   
  49.                     map.put("display""true");   
  50.                     father = (String) map.get("father");   
  51.                     if (!"".equals(father) && father != null)   
  52.                     {   
  53.                         setParentDisplay(list, father);   
  54.                     }   
  55.                 }   
  56.                 break;   
  57.             }   
  58.         }   
  59.     }   
  60.   
  61.     //將無效的元素移除   
  62.     public void removeDisplay(List list)   
  63.     {   
  64.         Iterator it = list.listIterator();   
  65.         Map map = null;   
  66.   
  67.         while (it.hasNext())   
  68.         { //循環list中的元素   
  69.             map = (HashMap) it.next();   
  70.             if (map.get("display") == null)   
  71.             {   
  72.                 it.remove();   
  73.             }   
  74.         }   
  75.     }   
  76.   
  77.     //將列表打印出來   
  78.     public String MenuPrint(List list)   
  79.     {   
  80.         Iterator it = list.listIterator();   
  81.         Map map = null;   
  82.         String father = "";   
  83.         StringBuffer result = new StringBuffer(1000);   
  84.         int i = 0;   
  85.         int j = 0;   
  86.         // this "k" statement is addad   
  87.         int k = 0;   
  88.         while (it.hasNext())   
  89.         {   
  90.             map = (Map) it.next();   
  91.             while (!father.equals(map.get("father")))   
  92.             {   
  93.                 result.append("</table>\n");   
  94.                 if (!"".equals(map.get("father")))   
  95.                 {   
  96.                     result.append("</td>\n</tr>\n");   
  97.                 }   
  98.                 father = findFather(list, father);   
  99.                 i--;   
  100.             }   
  101.             if ("".equals(map.get("father")) && k == 0)   
  102.             {   
  103.                 j++;   
  104.                 k = k + 1;   
  105.                 result.append("<table width=\"198\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");   
  106.                 result.append("<tr>\n");   
  107.                 result.append("<td align=\"left\" colspan=2><a οnclick=\"openclose(img_").   
  108.                        append(map.get("name")).   
  109.                        append(",").   
  110.                        append(map.get("name")).   
  111.                        append(");\"><img src=\"./images/channel/close.gif\" align=\"left\" border=\"0\" name=\"img_").   
  112.                        append(map.get("name")).   
  113.                        append("\">").   
  114.                        append(map.get("text")).   
  115.                        append("</a></td>\n");   
  116.                 result.append("</tr>\n");   
  117.                 i++;   
  118.                 result.append("<tr valign=\"top\">\n<td width=\"14px\"></td><td>\n");   
  119.                 result.append("<table id=\"" + map.get("name") + "\" style=\"display:none\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");   
  120.                 father = (String) map.get("name");   
  121.             }   
  122.             else if (father.equals(map.get("father")))   
  123.             {   
  124.                 result.append("<tr>\n");   
  125.                 if ("".equals(map.get("url")) || map.get("url") == null)   
  126.                 {   
  127.                     j++;   
  128.                     i++;   
  129.                     result.append("<td align=\"left\" colspan=2><a οnclick=\"openclose(img_").   
  130.                            append(map.get("name")).   
  131.                            append(",").   
  132.                            append(map.get("name")).   
  133.                            append(");\"><img src=\"./images/channel/close.gif\" align=\"left\" border=\"0\" name=\"img_").   
  134.                            append(map.get("name")).   
  135.                            append("\">").   
  136.                            append(map.get("text")).   
  137.                            append("</a></td>\n");   
  138.                     result.append("</tr>\n");   
  139.                     result.append("<tr valign=\"top\">\n<td width=\"14px\"></td>");   
  140.                     result.append("<td>\n");   
  141.                     result.append("<table id=\"" + map.get("name") + "\" style=\"display:none\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");   
  142.                     father = (String) map.get("name");   
  143.                 }   
  144.                 else  
  145.                 {   
  146.                     // this "if" statement is addad   
  147.                     if ("".equals(map.get("father")))   
  148.                     {    
  149.                         result.append("<td align=\"left\" colspan=2><a><img src=\"./images/channel/close.gif\" align=\"left\" border=\"0\" name=\"img_").   
  150.                                append(map.get("name")).   
  151.                                append("\"></a><a href=\"").   
  152.                                append(map.get("url")).   
  153.                                append("\" target=\"_self\">").   
  154.                                append(map.get("text")).   
  155.                                append("</a></td>\n");   
  156.                         result.append("</tr>\n");   
  157.                     }   
  158.                     else  
  159.                     {   
  160.                         result.append("<td align=\"left\" colspan=2><a><img src=\"./images/channel/circ.gif\" align=\"left\" border=\"0\" name=\"img_").   
  161.                                append(map.get("name")).   
  162.                                append("\"></a><a href=\"javascript:openWin('").   
  163.                                append(map.get("url")).   
  164.                                append("')\">").   
  165.                                append(map.get("text")).   
  166.                                append("</a></td>\n");   
  167.                         result.append("</tr>\n");   
  168.                     }   
  169.                 }   
  170.             }   
  171.         }   
  172.         while (!"".equals(father))   
  173.         {   
  174.             result.append("</table>\n</td>\n</tr>\n");   
  175.             father = findFather(list, father);   
  176.         }   
  177.         result.append("</table>\n");   
  178.   
  179.         return result.toString();   
  180.     }   
  181.   
  182.     //用於查找上一級父菜單   
  183.     public String findFather(List list, String father)   
  184.     {   
  185.         Iterator it = list.listIterator();   
  186.         Map map = null;   
  187.         while (it.hasNext())   
  188.         {   
  189.             map = (Map) it.next();   
  190.             if (father.equals(map.get("name")))   
  191.             {   
  192.                 father = (String) map.get("father");   
  193.                 return father;   
  194.             }   
  195.         }   
  196.         return null;   
  197.     }   
  198.        
  199.     public void addMenu(String name,String text,String url,String power,String father,Map map,List list,HttpServletRequest request) throws Exception   
  200.     {   
  201.         //Map map = null;   
  202.         //List list = new ArrayList();   
  203.   
  204.         map = new HashMap();   
  205.         map.put("name", name);   
  206.         map.put("text", text);   
  207.         map.put("url", url);   
  208.         map.put("power", power);   
  209.         map.put("father", father);   
  210.         try  
  211.         {   
  212.             addMenu(list, map, request);   
  213.         }   
  214.         catch(Exception e)   
  215.         {   
  216.         }   
  217.     }   
  218.   
  219. %>   
  220. <%   
  221. String path = request.getContextPath();   
  222. //數據區   
  223. Map map = null;   
  224. List list = new ArrayList();   
  225.   
  226. //level0   
  227. map = new HashMap();   
  228. map.put("name""level0");   
  229. map.put("text""根菜單");   
  230. map.put("url""");   
  231. map.put("power""");   
  232. map.put("father""");   
  233. addMenu(list, map, request);   
  234. //level1   
  235. map = new HashMap();   
  236. map.put("name""level10");   
  237. map.put("text""搜索引擎");   
  238. map.put("url""");   
  239. map.put("power""");   
  240. map.put("father""level0");   
  241. addMenu(list, map, request);   
  242. //level2   
  243. map = new HashMap();   
  244. map.put("name""level20");   
  245. map.put("text""百度");   
  246. map.put("url""http://www.baidu.com");   
  247. map.put("power""");   
  248. map.put("father""level10");   
  249. addMenu(list, map, request);   
  250.   
  251. map = new HashMap();   
  252. map.put("name""level21");   
  253. map.put("text""GOOGLE");   
  254. map.put("url""http://www.google.com");   
  255. map.put("power""");   
  256. map.put("father""level10");   
  257. addMenu(list, map, request);   
  258. //level1   
  259. map = new HashMap();   
  260. map.put("name""level11");   
  261. map.put("text""消息網站");   
  262. map.put("url""");   
  263. map.put("power""");   
  264. map.put("father""level0");   
  265. addMenu(list, map, request);   
  266. //level2   
  267. map = new HashMap();   
  268. map.put("name""level22");   
  269. map.put("text""人民網");   
  270. map.put("url""http://www.people.com.cn");   
  271. map.put("power""");   
  272. map.put("father""level11");   
  273. addMenu(list, map, request);   
  274.   
  275. removeDisplay(list);   
  276.   
  277. String userName = "King";   
  278. %>   
  279. <html>   
  280. <head>   
  281. <title>導航菜單</title>   
  282. <STYLE type="text/css">   
  283. BODY {   
  284.     FONT-SIZE: 9pt; FONT-FAMILY: sans-serif;background-color:#A9C3F0;   
  285. }   
  286. A   
  287. {   
  288.     COLOR: 000000; TEXT-DECORATION: none   
  289. }   
  290. A:hover   
  291. {   
  292.     COLOR: cc3300; TEXT-DECORATION: none   
  293. }   
  294. A:link {   
  295.     FONT-FAMILY: sans-serif; TEXT-DECORATION: none   
  296. }   
  297. A:active {   
  298.     FONT-FAMILY: sans-serif; TEXT-DECORATION: none   
  299. }   
  300. A:visited {   
  301.     FONT-FAMILY: sans-serif; TEXT-DECORATION: none   
  302. }   
  303. B {   
  304.     FONT-SIZE: 14px; COLOR: #000000  
  305. }   
  306. TABLE   
  307. {   
  308.      font:9pt;color: #cc3300   
  309. }   
  310. TD {   
  311.     FONT-SIZE: 12px;color:#363636  
  312. }   
  313. </STYLE>   
  314. <script language="JavaScript">   
  315. function openclose(param1, param)   
  316. {   
  317.     if (param.style.display == "none")   
  318.     {   
  319.         param.style.display = "";   
  320.         param1.src = "./images/channel/open.gif";   
  321.     }   
  322.     else  
  323.     {   
  324.         param.style.display = "none";   
  325.         param1.src = "./images/channel/close.gif";   
  326.     }   
  327. }   
  328.   
  329. function openWin(url)   
  330. {   
  331.     parent.LoginTime.location.reload();   
  332.     //window.open(url,"forum");   
  333.     parent.forum.location = url;   
  334. }   
  335. window.onload = function(){   
  336.     //openclose(img_level0, level0);   
  337.     openAllMenu();   
  338. }   
  339. function openAllMenu()   
  340. {   
  341.     var tb = document.getElementsByTagName("TABLE");   
  342.     for (var i = 0; i < tb.length; i++)   
  343.     {   
  344.         if (tb[i].id == null || tb[i].id == ""continue;   
  345.         openclose(eval("img_" + tb[i].id), tb[i]);   
  346.     }   
  347. }   
  348. CollectGarbage();   
  349. </script>   
  350. </head>   
  351. <body topmargin=10 leftmargin=0>   
  352.   <table width="100%" height="100%" border="0" align="center" cellpadding="1" cellspacing="0" class="blue">   
  353.     <tr>   
  354.         <td cellspacing="2" height="23" background="./images/bgwelcome.png"> &nbsp;<strong>歡迎您-<%=(String) userName%></strong></td>   
  355.     </tr>   
  356.     <tr>   
  357.         <td  valign="top" height="100%" background="./images/bg.png"><%=MenuPrint(list)%></td>   
  358.     </tr>   
  359.  </table>   
  360. </body>   
  361. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章