大位大虾:
怎样生成实现树状菜单?有三种方式分别如何实现?
1.jsp/servlet;
2.Applet+jsp;
3.jsp动态生成树状结构描述的xml文件,然后编写自定义标签库.通过自定义标签库来控制和显示树状结构.
------解决方案--------------------
package com.xintong.action.test;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2006
* Company: 河南信通
* @author 邵炜
* @version 1.0
*/
import com.xintong.action.sqlcon;
public class ActivatedTree {
public void init(javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request) throws Exception {
out.println( " <title> 测试树 </title> ");
buildTree(out,0,0);//初始调用
}
public void buildTree(javax.servlet.jsp.JspWriter out,int parentid,int level) throws Exception {
sqlcon sqlbean=new sqlcon();
level++;
sqlbean.executeQuery( "select * from test_menu where par_menu_id= "+parentid+ " order by menu_id ");
while(sqlbean.next()) {
out.println( " <div> ");
for(int i=0;i <level-1;i++)
out.print( " <img src=\ "../pics/t.gif\ "> ");
if(has_child(sqlbean.getInt( "menu_id "))) {
String id=sqlbean.getData( "menu_id ");
String name=sqlbean.getData( "menu_name ");
out.print( " <img alt=\ "展开\ " style=\ "cursor:pointer;\ " onclick=\ "myClick( ' "+id+ " ');\ " id=\ "img "+id+ "\ " src=\ "../pics/tplus.gif\ "> <img id=\ "im "+id+ "\ " src=\ "../pics/icon_folder.gif\ "> ");
out.print( " <span style=\ "cursor:pointer;\ " onclick=\ "myClick1( ' "+sqlbean.getData( "menu_id ")+ " ');\ " id=\ "span "+id+ "\ "> "+name+ " </span> ");
out.println( " <div style=\ "display:none;\ " id=\ "div "+id+ "\ "> ");
buildTree(out,sqlbean.getInt( "menu_id "),level);//递归调用
out.println( " </div> ");
} else
out.print( " <img src=\ "../pics/tminus.gif\ "> <img src=\ "../pics/icon_folderopen.gif\ "> <span style=\ "cursor:pointer;\ " onclick=\ "myClick1( ' "+sqlbean.getData( "menu_id ")+ " ');\ " id=\ "span "+sqlbean.getData( "menu_id ")+ "\ "> "+sqlbean.getData( "menu_name ")+ " </span> ");
out.println( " </div> ");
}
sqlbean.close();
}
private boolean has_child(int parentid) throws Exception {
sqlcon sqlbean=new sqlcon();
sqlbean.executeQuery( "select * from test_menu where par_menu_id= "+parentid+ " order by menu_id ");
return sqlbean.next();
}
}
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.*,java.io.*,java.util.Hashtable " errorPage= " " %>
<%@ page import = "java.util.Calendar,java.sql.* "%>
<%@ include file= "../check.jsp " %>
<script language= "JavaScript ">
<!--这段js为了实现树的展开和关闭的效果-->
<!--
function myClick(id) {
eval( "var div=div "+id);
eval( "var img=img "+id);
//eval( "var im=im "+id);
div.style.display=div.style.display!= "none "? "none ": "block ";
img.src=div.style.display!= "none "? "../pics/tminus.gif ": "../pics/tplus.gif ";