jsp从数据库读取数据动态生成树形菜单
- 提问者网友:树红树绿
- 2021-02-25 05:56
- 五星知识达人网友:北方的南先生
- 2021-02-25 06:54
- 1楼网友:妄饮晩冬酒
- 2021-02-25 08:20
jsp动态树形菜单须用到递归算法,比如在数据库有张表,parent表,parent的字段有id,name,depth,leve,id自增,depth设置为级数,如这条数据最大,为0,如为字菜单就为1,而leve就指定它父节点的id,给段代码自己可以摸索下
public vector getmoduletree() { vector pclass = new vector(); try { stmt =con.createstatement(); string sql = "select * from module where parentid = 0"; rs = stmt.executequery(sql); module cvo = null; while(rs.next()) { cvo = new module(); cvo.setmodule_id(rs.getint("module_id")); cvo.setmodule_name(rs.getstring("module_name")); cvo.setmodule_url(rs.getstring("module_url")); cvo.setparentid(rs.getint("parentid"));
cvo.setrootid(rs.getint("rootid"));
cvo.setdepth(rs.getint("depth"));
pclass.add(cvo); } for (int i = 0; i < pclass.size(); i++) { module pcvo = (module) pclass.get(i); showtreemenu(pcvo); } con.commit();
} catch (sqlexception e) { e.printstacktrace(); } finally { try { if(rs!=null) { rs.close(); } if(stmt!=null) { stmt.close(); } if(con!=null) { con.close(); } } catch (sqlexception e) { e.printstacktrace(); } } return classlist; } public void showtreemenu(module c) { module ccvo = null; string sql = "select * from module where parentid = " + c.getmodule_id(); vector cclass = new vector(); try { module cvotemp; stmt =con.createstatement(); rs = stmt.executequery(sql); while(rs.next()) { cvotemp = new module(); cvotemp.setmodule_id(rs.getint("module_id")); cvotemp.setmodule_name(rs.getstring("module_name")); cvotemp.setmodule_url(rs.getstring("module_url")); cvotemp.setparentid(rs.getint("parentid"));
cvotemp.setrootid(rs.getint("rootid"));
cvotemp.setdepth(rs.getint("depth"));
cclass.add(cvotemp); } system.out.println(cclass.size()+"(((((((((((((((((((((((((9"); if (cclass.size() > 0) { c.sethaschild("have"); classlist.add(c); for (int j = 0; j < cclass.size(); j++) { ccvo = (module) cclass.get(j); showtreemenu(ccvo); }
} else { classlist.add(c); } } catch (sqlexception e) { e.printstacktrace(); } }