JSP自定義標籤01

 

演示傳統自定義標籤(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;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章