大家看看这段代码
import com.hit.bean.Product;import java.io.IOException;
import java.util.List;import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;public class WhileTag extends TagSupport
{ public WhileTag()
{
type = "";
src = null;
note = "";
cols = "";
index = 0;
count = 0;
} public void setSrc(List src)
{
this.src = src;
} public void setType(String type)
{
this.type = type;
} public void setNote(String note)
{
this.note = note;
} public void setCols(String cols)
{
this.cols = cols;
} public void setCols(int cols)
{
this.cols = String.valueOf(cols);
} public int doStartTag()
throws JspException
{
index = 0;
count = src.size();
System.out.println(count);
pageContext.setAttribute(getId(), (Product)src.get(index++));
return 1;
} public int doAfterBody()
throws JspException
{
if(index >= count)
{
return 0;
} else
{
pageContext.setAttribute(getId(), (Product)src.get(index++));
return 2;
}
} public int doEndBody()
throws JspException
{
return 6;
} public void release()
{
type = null;
src = null;
note = null;
cols = null;
super.release();
}
private Product product;
private String type;
private List src;
private String note;
private String cols;
private int index;
private int count;
}如果我传过来的list里面有三条记录,也就是我想让这三条记录在jsp页面迭代出来,当doAfterBody方法return 2的时候是不是又调用doStartTag()这个方法啦,如果调用这个doStartTag()方法,那index不是又被赋值为零了吗?如果赋值为零不就是死循环了吗?谁能帮我解释一下,谢谢啦!也许我现在是看迷糊了。
import com.hit.bean.Product;import java.io.IOException;
import java.util.List;import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;public class WhileTag extends TagSupport
{ public WhileTag()
{
type = "";
src = null;
note = "";
cols = "";
index = 0;
count = 0;
} public void setSrc(List src)
{
this.src = src;
} public void setType(String type)
{
this.type = type;
} public void setNote(String note)
{
this.note = note;
} public void setCols(String cols)
{
this.cols = cols;
} public void setCols(int cols)
{
this.cols = String.valueOf(cols);
} public int doStartTag()
throws JspException
{
index = 0;
count = src.size();
System.out.println(count);
pageContext.setAttribute(getId(), (Product)src.get(index++));
return 1;
} public int doAfterBody()
throws JspException
{
if(index >= count)
{
return 0;
} else
{
pageContext.setAttribute(getId(), (Product)src.get(index++));
return 2;
}
} public int doEndBody()
throws JspException
{
return 6;
} public void release()
{
type = null;
src = null;
note = null;
cols = null;
super.release();
}
private Product product;
private String type;
private List src;
private String note;
private String cols;
private int index;
private int count;
}如果我传过来的list里面有三条记录,也就是我想让这三条记录在jsp页面迭代出来,当doAfterBody方法return 2的时候是不是又调用doStartTag()这个方法啦,如果调用这个doStartTag()方法,那index不是又被赋值为零了吗?如果赋值为零不就是死循环了吗?谁能帮我解释一下,谢谢啦!也许我现在是看迷糊了。
private int i = 1;
public void setI(int i) {
this.i = i;
}
public int doStartTag()
throws JspException
{
if (src!= null && src.size()> 0) {
pageContext.setAttribute(getId(),(Product)src.get(0));
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
} public int doAfterBody()
throws JspException
{
if (i < src.size()) {
pageContext.setAttribute(getId(),(Product)src.get(i));
i++;
return EVAL_BODY_AGAIN;// 循环
} else {
return SKIP_BODY;
}} }
当程序读到标签<便会去寻找标签处理类,调用它的doStartTag(),执行 index = 0;
count = src.size();
System.out.println(count);
pageContext.setAttribute(getId(), (Product)src.get(index++)); 然后返回到jsp页面,这里的pageContext.setAttribute(getId(), (Product)src.get(index++)); 就是给jsp页面那个脚本变量赋值,那个getId()是TagSupport类自带的set和get方法不用去设置,getId()设置的是jsp页面里要迭代的bean。当执行完jsp页面里面的body体,就会去执行doStartTag()方法里面的return1这个return1就是EVAL_BODY_INCLUDE,是人家个最终静态常量,当执行return1的时候,程序就会调用doAfterBody(),(jsp内部就是这么实现的)然后执行方法体if(index >= count)
{
return 0;
} else
{
pageContext.setAttribute(getId(), (Product)src.get(index++));
return 2;
}
此处就是我原来不理解的地方(如果我传过来的list里面有三条记录,也就是我想让这三条记录在jsp页面迭代出来,当doAfterBody方法return 2的时候是不是又调用doStartTag()这个方法啦,如果调用这个doStartTag()方法,那index不是又被赋值为零了吗?如果赋值为零不就是死循环了吗?)
其实程序走到这里,再也不会回到doStartTag() 方法了,所以我担心的情况也就不存在了,(最恨那些灌水的文章,害我理解成了返回到doStartTag() ) 程序现在是循环的把脚本变量里面的值往外输出。也就是执行完doAfterBody()里面的 pageContext.setAttribute(getId(), (Product)src.get(index++)); 然后执行bodycontent就是我迭代标签之间的东西。然后再去执行return2也就是再去调用doAfterBody()方法,直到(index < count)时执行return0才跳出我的这个标签。
sun有说明的啊