當前位置:菜譜大全網 - 菜譜 - 樹形菜單

樹形菜單

Jsp動態樹菜單必須使用遞歸算法。比如數據庫中有壹個表,壹個父表,parent的字段有id,name,depth,leve,ID自增,深度設置為series。如果這個數據最大,則為0,如果是word菜單,則為1,Leve指定其父節點的ID,這樣就可以自己探索段代碼的公共向量getModuleTree()。

{

Vector pclass = new Vector();

嘗試

{

stmt = con . create statement();

string SQL = " select * from parentid = 0的模塊";

RS = stmt . execute query(SQL);

模塊cvo = null

while(rs.next())

{

cvo =新模塊();

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 . set depth(RS . getint(" depth "));pclass . add(cvo);

}

for(int I = 0;我& ltpclass . size();i++)

{

Module pcvo =(Module)pclass . get(I);

show tree menu(pcvo);

}

con . commit();} catch (SQLException e)

{

e . printstacktrace();

}最後

{

嘗試

{

如果(rs!=空)

{

RS . close();

}

如果(stmt!=空)

{

stmt . close();

}

如果(con!=空)

{

con . close();

}

}

catch (SQLException e)

{

e . printstacktrace();

}

}

返回classList

}

公共void ShowTreeMenu(模塊c)

{

模塊ccvo = null

string SQL = " select * from Module where parentid = "+c . get Module _ id();

Vector cclass = new Vector();

嘗試

{

模塊cvotemp

stmt = con . create statement();

RS = stmt . execute query(SQL);

while(rs.next())

{

cvo temp = new Module();

cvo temp . setmodule _ id(RS . getint(" Module _ id "));

cvo temp . set Module _ name(RS . getstring(" Module _ name "));

cvo temp . setmodule _ URL(RS . getstring(" Module _ URL "));

cvo temp . setparentid(RS . getint(" parentid "));cvo temp . setrootid(RS . getint(" rootid "));cvo temp . set depth(RS . getint(" depth "));cclass . add(cvo temp);

}

system . out . println(cclass . size()+"(((((((((((((((((9)));

if(cclass . size()& gt;0)

{

c . sethaschild(" have ");

class list . add(c);

for(int j = 0;j & ltcclass . size();j++)

{

ccvo =(Module)cclass . get(j);

show tree menu(ccvo);

} }否則

{

class list . add(c);

}

} catch (SQLException e)

{

e . printstacktrace();

}

}