JSP动态树形菜单,菜单项从数据库中获得

有代码给个,非常谢谢
2024-12-27 18:25:05
推荐回答(2个)
回答1:

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();
}
}

回答2:

1、得到树信息
public Treeinfo[] getAllTreeinfo() throws Exception{
Connection conn = getConn();
PreparedStatement smt = conn.prepareStatement("select * from treeTable where parent_treeId = -1");
CachedRowSet cs = new CachedRowSetImpl();
cs.populate(smt.executeQuery());
List treeinfoList = new ArrayList();
while(cs.next()){
Treeinfo treeinfo = new Treeinfo();
treeinfo.setTreeId(cs.getInt(1));
treeinfo.setNodeName(cs.getString(2));
treeinfo.setParentId(cs.getInt(3));
treeinfo.setChildren(getChildren(treeinfo.getTreeId()));
treeinfoList.add(treeinfo);
}
cs.close();
smt.close();
conn.close();
Treeinfo[] treeinfo = new Treeinfo[treeinfoList.size()];
treeinfo = treeinfoList.toArray(treeinfo);
return treeinfo;
}
2、得到当前id的子节点信息,这里用了递归调用
public Treeinfo[] getChildren(int treeNodeId) throws Exception{
Connection conn = getConn();
PreparedStatement smt = conn.prepareStatement("select * from treeTable where parent_treeId = ?");
smt.setInt(1, treeNodeId);
ResultSet rs = smt.executeQuery();
List childList = new ArrayList();
while(rs.next()){
Treeinfo treeinfo = new Treeinfo();
treeinfo.setTreeId(rs.getInt(1));
treeinfo.setNodeName(rs.getString(2));
treeinfo.setParentId(rs.getInt(3));
treeinfo.setChildren(getChildren(treeinfo.getTreeId()));
childList.add(treeinfo);
}
rs.close();
smt.close();
conn.close();
Treeinfo[] childResult = new Treeinfo[childList.size()];
childResult = childList.toArray(childResult);
return childResult;
}
}
3、最后新建显示树形菜单的jsp页面
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>



My JSP 'testtree.jsp' starting page




theme="ajax"
rootNode="root"
childCollectionProperty="children"
nodeIdProperty="treeId"
nodeTitleProperty="nodeName"
treeSelectedTopic="treeSelected">