Jsp自定义标签01

来源:互联网 发布:c语言fclose 编辑:程序博客网 时间:2024/06/05 17:23
 

1、演示viewIP

1)        演示jsp中java代码实现

2)        自定义标签实现

 

2、演示传统自定义标签(jsp2.0以前的)

1)        使用自定义标签控制页面内容(标签体)是否输出,利用doStartTag()的返回值控制

return this.SKIP_BODY; //忽略标签体

return this.EVAL_BODY_INCLUDE; //执行标签体

 

2)        控制整个jsp的输出

利用doEndTag()的返回值控制

return this.SKIP_PAGE;  //跳过页面标签后余下的jsp代码

return this.EVAL_PAGE; //继续执行余下jsp代码

 

3)        自定义标签实现内容(标签体)循环输出

利用Tag子接口Iteration中定义的doAfterBody()和返回值EVAL_BODY_AGAIN,SKIP_BODY实现

a)    先覆盖doStartTag()方法,返回EVAL_BODY_INCLUDE

b)    覆盖doAfterBody()

public int doAfterBody() throws JspException {

   times++;

   int result = this.EVAL_BODY_AGAIN;

   if(times>4){

      result = this.SKIP_BODY;

   }

   return result;

}

 

4)        自定义标签修改内容(标签体)EVAL_BODY_BUFFERED;

标签处理类:

c)        继承BodyTagSupport

d)       覆盖doStartTag(),并返回EVAL_BODY_BUFFERED;

e)        覆盖doEndTag()

public int doEndTag() throws JspException {

   BodyContent bc = this.getBodyContent();

   String c = bc.getString();

   c = c.toUpperCase();

  

   JspWriter out = this.pageContext.getOut();

   try {

      out.write(c);

   } catch (IOException e) {

      throw new RuntimeException(e);

   }

  

   return this.EVAL_PAGE;

}

原创粉丝点击