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

JSP动态树形菜单,菜单项从数据库中获得,第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 = 0i <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 = 0j <cclass.size()j++)

{

ccvo = (Module) cclass.get(j)

ShowTreeMenu(ccvo)

}} else

{

classList.add(c)

}

} catch (SQLException e)

{

e.printStackTrace()

}

}

使用下面这个jsp的前 你需要写一个Stu类 生成set和get方法

Public Class Stu{ private int idprivate String namepublic void setId(int id){ this id=id} public int getId(){ return this id} public void setName(String name){ this name=name} public String getName(){ return this name} }

写个简单的servlet

public class gotoMall extends HttpServlet { public void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { ArrayList<Stu>stus=new ArrayList<Stu>()Stu stu =new Stu()stu setId( )stu setName("aa")stu setId( )stu setName("bb")stus add(stu )stus add(stu )request setAttribute("stus" stus)request getRequestDispatcher("/WEB INF/admin/my jsp") forward(request response)} public void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { this doGet(request response)} }

my jsp

lishixinzhi/Article/program/Java/JSP/201311/19829

将每个表当做一个对象,表的列当做属性,写set和get方法。两表中共同的那么可以在一个对象中添加另一个对象的引用。如role表中有node_id那么可以将这个属性设置为SYS_FUNC这个对象。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11652864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存