java如何实现二级树形菜单动态显示。要求加载页面时显示一级,点击一级菜单再去数据库查找出二级菜单

java如何实现二级树形菜单动态显示。要求加载页面时显示一级,点击一级菜单再去数据库查找出二级菜单,第1张

用json格式,异步请求,当你点击一级菜单的时候,js触发,通过ajax异步把相关参数传到后台,查询出来的数据,然后封装成json格式数据返回,js拿到数据后,通过js把数据设置到页面相应的地方!

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

}

}

使用JTree 给你个例子import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.util.*public class TreeDemo1{

public TreeDemo1(){

JFrame f=new JFrame("TreeDemo1")

Container contentPane=f.getContentPane()

String[] s1={"公司文件","个人信件","私人文件"}

String[] s2={"本机磁盘(C:)","本机磁盘(D:)","本机磁盘(E:)"}

String[] s3={"奇摩站","职棒消息","网络书店"}

Hashtable hashtable1=new Hashtable()

Hashtable hashtable2=new Hashtable()

hashtable1.put("我的公文包",s1)

hashtable1.put("我的电脑",s2)

hashtable1.put("收藏夹",hashtable2)

hashtable2.put("网站列表",s3)

Font font = new Font("Dialog", Font.PLAIN, 12)

Enumeration keys = UIManager.getLookAndFeelDefaults().keys()

/**定义widnows界面**/

while (keys.hasMoreElements()) {

Object key = keys.nextElement()

if (UIManager.get(key) instanceof Font) {

UIManager.put(key, font)

}

}

try{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")

}catch(Exception el){

System.exit(0)

}

/**定义widnows界面**/

JTree tree=new JTree(hashtable1)

JScrollPane scrollPane=new JScrollPane()

scrollPane.setViewportView(tree)

contentPane.add(scrollPane)

f.pack()

f.setVisible(true)

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

}

public static void main(String[] args){

new TreeDemo1()

}

}


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

原文地址:https://54852.com/sjk/9652998.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存