那我改成下面的,怎么也不输出?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; } }
在doAfterBody()中,在缓存中写入,如 JspWriter out = pageContext.getOut(); out.print("Hello!"); 相当于堆栈。你的代码中在count=5时,JspWriter out = pageContext.getOut();new了一个out对象没副值,是空的。用bodyContent.writeOut(out);//输出,当然是空的
if(count<5) {
try {
pageContext.getOut().print("Hello!");
}
catch(Exception e) {
System.out.println(e);
}
repeat = EVAL_BODY_AGAIN;
count++;
}
改成
pageContext.getOut().print("Hello!<br>");试试
必须设置为静态的,才行。
最后出一招:重起
哥们,你也不要揪着东西就不放,pageContext.getOut().print("Hello!<br/>");
pageContext.getOut().print("Hello!<br>");我不知道上面两句有什么不同?
to:nc201() 不要发怒,flashroom(找到啦) 指的不是你!
test.jsp<%@ taglib uri="/taglibtest/taglib.tld" prefix="showresult" %>
<showresult:Hello/>
world!
</hello:hello>
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;
}
为了if和while吵起来了?
不会吧?
楼主干脆两个都用用,呵呵。然后把结果贴出来好了。
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;
}
}这个是书上的程序,能循环,我把中间循环的部分拿出来了,他就不循环了。
应该改成这样:
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>
pageContext.getOut().print("Hello!");
这句不是输出吗?
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;
}
}
JspWriter out = pageContext.getOut();
out.print("Hello!");
相当于堆栈。你的代码中在count=5时,JspWriter out = pageContext.getOut();new了一个out对象没副值,是空的。用bodyContent.writeOut(out);//输出,当然是空的