我想做一个4层的树型结构,每层数据都是从数据库里动态生成的,我的这个结点数,及子结点是如何获得的 你可以在数据库相应的表里设置level参数,定义树的层次,你就可以通过select * from tree_table where level=x (x为节点当前的层次数,可以在页面里面设置他为隐藏变量)得到节点的个数,如果where子句改为level=x+1就得到x层的字节点的个数 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简单说几点,希望给你启发。1 每个节点应当知道自己的父亲是谁;2 通过精心设计的视图,方便查询一系列相关节点;3 在Action中完成整个树的生成,以及相应的javascript代码,然后在JSP中简单的打印出树, 及Javascript函数; 你在jsp论坛和javascript里都可以找的到代码的,很多树的例子。而且都是无限的分层的。 package beans;import java.sql.*;import javax.naming.*;import java.util.*;public class CreateTree{ public CreateTree() { }//这个函数是我得到数据库连接用的,你可以用自己的方法得到连结 public static Connection getCon() { Connection conn = null; Statement stmt=null; javax.sql.DataSource ds=null; try { Context initCtx=new InitialContext(); Context ctx=(Context)initCtx.lookup("java:comp/env"); Object obj=(Object)ctx.lookup("jdbc/sqlServer"); ds=(javax.sql.DataSource)obj; //System.out.println("开始连接"); conn=ds.getConnection(); } catch(javax.naming.NamingException nex) { System.out.println("DataBaseCon.DataBaseCon():"+nex.getMessage()); } catch(SQLException e) { System.out.println("DataBaseCon.DataBaseCon():"+e.getMessage()); } return conn; }//这一部分是我用来转码的 public static String getStr(String str) { try { if (str == null || str.equals("")) { return ""; } String temp_p=str; byte[] temp_t=temp_p.getBytes("ISO8859-1"); String temp=new String(temp_t); return temp; } catch(Exception e) { e.printStackTrace(); } return "null"; } //type定义显示树的类型:0维护,1增加,2删除,3修改,4审核 public static void tree(int type,int[] popedomList,String nextPage,String catalogId,javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request) throws Exception { Connection conn=null; try { conn=getCon(); outTree(type,popedomList,nextPage,catalogId,out,request,conn); } catch(Exception e) {} finally { if(conn!=null) { conn.close(); } } } public static void outTree(int type,int[] popedomList,String nextPage,String catalogId,javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request,Connection conn) throws Exception { Statement stmt=conn.createStatement(); String catalogListSql="SELECT * FROM TB_PRODUCT_CATALOG WHERE PARENT_ID='"+catalogId+"'"; System.out.println(catalogListSql); Arrays.sort(popedomList); ResultSet catalogListRs=stmt.executeQuery(catalogListSql); while(catalogListRs.next()) { int id=catalogListRs.getInt("ID"); int combineOrStock=catalogListRs.getInt("COMBINE_OR_STOCK"); String name=getStr(catalogListRs.getString("NAME")); int flag=1; //维护栏目显示全部,不需要在类中判断权限,否则要判断用户是否拥有第一级别的增加删除权限 if(type!=0&&catalogId.equals("0")) { //只判断第一级别的 int isPermitNumer=-1; int nowPopedom; if(type==1) { nowPopedom=Integer.parseInt(Integer.toString(id)+1); } else if(type==2) { nowPopedom=Integer.parseInt(Integer.toString(id)+2); } else if(type==3) { nowPopedom=Integer.parseInt(Integer.toString(id)+3); } else { nowPopedom=Integer.parseInt(Integer.toString(id)+4); } isPermitNumer=Arrays.binarySearch(popedomList,nowPopedom); if(isPermitNumer<0) { flag=-1; } } if(flag==1) { out.println("<table cellspacing=0 cellpadding=0><tr>"); out.println("<td colspan=2 class=f9a>"); out.println("<a href='#aa"+id+"' onclick='diva_show("+id+")' id=link"+id+">"); out.println("<image name=f"+id+" src=images/plus1.gif border=0 width=16 height=18>"); out.println("<image name=i"+id+" src=images/icon_book_close.gif border=0></a>"); out.println("<a href="+nextPage+"?catalogId="+id+"&catalogName="+name+">"+name+"</a>"); if(catalogId.equals("0")) { if(combineOrStock==1) { out.println("[集团]"); } if(combineOrStock==2) { out.println("[股份]"); } } //out.println("<br>"); out.println("</td></table>"); out.println("<div id=a"+id+" style=display:none>"); //String childSql="SELECT COUNT(*) AS COUNT FROM TB_PRODUCT_CATALOG WHERE PARENT_ID='"+id+"'"; //int nextCount=0; //ResultSet childRs=stmt.executeQuery(childSql); //if(childRs.next()) //{ // nextCount=childRs.getInt("COUNT"); //} //childRs.close(); //if(nextCount>0) //{ out.println("<table width=100% border=0 cellspacing=0 cellpadding=0>"); out.println("<tr>"); out.println("<td width=18> </td>"); out.println("<td>"); outTree(type,popedomList,nextPage,Integer.toString(id),out,request,conn); out.println("</td>"); out.println("</tr>"); out.println("</table>"); //} out.println("</div>"); } } catalogListRs.close(); stmt.close(); }} 【求推荐】最好用的 web应用 java开发平台? 支持可视化的?【不考虑用微软的产品】 java中报错 求一个字符串处理的解决方法,在线求助 根据select框的内容刷新表格 webwork的一个特别菜的问题(初学) 怎样获得meta中的参数啊? 关于测试问题,请大家帮忙 servlet问题 ,form中的action如何写? Microsoft Internet Explorer 5.0版本以后有XML数据岛的概念 JSP中怎么好好的利用呢? 怎样使表格上边框为0 求一java实现的树形结构 请问版主左边的论坛树是怎样实现的???
1 每个节点应当知道自己的父亲是谁;
2 通过精心设计的视图,方便查询一系列相关节点;
3 在Action中完成整个树的生成,以及相应的javascript代码,然后在JSP中简单的打印出树, 及Javascript函数;
import java.sql.*;
import javax.naming.*;
import java.util.*;
public class CreateTree
{
public CreateTree()
{
}
//这个函数是我得到数据库连接用的,你可以用自己的方法得到连结
public static Connection getCon()
{
Connection conn = null;
Statement stmt=null;
javax.sql.DataSource ds=null;
try
{
Context initCtx=new InitialContext();
Context ctx=(Context)initCtx.lookup("java:comp/env");
Object obj=(Object)ctx.lookup("jdbc/sqlServer");
ds=(javax.sql.DataSource)obj;
//System.out.println("开始连接");
conn=ds.getConnection();
}
catch(javax.naming.NamingException nex)
{
System.out.println("DataBaseCon.DataBaseCon():"+nex.getMessage());
}
catch(SQLException e)
{
System.out.println("DataBaseCon.DataBaseCon():"+e.getMessage());
}
return conn;
}
//这一部分是我用来转码的
public static String getStr(String str)
{
try
{
if (str == null || str.equals(""))
{
return "";
}
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace();
}
return "null";
} //type定义显示树的类型:0维护,1增加,2删除,3修改,4审核
public static void tree(int type,int[] popedomList,String nextPage,String catalogId,javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request) throws Exception
{
Connection conn=null;
try
{
conn=getCon();
outTree(type,popedomList,nextPage,catalogId,out,request,conn);
}
catch(Exception e)
{}
finally
{
if(conn!=null)
{
conn.close();
}
}
}
public static void outTree(int type,int[] popedomList,String nextPage,String catalogId,javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request,Connection conn) throws Exception
{
Statement stmt=conn.createStatement();
String catalogListSql="SELECT * FROM TB_PRODUCT_CATALOG WHERE PARENT_ID='"+catalogId+"'";
System.out.println(catalogListSql);
Arrays.sort(popedomList);
ResultSet catalogListRs=stmt.executeQuery(catalogListSql);
while(catalogListRs.next())
{
int id=catalogListRs.getInt("ID");
int combineOrStock=catalogListRs.getInt("COMBINE_OR_STOCK");
String name=getStr(catalogListRs.getString("NAME"));
int flag=1;
//维护栏目显示全部,不需要在类中判断权限,否则要判断用户是否拥有第一级别的增加删除权限
if(type!=0&&catalogId.equals("0"))
{
//只判断第一级别的
int isPermitNumer=-1;
int nowPopedom;
if(type==1)
{
nowPopedom=Integer.parseInt(Integer.toString(id)+1);
}
else if(type==2)
{
nowPopedom=Integer.parseInt(Integer.toString(id)+2);
}
else if(type==3)
{
nowPopedom=Integer.parseInt(Integer.toString(id)+3);
}
else
{
nowPopedom=Integer.parseInt(Integer.toString(id)+4);
}
isPermitNumer=Arrays.binarySearch(popedomList,nowPopedom);
if(isPermitNumer<0)
{
flag=-1;
}
}
if(flag==1)
{
out.println("<table cellspacing=0 cellpadding=0><tr>");
out.println("<td colspan=2 class=f9a>");
out.println("<a href='#aa"+id+"' onclick='diva_show("+id+")' id=link"+id+">");
out.println("<image name=f"+id+" src=images/plus1.gif border=0 width=16 height=18>");
out.println("<image name=i"+id+" src=images/icon_book_close.gif border=0></a>");
out.println("<a href="+nextPage+"?catalogId="+id+"&catalogName="+name+">"+name+"</a>");
if(catalogId.equals("0"))
{
if(combineOrStock==1)
{
out.println("[集团]");
}
if(combineOrStock==2)
{
out.println("[股份]");
}
}
//out.println("<br>");
out.println("</td></table>");
out.println("<div id=a"+id+" style=display:none>");
//String childSql="SELECT COUNT(*) AS COUNT FROM TB_PRODUCT_CATALOG WHERE PARENT_ID='"+id+"'";
//int nextCount=0;
//ResultSet childRs=stmt.executeQuery(childSql);
//if(childRs.next())
//{
// nextCount=childRs.getInt("COUNT");
//}
//childRs.close();
//if(nextCount>0)
//{
out.println("<table width=100% border=0 cellspacing=0 cellpadding=0>");
out.println("<tr>");
out.println("<td width=18> </td>");
out.println("<td>");
outTree(type,popedomList,nextPage,Integer.toString(id),out,request,conn);
out.println("</td>");
out.println("</tr>");
out.println("</table>");
//}
out.println("</div>");
}
}
catalogListRs.close();
stmt.close();
}
}