:) 每次都把上次的hello覆盖了。

解决方案 »

  1.   

    呵呵,看仔细才行啊,这个是IF语句并不是WHILE啊,执行一次就完了,
    if(count<5) {
    try {
    pageContext.getOut().print("Hello!");
    }
    catch(Exception e) {
    System.out.println(e);
    }
    repeat = EVAL_BODY_AGAIN;
    count++;
    }
      

  2.   

    pageContext.getOut().print("Hello!");
    改成
    pageContext.getOut().print("Hello!<br>");试试
      

  3.   

    pageContext.getOut().print("Hello!<br/>"); 也不行!jery_lee(U2-G2000)  怎样才能不覆盖呢?
      

  4.   

    pageContext.getOut().print("Hello!<br>");呵呵,不好意思打错了,这样试试
      

  5.   

    static private int count=0;
    必须设置为静态的,才行。
      

  6.   

    这明明就是把循环语句(while)写成了条件语句(if),当然只有一次,你把if改为while就可了。
      

  7.   

    static ??????不是吧???唉我说呢,这么简单的问题100分?原来是疑难杂症!
    最后出一招:重起
      

  8.   

    去看struts的template标签是如何实现循环的
      

  9.   

    TO flashroom(找到啦)
    哥们,你也不要揪着东西就不放,pageContext.getOut().print("Hello!<br/>");
    pageContext.getOut().print("Hello!<br>");我不知道上面两句有什么不同?
      

  10.   

    首先谢谢大家的踊跃发言,不过上面的方法我都试验过了。
    to:nc201()  不要发怒,flashroom(找到啦) 指的不是你!
      

  11.   

    打起来来,咦,好象不关我的事路过。:P我道歉。而且我不针对任何一个人。只是讨论一下技术。共同进步。nc201(),I‘M VERY SORRY !
      

  12.   

    确定一下你的jsp文件写的是否正确,你这个写法应该是正确的。
      

  13.   

    to chinaczg(頑張る人) :不要再这样说了,你没看都要打起来了吗?
      

  14.   

    写个例子你比较一下:
    test.jsp<%@ taglib uri="/taglibtest/taglib.tld" prefix="showresult" %>
    <showresult:Hello/>
      

  15.   

    jsp :<%@ taglib uri="/hellotags.tld" prefix="hello" %><hello:hello>
    world!
    </hello:hello>
      

  16.   

    repeat有问题吧?只要if成立,repeat的值就没有变;
      

  17.   

    改成这样试试:public int doAfterBody() throws JspException {
    int repeat = SKIP_BODY;
    if(count<5) {
    try {
    pageContext.getOut().print("Hello!");
    }
    catch(Exception e) {
    System.out.println(e);
    }
    count++;
                               return EVAL_BODY_AGAIN; }
    return repeat;
    }
      

  18.   

    ??
    为了if和while吵起来了?
    不会吧?
    楼主干脆两个都用用,呵呵。然后把结果贴出来好了。
      

  19.   

    package tag;import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;public class CallTrace extends BodyTagSupport {
    private int iterationCounter=0;
    private static final int NUMBER_OF_ITERATIONS = 3;
    private String name=""; public String getName() throws JspException {
    try {
    pageContext.getOut().print("<h6> getName() called </h6>");
    }
    catch(Exception e) {
    System.err.println(e);
    }
    return name;
    } public void setName(String name) throws JspException {
    this.name = name;
    try {
    pageContext.getOut().print("<h6> setName() called </h6>");
    }
    catch(Exception e) {
    System.err.println(e);
    }
    } public int doStartTag() throws JspException {
    try {
    JspWriter out = pageContext.getOut();
    out.print("<h2>doStartTag() called..");
    out.print("iterating " +NUMBER_OF_ITERATIONS+" times</h2>");
    }
    catch(Exception e) {
    System.err.print(e);
    }
    return EVAL_BODY_BUFFERED;
    } public void setBodyContent(BodyContent body) {
    super.setBodyContent(body);
    try {
    pageContext.getOut().print("<h3>setBodyContent() called </h3>");
    }
    catch(Exception e) {
    System.err.println(e);
    }
    } public void doInitBody() throws JspException {
    try {
    pageContext.getOut().print("<h4> doInitBody() called..<h4>");
    }
    catch(Exception e) {
    System.err.println(e);
    }
    } public int doAfterBody() throws JspException {
    int repeatBody = SKIP_BODY;
    try {
    JspWriter out = pageContext.getOut();
    out.print("<h5>doAfterBody() called..");
    out.print("iteration "+iterationCounter);
    if(iterationCounter<NUMBER_OF_ITERATIONS) {
    repeatBody = EVAL_BODY_AGAIN;
    iterationCounter++;
    }
    else 
    out.print(",Stopping.");
    out.print("</h5>");
    }
    catch(Exception e) {
    System.err.println(e);
    }
    return repeatBody;
    } public int doEndTag() throws JspException {
    try {
    JspWriter out = pageContext.getOut();
    this.getBodyContent().writeOut(out);
    out.print("<h2>doEndTag() calles..</h2>");
    }
    catch(Exception e) {
    System.err.print(e);
    }
    return EVAL_PAGE;
    }
    }这个是书上的程序,能循环,我把中间循环的部分拿出来了,他就不循环了。
      

  20.   

    用你的jsp吗?我开始就是那样的,不行才改成上面的。
      

  21.   

    输出都没有,我倒
    应该改成这样:
    package tag;import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;public class Hello extends BodyTagSupport { private int count=0; public int doAfterBody() throws JspException {
         int repeat = SKIP_BODY;
                  try{
                      if(count<5) {
                           pageContext.getOut().print("Hello!");
                           repeat = EVAL_BODY_AGAIN;
                           count++;
                       }
                       else
                           bodyContent.writeOut(getPreviousOut());//输出
                  }catch(Exception e) {
                      System.out.println(e);
                  }
                  return repeat;
             }
    }jsp :<%@ taglib uri="/hellotags.tld" prefix="hello" %><hello:hello>
    world!<br>
    </hello:hello>
      

  22.   

    to: 3332221119(飘)  
     pageContext.getOut().print("Hello!");
     这句不是输出吗?
      

  23.   

    不是,在doAfterBody()中,pageContext.getOut().print("Hello!");只写入了标签体内容的缓存,需要写回外层写入器才能输出。
      

  24.   

    那我改成下面的,怎么也不输出?package tag;import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;public class Hello extends BodyTagSupport { private int count=0; public int doAfterBody() throws JspException {
         int repeat = SKIP_BODY;
     JspWriter out = pageContext.getOut();
                  try{
                      if(count<5) {
       out.print("Hello!");
                           repeat = EVAL_BODY_AGAIN;
                           count++;
                       }
                       else
                           bodyContent.writeOut(out);//输出
                  }catch(Exception e) {
                      System.out.println(e);
                  }
                  return repeat;
             }
    }
      

  25.   

    在doAfterBody()中,在缓存中写入,如
     JspWriter out = pageContext.getOut();
    out.print("Hello!");
    相当于堆栈。你的代码中在count=5时,JspWriter out = pageContext.getOut();new了一个out对象没副值,是空的。用bodyContent.writeOut(out);//输出,当然是空的