struts2简单实现树形菜单(从数据库动态获取)

这里只介绍实现的流程及步骤,方便以后查阅,并没有考虑代码的优化及其它的方面,有兴趣的可以自己去修改!

另外,这里只是实现显示树,并没有实现节点的添加及删除,有需要的可以自己去完成!

首先,建立数据库表,treeTabel

create table treeTables
(
treeId int identity(1,1) primary key, //id标识列,自动增长
treeName varchar(20), //节点名称
parent_treeId int //父节点id
)

建立数据库的操作类,得到树的详细信息

package com.tree.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

public class TreeService {

private TreeService(){
   try {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}

private static TreeService treeService;

public static TreeService getInstance(){
   if(treeService == null)
    treeService = new TreeService();
   return treeService;
}

public Connection getConn() throws Exception{
   return DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=testDB","sa","");
}

/**
* 得到树信息
* @return
* @throws Exception
*/
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<Treeinfo> treeinfoList = new ArrayList<Treeinfo>();
   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;
}

/**
* 得到当前id的子节点信息,这里用了递归调用
* @param treeNodeId
* @return
* @throws Exception
*/
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<Treeinfo> childList = new ArrayList<Treeinfo>();
   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;
}
}
建立中间层Action类

package com.tree.test;

import com.opensymphony.xwork2.ActionSupport;

public class TreeAction extends ActionSupport {

private Treeinfo root;


@Override
public String execute() throws Exception {
   root = new Treeinfo();
   root.setTreeId(-1);
   root.setNodeName("root");
   root.setChildren(TreeService.getInstance().getAllTreeinfo());
   return SUCCESS;
}

public Treeinfo getRoot() {
   return root;
}

public void setRoot(Treeinfo root) {
   this.root = root;
}
}

最后新建显示树形菜单的jsp页面

<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>My JSP 'testtree.jsp' starting page</title>
    <s:head theme="ajax" debug="true" />
    <script language="JavaScript">
   function treeNodeSelected(arg) {
    alert("id["+arg.source.widgetId+"], name["+ arg.source.title+ "] selected");
   }
   dojo.addOnLoad(function() {               
      var s = dojo.widget.byId('treeTestId').selector;               
      dojo.event.connect(s, 'select', 'treeNodeSelected');
   });
</script>
</head>
<body>
    <s:tree id="treeTestId"
     theme="ajax"
     rootNode="root"
     childCollectionProperty="children"
     nodeIdProperty="treeId"
     nodeTitleProperty="nodeName"
     treeSelectedTopic="treeSelected">
</s:tree>
</body>
</html>
特别注意,要在页面引入    <s:head theme="ajax" debug="true" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值