情况是这样的,在doStartTag() 中得到的ArrayList对象为空,而同样的代码在main函数里却可以得到返回值
真是奇怪,还望各位指教
package taglib;
import java.util.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.IOException;
import Node.NODE;
public class TreeTag implements Tag
{
private PageContext context = null;
public TreeTag()
{
super();
}
public void setPageContext(PageContext pagecontext)
{
this.context = pagecontext;
}
public void setParent(Tag tag)
{
}
public Tag getParent()
{
return null;
}
public int doStartTag() throws JspException
{
ArrayList list = NODE.getAllNodes();
try
{
context.getOut().println(list.size()); // 程序中显示输出0
context.getOut().println("<script type=\"text/javascript\">");
context.getOut().println("d = new dTree('d');");
for(int i = 0; i < list.size(); i ++)
{
NODE n = (NODE)(list.get(i));
context.getOut().println("d.add("+n.getID()+","
+ n.getPID() + ","
+ n.getCaption() +" );");
}
context.getOut().println("</script>");
context.getOut().println("</div></body></html>");
}
catch(IOException ex)
{
throw new JspException(ex.getMessage());
}
return SKIP_BODY;
}
public int doEndTag()
{
return SKIP_PAGE;
}
public void release()
{
}
public static void main(String args[]) // main 里面确可以正常读取数据,l不为空
{
ArrayList l = NODE.getAllNodes();
for(int i = 0; i < l.size(); i ++)
{
NODE n = (NODE)(l.get(i));
System.out.println(n.getCaption());
}
}
}