TagSupport与BodyTagSupport的区别主要是标签处理类是否需要与标签体交互,如果不需要交互的就用TagSupport,否则如果需要交互就用BodyTagSupport。
交互就是标签处理类是否要读取标签体的内容和改变标签体返回的内容。
交互就是标签处理类是否要读取标签体的内容和改变标签体返回的内容。
用TagSupport实现的标签,都可以用BodyTagSupport来实现,因为BodyTagSupport继承了TagSupport;
TagSupport
public class LangHuaTag extends TagSupport {
private long startTime;
private long endTime;
public int doStartTag() throws JspException {
startTime = System.currentTimeMillis();
return TagSupport.EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
endTime = System.currentTimeMillis();
long elapsed = endTime - startTime;
try {
JspWriter out = pageContext.getOut();
out.println("runtime is "+ elapsed);
} catch (IOException e) {
e.printStackTrace();
}
return TagSupport.EVAL_PAGE;
}
}
TagSupport
public class DisplayTag extends TagSupport {
public int doStartTag() throws JspException {
System.out.println("doStartTag");
return this.EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException {
System.out.println(" doAfterBod");
return this.SKIP_BODY;
}
public int doEndTag() throws JspException {
System.out.println("doEndTag");
JspWriter out = this.pageContext.getOut();
try {
out.println("enen");
} catch (IOException e) {
e.printStackTrace();
}
return super.doEndTag();
}
}