如何在 Jsp 中循环自引用 class?
How to loop self referenced class in Jsp?
我写了一个节点 class 用于存储 tree
数据结构,
节点看起来像,
我想开发的树,
public class Node1
{
public String data;
public Node1 left;
public Node1 right;
public static int level=0;
public Node1(String data)
{
this.data=data;
this.left=null;
this.right=null;
}
}
我将节点存储为,
public void add(Node1 ht,Node1 tm,String Data)
{
String LNsearchData=Data.substring(0,Data.indexOf("#"));
Data=Data.substring(Data.indexOf("#")+1);
//System.out.println("righ"+LNsearchData);
boolean rights=false,lefts=false;
while(true)
{
if(ht.data.startsWith(LNsearchData))
{
System.out.println("righ"+tm.data);
if(ht.right==null)
{ rights=true;
break;
}
ht=ht.right;
if(Data.length()==0)
break;
LNsearchData=Data.substring(0,Data.indexOf("#"));
Data=Data.substring(Data.indexOf("#")+1);
}
else
{
if(ht.left==null)
{
break;
}
ht=ht.left;
if(Data.length()==0)
break;
LNsearchData=Data.substring(0,Data.indexOf("#"));
Data=Data.substring(Data.indexOf("#")+1);
}
}
if(rights==false)
{
System.out.println("righ"+tm.data);
while(ht.left!=null)
{
ht=ht.left;
}
ht.left=tm;
}
else
{
while(ht.right!=null)
{
ht=ht.right;
}
ht.right=tm;
}
}
现在我想在 jsp 中将此节点对象显示为树结构,所以在 jsp 中我做到了,
<%
Node1 n1=new Node1("A");
n1.add(n1, new Node1("B"),"A#");
n1.add(n1, new Node1("C"),"A#B#");
n1.add(n1, new Node1("K"),"A#B#C#");
n1.add(n1, new Node1("E"),"A#");
n1.add(n1, new Node1("F"),"A#");
n1.add(n1, new Node1("X"),"X#");
n1.level=1;
n1.traverse(n1);
%>
要在jsp、
中构建树结构
<%!
public void traverse(Node1 di,JspWriter out)throws IOException
{
//System.out.println("Par"+di.data+"level"+level);
out.println("<li class='folder'>"+di.data);
out.println(" <ul>");
if(di.right!=null)
{
//level++;
traverse(di.right,out);
//level--;
}
if(di.left!=null)
{
out.print("<ul>");
//out.print("left: "+di.data+"\n");
traverse(di.left,out);
}
if(di.left==null&&di.right==null)
{
out.println("</ul>");
}
//System.out.println("returnd "+level);
}
%>
<div id="tree">
<%traverse(n1,out); %>
</div>
我面临的问题是我只能遍历节点的右侧,但我必须遍历两侧。谁能帮我解决这个问题。
我猜是 ul 标签没有定义好。
试试这个:
<%!
public void traverse(Node1 di,JspWriter out)throws IOException
{
//System.out.println("Par"+di.data+"level"+level);
out.println("<li class='folder'>"+di.data);
if(di.right!=null)
{
out.print("<ul>");
traverse(di.right,out);
out.println(" </ul>");
}
if(di.left!=null)
{
traverse(di.left,out);
}
}
%>
<div id="tree">
<ul>
<%traverse(n1,out); %>
</ul>
</div>
我写了一个节点 class 用于存储 tree
数据结构,
节点看起来像,
我想开发的树,
public class Node1
{
public String data;
public Node1 left;
public Node1 right;
public static int level=0;
public Node1(String data)
{
this.data=data;
this.left=null;
this.right=null;
}
}
我将节点存储为,
public void add(Node1 ht,Node1 tm,String Data)
{
String LNsearchData=Data.substring(0,Data.indexOf("#"));
Data=Data.substring(Data.indexOf("#")+1);
//System.out.println("righ"+LNsearchData);
boolean rights=false,lefts=false;
while(true)
{
if(ht.data.startsWith(LNsearchData))
{
System.out.println("righ"+tm.data);
if(ht.right==null)
{ rights=true;
break;
}
ht=ht.right;
if(Data.length()==0)
break;
LNsearchData=Data.substring(0,Data.indexOf("#"));
Data=Data.substring(Data.indexOf("#")+1);
}
else
{
if(ht.left==null)
{
break;
}
ht=ht.left;
if(Data.length()==0)
break;
LNsearchData=Data.substring(0,Data.indexOf("#"));
Data=Data.substring(Data.indexOf("#")+1);
}
}
if(rights==false)
{
System.out.println("righ"+tm.data);
while(ht.left!=null)
{
ht=ht.left;
}
ht.left=tm;
}
else
{
while(ht.right!=null)
{
ht=ht.right;
}
ht.right=tm;
}
}
现在我想在 jsp 中将此节点对象显示为树结构,所以在 jsp 中我做到了,
<%
Node1 n1=new Node1("A");
n1.add(n1, new Node1("B"),"A#");
n1.add(n1, new Node1("C"),"A#B#");
n1.add(n1, new Node1("K"),"A#B#C#");
n1.add(n1, new Node1("E"),"A#");
n1.add(n1, new Node1("F"),"A#");
n1.add(n1, new Node1("X"),"X#");
n1.level=1;
n1.traverse(n1);
%>
要在jsp、
中构建树结构<%!
public void traverse(Node1 di,JspWriter out)throws IOException
{
//System.out.println("Par"+di.data+"level"+level);
out.println("<li class='folder'>"+di.data);
out.println(" <ul>");
if(di.right!=null)
{
//level++;
traverse(di.right,out);
//level--;
}
if(di.left!=null)
{
out.print("<ul>");
//out.print("left: "+di.data+"\n");
traverse(di.left,out);
}
if(di.left==null&&di.right==null)
{
out.println("</ul>");
}
//System.out.println("returnd "+level);
}
%>
<div id="tree">
<%traverse(n1,out); %>
</div>
我面临的问题是我只能遍历节点的右侧,但我必须遍历两侧。谁能帮我解决这个问题。
我猜是 ul 标签没有定义好。 试试这个:
<%!
public void traverse(Node1 di,JspWriter out)throws IOException
{
//System.out.println("Par"+di.data+"level"+level);
out.println("<li class='folder'>"+di.data);
if(di.right!=null)
{
out.print("<ul>");
traverse(di.right,out);
out.println(" </ul>");
}
if(di.left!=null)
{
traverse(di.left,out);
}
}
%>
<div id="tree">
<ul>
<%traverse(n1,out); %>
</ul>
</div>