JSP1.x 自訂標籤,jsp1.x自訂標籤

來源:互聯網
上載者:User

JSP1.x 自訂標籤,jsp1.x自訂標籤

Tag介面

任何一個標籤都對應著一個java類,該類必須實現Tag介面,JSP遇到一個標籤後後,將通過一個tld檔案尋找該標籤的實作類別,並運行該類的相關方法

import javax.servlet.jsp.JspException;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.Tag; public class TagTest implements Tag {      private Tag parent;      private PageContext pageContext;       @Override      public int doEndTag() throws JspException{  //標籤結束時執行             JspWriter out =pageContext.getOut();             try{                    out.println("……");             }catch(IOExceptione){                    throw new JspException(e);             }             return EVAL_PAGE;      }       @Override      public int doStartTag() throwsJspException {  //標籤開始時執行             return SKIP_BODY;      }       @Override      public Tag getParent() {             // TODO Auto-generated method stub             return null;      }       @Override      public void release() {             // TODO Auto-generated method stub       }       @Override      public void setPageContext(PageContextarg0) {             // TODO Auto-generated method stub       }       @Override      public void setParent(Tag arg0) {             // TODO Auto-generated method stub       } }


doStartTag方法可以返回兩種參數,如果為SKIP_BODY,表示標籤體內的內容不被輸出,如果為EVAL_BODY_INCLUDE,則執列標籤體內的代碼

doEndTag也可以返回兩種參數,如果為SKIP_PAGE,表示不執列標籤後面的內容。如果為EVAL_PAGE,則執列標籤後面的內容

此類中有兩個屬性,parent和pageContext,parent為該標籤的父標籤(或者是上一層的標籤),pageContext為運行該標籤的JSP頁面,這兩個參數都是JSP在運行時通過setter方法注射進去的

 

Tag實作類別有了,還需要tld檔案(Tag Library Descriptor)

 

<?xml version="1.0" ecoding="UTF-8"> <taglib xmlns=http://java.sun.com/xml/ns/j2ee      xmlns:xsi:="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee                 web-jsptaglibrary_2_0.xsd      version="2.0">       <tlibversion>1.0</tlibversion>      <jspversion> 1.1</jspversion>      <shortname>tagname</shortname>      <uri>http://www.clf.com/tags</uri>      <info>A simple tag test</info> <tag>      <name>tagname</name>      <tagclass>com.chen.tags.TagTest</tagclass>      <bodycontent>JSP</ bodycontent>      <info>tag information</info></tag> </taglib>

 

shortname也就是推薦使用的prefix,uri就是引用這個標籤庫時使用的uri

bodycontent為標籤體的限制,有三種取值

empty:不允許有標籤體的存在,如果有會拋出異常

JSP:允許有標籤體存在,可以為JSP代碼

tagdependent:允許有標籤體存在,但是標籤裡的JSP代碼不會被執行

 

如果tld檔案位於/WEB-INF/下面,Tomcat會自動載入;如果位於其他位置,可以在web.xm中配置

<jsp-config>      <taglib>             <taglib-uri> http://www.clf.com/tags</taglib-uri>             <taglib-location>/WEB-INF/taglib.tld</taglib-location>      </taglib></jsp-config>

TagSupport

多數情況下不需要直接實現Tag介面,使用TagSupport類就可以了,此類是java提供的一個模板類,一般來說,只需要實現doStartTag方法和doEndTag方法就可以了

 

import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.TagSupport;  public class Tagtest extends TagSupport {      private int num1;      private int num2;       @Override      public int doEndTag() throws JspException{             try {             this.pageContext.getOut().println("兩數相加等於:"+(num1+num2));             } catch (Exception e) {                                }             return EVAL_PAGE;      }       @Override      public int doStartTag() throwsJspException {             return super.doStartTag();      }       public int getNum1() {             return num1;      }       public void setNum1(int num1) {             this.num1 = num1;      }       public int getNum2() {             return num2;      }       public void setNum2(int num2) {             this.num2 = num2;      } }

 

設定檔

 

<tag>      <name>add</name>      <tagclass>com.chen.tags.TagTest</tagclass>      <bodycontent>JSP</bodycontent>      <info>tag information</info>      <attribute>             <name>num1</name>             <required>true</required >             <rtexprvalue>true</rtexprvalue >      </attribute>      <attribute>             <name>num2</name>             <required>true</required >             <rtexprvalue>true</rtexprvalue >      </attribute></tag>

 

name代表屬性的名字,required代表是否是必需的,rtexprvalue代表此屬性是否允許使用EL運算式

以上這個標籤這樣使用:

<taglib:add num1=”2” num2=”3”/>

則頁面會輸出2和3 的相加之和

 

BodyTagSupport

BodyTagSupport是Tag介面的子類,專門處理帶標籤體的標籤

public class ToLowerCaseTag extends BodyTagSupport{      public int doEndTag throws JspException{             //擷取標籤體內的代碼             String contend = this.getBodyContent().getString();      try{             this.pageContent.getOut().println(content.toLowerCase());      }catch(Exception e){   }             return EVAL_PAGE;      }}

只要在setBodyContent()方法之後被調用的方法中,都可以使用getBodyContent()方法擷取標籤體的內容,一般為doAfterBody()或者doEndTag()

BodyTagSupport的執行流程如下:

1.當容器建立一個新的標籤執行個體後,通過setPageContext來設定標籤的頁面上下文.

2.使用setParent方法設定這個標籤的上一級標籤,如果沒有上一級嵌套,設定為null.

3.設定標籤的屬性,這個屬性在標籤庫描述檔案中定義,如果沒有定義屬性,就不調用此類方法.

4.調用doStartTag方法,這個方法可以返回EVAL_BODY_INCLUDE和SKIP_BODY,當返回EVAL_BODY_INCLUDE時,就計算標籤的body,如果返回SKIP_BODY,就不再計算標籤的body,如果為EVAL_BODY_BUFFERED,則不會輸出,而是將標籤體內容通過setBodyContent()方法注射到標籤類中,然後就可以使用getBodyContent()取得標籤體的內容

5.調用setBodyContent設定當前的BodyContent.

6.調用doInitBody,如果計算BodyContent時需要進行一些初始化,就在這個方法中進行.

7.每次計算完Body後調用doAfterBody,如果返回EVAL_BODY_AGAIN,表示繼續計算一次Body,直到返回SKIP_BODY才繼續往下執行.

8.調用doEndTag方法,這個方法可以返回EVAL_PAGE或者SKIP_PAGE,當返回EVAL_PAGE時,容器將在標籤結束時繼續計算JSP頁面其他的部分;如果返回SKIP_PAGE,容器將在標籤結束時停止計算JSP頁面其他的部分.

9.調用release()方法釋放標籤程式佔用的任何資源。

 

 

public class LoopTag extends BodyTagSupport{      private int times;           public int doStartTag() throwsJspException{             times = 5;             return super.doStartTag();      }       public int doAfterBody() throwsJspException{             if(times-- >0){ 只要times大於零繼續迴圈,同時times自減                    try{                    this.getPreviousOut().println(this.getBodyContent().getString());                    }catch(Exception e){  }                    return EVAL_BODY_AGAIN;  //繼續執行             }else{                    return SKIP_BODY;             }      }}

以上定義了一個迴圈標籤

<taglib:loop>loop.</taglib:loop>

會得到如下輸出:

loop.

loop. loop.

loop. loop. loop.

loop. loop. loop.loop.

loop. loop. loop.loop. loop.

 

實際上,doAfterBody方法內的輸出是寫到bodyContent緩衝中的

BodyTagSupport中,裡面的標籤可以通過getparent()方法獲得上層的標籤

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.