1:区别
TagSupport与BodyTagSupport的区别
TagSupport与BodyTagSupport的区别主要是标签处理类是否需要与标签体交互,如果不需要交互就用TagSupport,若需要交互就用BodyTagSupport。
交互就是标签处理类是否要读取标签体的内容和改变标签体返回的内容。
用TagSupport实现的标签,都可以用BodyTagSupport实现,因为BodyTagSupport继承了TagSupport。
2:怎么用
TagSupport:
- //最简单的标签
- public class LangHuaTag extends TagSupport {
- private long startTime;
- private long endTime;
- public int doStartTag() throws JspException {
- startTime = System.currentTimeMillis();
- //表示定制标记里面有所包括的JSP页面
- 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();
- }
- //表示JSP页面继续运行
- return TagSupport.EVAL_PAGE;
- }
- }
BodyTagSupport:
- public class DisplayTag extends TagSupport {
- public int doStartTag() throws JspException {
- System.out.println("********* doStartTag()........\n");
- return this.EVAL_BODY_INCLUDE;
- }
- public int doAfterBody() throws JspException {
- System.out.println("********* doAfterBody()........\n");
- return this.SKIP_BODY;
- }
- public int doEndTag() throws JspException {
- System.out.println("********* doEndTag()........\n");
- JspWriter out = this.pageContext.getOut();
- try {
- out.println("Hello !!!!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return super.doEndTag();
- }
- }