自定義JSP標籤

一、自定義JSP標籤的執行過程

當一個含有自定義標籤的JSP頁面被JSP引擎(Web容器)轉譯成Servelt時,JSP引擎遇到自定義的標籤,會把這個自定標籤標識成對一個稱爲“標籤處理類”的調用。當這個JSP頁面被執行時,JSP引擎就會調用這個“標籤處理類”對象,並執行其內部定義的相應操作方法,從而完成相應的功能。

從這個執行過程來看,自定義標籤就是把原來編寫在JSP頁面的Java代碼單獨封裝到一個Java類中,當調用自定義標籤時,其實是映射調用相應的“標籤處理類”來完成工作。

二、自定義JSP標籤的開發流程

使用Java處理類來開發自定義JSP標籤時,主要分爲以下幾個步驟:

(1)       創建標籤的處理類(Tag Handle Class)這個類用來定義標籤的行爲,並在JSP引擎遇到自定義標籤時調用執行。標籤處理類是一個Java類,這個類只需要繼承了JSP標籤API中提供的接口或類就可以很簡單地實現自定義JSP標籤的具體功能。

(2)       創建標籤庫描述文件(Tag Library Descriptor File)。這個文件是描述標籤庫的XML文檔,它描述了標籤庫中每個標籤的屬性詳細信息,向JSP引擎提供有關自定義標籤的標籤處理程序的信息。

(3)       在web.xml文件中聲明TLD的位置。(在使用一些集成框架時,這個步驟不是必須的。 例如在下面的例子中用到了spring,只要在jsp中引入即可使用。)

(4)       在JSP文件中用taglib指令引入標籤庫,然後使用標籤庫描述文件中指定的標籤名來使用它。 

三、JSP標籤API

Javax.servlet.jsp.tagext包

JSP 1.1和1.2規範中常用的接口主要有以下3個。

Tag:此接口定義對於所有標籤處理類都需要實現的方法。

IterationTag:此接口擴展了Tag接口,增加了控制重複執行標籤主體的方法。

BodyTag:此接口擴展了IterationTag接口,並增加了訪問和操作標籤主體內容的方法。

JSP API針對這3個接口還提供了兩個支持類,即TagSuppor和BodyTagSupport,用來簡化自定義標籤的開發。

四、標籤庫描述符

標籤庫描述符文件是一個以“.tld”結尾的標準XML文檔,用來記錄一個標籤庫內擁有哪些標籤、第個標籤包含哪些屬性。

<taglib>元素是標籤庫描述符的根元素,它包含12個子元素,如

1.         <description>:標籤庫的一個文本描述。

2.         <tlib-version>:指定標籤庫的版本。

3.         <short-name>:爲標籤定義簡短的名字,在taglib指令中可作爲首選的前綴名使用。

4.         <uri>:定義一個URL,用於唯一地標識些標籤庫。

5.         <tag>:用於指定自定義標籤的相關信息。<tag>元素有12個子元素,這裏只對部分元素做介紹,如下:

a)         <description>:爲自定義標籤提供一個文本描述。

b)        <display-name>:爲標籤指定一個簡短的名字。

c)        <name>:指定標籤的名字。

d)        <tag-class>:指定標籤處理類的完整路徑。

e)         <body-content>:指定標籤體的格式。格式有四種:

²        empty:標識標籤沒有標籤體。

²        scriptless:表示標籤體可以包含EL表達式和JSP的動作元素,但是不能包含JSP的腳本元素。

²        JSP:表示標籤體可以包含JSP代碼。

²        tagdependent:表示標籤體由標籤本身去解析處理。若指定tagdependent,那麼在標籤體中所寫的代碼將作爲純文本原封不動地傳給處理類,而不是將執行的結果傳給標籤處理類。

f)<attribute>:該標籤用於設置標籤的屬性。該元素有6個子元素。具體如下:

²        <description>:爲屬性提供一個文本描述

²        <name>:指定屬性的名字

²        <required>:指定該屬性是否必須。默認爲false

²        <rtexprvaalue>:指定屬性值是否可以在JSP運行時期動態產生

²        <type>:指定屬性的類型

²        <fragment>:指定屬性是否爲JspFragment對象,默認false

g)<example>:用於提供一個使用該標籤例子的信息描述。

h)<variable>:定義標籤處理類提供給JSP頁面使用的腳本變量。

i)<dynamic-attributes>:指定標籤是否支持動態屬性,取值是否支持動態屬性,取值爲true或者false。

j)<icon>、<tag-extension>、<tei-class>

6、<display-name>:爲標籤庫指定一個簡短的別名。

7、<small-icon>:爲標籤庫指定大小爲16x16的小圖標(gif或jpeg格式),該圖標可在圖形界面工具中顯示。

8、<large-icon>:爲標籤庫指定大小爲32x32的小圖標(gif或jpeg格式),該圖標可在圖形界面工具中顯示。

9、<validator>:爲標籤庫提供一個驗證器。

10、<listener>:爲標籤庫提供一個監聽器。

11、<tag-file>:用於描述標籤文件。

12、<function>:用於指定在表達式語言中使用的函數。

--------------------------------------------------------------------以上內容爲轉載------------------------------------------------------------------

實踐代碼:<這是一個自己寫的例子,測試通過>

1、創建標籤的處理類(Tag Handle Class

 

package org.test.zTest;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.TagSupport;

 

public class TagTest extends TagSupport {

         private static final long serialVersionUID = 3095327982159183182L;

         // 自定義標籤的部分屬性

         private String name;

         private String type;

         private String value = null;

         private String onclick;

         private String show;

         private String id;

 

         public String getShow() {

                   return show;

         }

 

         public void setShow(String show) {

                   this.show = show;

         }

 

         public String getName() {

                   return name;

         }

 

         public String getId() {

                   return id;

         }

 

         public void setId(String id) {

                   this.id = id;

         }

 

         public void setName(String name) {

                   this.name = name;

         }

 

         public String getType() {

                   return type;

         }

 

         public void setType(String type) {

                   this.type = type;

         }

 

         public String getValue() {

                   return value;

         }

 

         public void setValue(String value) {

                   this.value = value;

         }

 

         public String getOnclick() {

                   return onclick;

         }

 

         public void setOnclick(String onclick) {

                   this.onclick = onclick;

         }

 

         @Override

         public int doStartTag() throws JspException {

                   return SKIP_BODY;

         }

 

         @Override

         public int doEndTag() throws JspException {

                   StringBuffer bufHtml = new StringBuffer();

                   if(!(show == null || "".equals(show)) && "label".equals(show)) {

                            bufHtml.append("<label ");

                            if (!(id == null || "".equals(id))) {

                                     bufHtml.append("id=\"" + id + "\" ");

                            }

                            bufHtml.append(">"+value+"</label>");

                   }else {

                            bufHtml.append("<input ");

                            if (!(name == null || "".equals(name))) {

                                     bufHtml.append("name=\"" + name + "\" ");

                            }

                            if (!(type == null || "".equals(type))) {

                                     bufHtml.append("type=\"" + type + "\" ");

                            }

                            if (!(value == null || "".equals(value))) {

                                     bufHtml.append("value=\"" + value + "\" ");

                            }

                            if (!(onclick == null || "".equals(onclick))) {

                                     bufHtml.append("οnclick=\"" + onclick + "\" ");

                            }

                            bufHtml.append("/>");

                   }

 

                   JspWriter out = this.pageContext.getOut();

                   try {

                            out.print(bufHtml.toString());

                   } catch (IOException e) {

                            e.printStackTrace();

                   }

                  

                   return EVAL_PAGE;

         }

}

 

2、創建標籤庫描述文件(Tag Library Descriptor FiletestTag.tld

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>

 <tlib-version>1.0</tlib-version>

 <jsp-version>1.2</jsp-version>

 <short-name>csdn</short-name>

 <uri>http://www.csdn.com</uri>

<description>

       <![CDATA[Custom tag library for this application]]>

</description>

 <tag> 

      <name>testTag</name>

      <tag-class>org.test.zTest.TagTest </tag-class>

      <body-content>jsp</body-content>

      <attribute>

         <name>show</name>

         <required>true</required>

      </attribute>

      <attribute>

         <name>name</name>

         <required>false</required>

      </attribute>

      <attribute>

         <name>type</name>

         <required>false</required>

      </attribute>

      <attribute>

         <name>value</name>

         <required>false</required>

      </attribute>

      <attribute>

         <name>onclick</name>

         <required>false</required>

      </attribute>

     

      <attribute>

         <name>id</name>

         <required>false</required>

      </attribute>

   </tag>

</taglib>

3、在JSP文件中用taglib指令引入標籤庫

<%@ page language="java"import="java.util.*"pageEncoding="GBK"%>

<%@ taglib uri="/WEB-INF/testTag.tld"prefix="jc"%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>My JSP 'tagTest.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

</head>

<scripttype="text/javascript">

    function showClick() {

       alert("test OK!");

    }

    </script>

<body>

    輸入文字:

    <jc:testTagshow="input"name="inFont"type="text"/>

    <br/>

    輸入密碼:

    <jc:testTagshow="label"id="inPsW"value="Test Ok"></jc:input>

    <br/>

    按鈕提交:

    <jc:testTag show="input"name="btnSubmit"type="submit"value="提交"/>

    <br/>

    按鈕重置:

    <jc:testTag show="input"name="btnReset"type="reset"onclick="clearContent();"value="重置"/>

    <br/>

    測試按鈕:&nbsp;

    <jc:testTag show="input"name="btnClick"type="reset"onclick="showClick();"value="按鈕"/>

    </body>

</html>

 <完>

 

發佈了26 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章