| |
/* * 创建日期 2005-4-14 * * todo 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - java - 代码样式 - 代码模板 */package treemenu;import java.util.*;import java.sql.*;import dbmanager.dbconnection;import javax.servlet.jsp.jspwriter;/** * @author 呆猴 lucky * * todo 要更改此生成的类型注释的模板,请转至 * 窗口 - 首选项 - java - 代码样式 - 代码模板 */public class tree { private dbconnection conn; public arraylist arrayid;//定义包含所有id的数组 public arraylist arrayname;//定义包含所有名称的数组 public arraylist arrayparent;//定义包含所有父id的数组 public arraylist class1_id;//定义包含所有一级信息id的数组 public tree(){ try{ conn=new dbconnection(); } catch(exception e){ system.out.println("sorry"); } } /** * * 定义读取所有相关记录和一级信息的方法 */ public void buidtreeinit() throws sqlexception{ resultset rs=conn.runrs("select * from sp_sys_menu_item"); string aa=""; string id=""; string name=""; string parent_id=""; int i=0; arrayid=new arraylist(); arrayname=new arraylist(); arrayparent=new arraylist(); class1_id=new arraylist(); while(rs.next()){ id=rs.getstring("id"); name=rs.getstring("name"); parent_id=rs.getstring("parent_id"); arrayid.add(id);//把所有id信息赋值到arrayid数组中 arrayname.add(name);//把所有name信息赋值到arrayname数组中 arrayparent.add(parent_id);//把所有parent_id信息赋值到arrayparent数组中 /** * 把所有的一级信息赋值到数组class1_id中 */ if(parent_id.equals("0")) { class1_id.add(id); } } conn.free(); } /** * 开始定义树型结构的构造 * @param parentid * @throws sqlexception */ public void buildtree(jspwriter out,string parentid,int j) throws exception{ j++; arraylist tmplist=new arraylist();//包含所有父id为parent_id的记录的名称数组 string mmm=" "; string nnn="|"; for(int q=0;q<j;q++){ nnn=nnn+"--"; mmm=mmm+" "; } string table2=""; table2=table2+"<tr bgcolor=/"#ffffff/">"; table2=table2+"<td width=/"70%/" height=/"30/">"+mmm+nnn+" <name></td>"; table2=table2+"<td width=/"30%/" height=/"30/" align=/"center/">"; table2=table2+"修改 "; table2=table2+"注册下级菜单 "; table2=table2+" <del>"; table2=table2+"</td>"; table2=table2+"</tr>"; while(arrayparent.indexof(parentid)!=-1) { string tmpname=(string)arrayname.get(arrayparent.indexof(parentid));//获取所有父id为parent_id的记录的名称 string tmpid=(string)arrayid.get(arrayparent.indexof(parentid));//获取该子信息的id,用于赋予下级子信息的父id if(has_child(tmpid)){ out.print(table2.replaceall("<name>",tmpname).replaceall("<del>","")); } else{ out.print(table2.replaceall("<name>",tmpname).replaceall("<del>","删除")); } int tmp=arrayparent.indexof(parentid);//获取参数parent_id所在位置 arrayparent.remove(tmp);//删除参数parent_id所在位置的parent_id arrayid.remove(tmp);//删除参数parent_id所在位置的id arrayname.remove(tmp);//删除参数parent_id所在位置name if(has_child(tmpid))//如果该条信息有相关子信息重新执行buildtree方法 { buildtree(out,tmpid,j); } } } /** * 进行是否有子信息判断 * @param parentid * @return */ public boolean has_child(string parentid) { boolean bb=false; if(arrayparent.indexof(parentid)!=-1) {
bb=true; } return bb; } /** * 树型结构显示 * @param args * @throws exception */ public void showtree(jspwriter out) throws exception{ tree aa=new tree(); aa.buidtreeinit(); string table1=""; table1=table1+"<tr bgcolor=/"#cccccc/">"; table1=table1+"<td width=/"70%/" height=/"30/"> <name></td>"; table1=table1+"<td width=/"30%/" height=/"30/" align='center'>"; table1=table1+"修改 "; table1=table1+"注册下级菜单 "; table1=table1+" <del>"; table1=table1+"</td>"; table1=table1+"</tr>"; for(int i=0;i<aa.class1_id.size();i++) { if(aa.has_child((string)aa.class1_id.get(i))){ out.print(table1.replaceall("<name>",(string)aa.arrayname.get(i)).replaceall("<del>",""));&nb
|
|