jsp 自定義標(biāo)簽
自定義標(biāo)簽是用戶(hù)定義的jsp語(yǔ)言元素。當(dāng)jsp頁(yè)面包含一個(gè)自定義標(biāo)簽時(shí)將被轉(zhuǎn)化為servlet,標(biāo)簽轉(zhuǎn)化為對(duì)被 稱(chēng)為tag handler的對(duì)象的操作,即當(dāng)servlet執(zhí)行時(shí)web container調(diào)用那些操作。
jsp標(biāo)簽擴(kuò)展可以讓你創(chuàng)建新的標(biāo)簽并且可以直接插入到一個(gè)jsp頁(yè)面。 jsp 2.0規(guī)范中引入simple tag handlers來(lái)編寫(xiě)這些自定義標(biāo)記。
你可以繼承simpletagsupport類(lèi)并重寫(xiě)的dotag()方法來(lái)開(kāi)發(fā)一個(gè)最簡(jiǎn)單的自定義標(biāo)簽。
創(chuàng)建"hello"標(biāo)簽
接下來(lái),我們想創(chuàng)建一個(gè)自定義標(biāo)簽叫作<ex:hello>,標(biāo)簽格式為:
<ex:hello />
要?jiǎng)?chuàng)建自定義的jsp標(biāo)簽,你首先必須創(chuàng)建處理標(biāo)簽的java類(lèi)。所以,讓我們創(chuàng)建一個(gè)hellotag類(lèi),如下所示:
package com.yapf; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class hellotag extends simpletagsupport { public void dotag() throws jspexception, ioexception { jspwriter out = getjspcontext().getout(); out.println("hello custom tag!"); } }
以下代碼重寫(xiě)了dotag()方法,方法中使用了getjspcontext()方法來(lái)獲取當(dāng)前的jspcontext對(duì)象,并將"hello custom tag!"傳遞給jspwriter對(duì)象。
編譯以上類(lèi),并將其復(fù)制到環(huán)境變量classpath目錄中。最后創(chuàng)建如下標(biāo)簽庫(kù):<tomcat安裝目錄>webapps\root\web-inf\custom.tld。
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>example tld</short-name> <tag> <name>hello</name> <tag-class>com.yapf.hellotag</tag-class> <body-content>empty</body-content> </tag> </taglib>
接下來(lái),我們就可以在jsp文件中使用hello標(biāo)簽:
<%@ taglib prefix="ex" uri="web-inf/custom.tld"%> <html> <head> <title>a sample custom tag</title> </head> <body> <ex:hello/> </body> </html>
以上程序輸出結(jié)果為:
hello custom tag!
訪(fǎng)問(wèn)標(biāo)簽體
你可以像標(biāo)準(zhǔn)標(biāo)簽庫(kù)一樣在標(biāo)簽中包含消息內(nèi)容。如我們要在我們自定義的hello中包含內(nèi)容,格式如下:
<ex:hello> this is message body </ex:hello>
我們可以修改標(biāo)簽處理類(lèi)文件,代碼如下:
package com.yapf; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class hellotag extends simpletagsupport { stringwriter sw = new stringwriter(); public void dotag() throws jspexception, ioexception { getjspbody().invoke(sw); getjspcontext().getout().println(sw.tostring()); } }
接下來(lái)我們需要修改tld文件,如下所示:
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>example tld with body</short-name> <tag> <name>hello</name> <tag-class>com.yapf.hellotag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
現(xiàn)在我們可以在jsp使用修改后的標(biāo)簽,如下所示:
<%@ taglib prefix="ex" uri="web-inf/custom.tld"%> <html> <head> <title>a sample custom tag</title> </head> <body> <ex:hello> this is message body </ex:hello> </body> </html>
以上程序輸出結(jié)果如下所示:
this is message body
自定義標(biāo)簽屬性
你可以在自定義標(biāo)準(zhǔn)中設(shè)置各種屬性,要接收屬性,值自定義標(biāo)簽類(lèi)必須實(shí)現(xiàn)setter方法, javabean 中的setter方法如下所示:
package com.yapf; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class hellotag extends simpletagsupport { private string message; public void setmessage(string msg) { this.message = msg; } stringwriter sw = new stringwriter(); public void dotag() throws jspexception, ioexception { if (message != null) { /* 從屬性中使用消息 */ jspwriter out = getjspcontext().getout(); out.println( message ); } else { /* 從內(nèi)容體中使用消息 */ getjspbody().invoke(sw); getjspcontext().getout().println(sw.tostring()); } } }
屬性的名稱(chēng)是"message",所以setter方法??是的setmessage()?,F(xiàn)在讓我們?cè)趖ld文件中使用的<attribute>元素添加此屬性:
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>example tld with body</short-name> <tag> <name>hello</name> <tag-class>com.yapf.hellotag</tag-class> <body-content>scriptless</body-content> <attribute> <name>message</name> </attribute> </tag> </taglib>
現(xiàn)在我們就可以在jsp文件中使用message屬性了,如下所示:
<%@ taglib prefix="ex" uri="web-inf/custom.tld"%> <html> <head> <title>a sample custom tag</title> </head> <body> <ex:hello message="this is custom tag" /> </body> </html>
以上實(shí)例數(shù)據(jù)輸出結(jié)果為:
this is custom tag
你還可以包含以下屬性:
屬性 | 描述 |
---|---|
name | 定義屬性的名稱(chēng)。每個(gè)標(biāo)簽的是屬性名稱(chēng)必須是唯一的。 |
required | 指定屬性是否是必須的或者可選的,如果設(shè)置為false為可選。 |
rtexprvalue | 聲明在運(yùn)行表達(dá)式時(shí),標(biāo)簽屬性是否有效。 |
type | 定義該屬性的java類(lèi)類(lèi)型 。默認(rèn)指定為 string |
description | 描述信息 |
fragment | 如果聲明了該屬性,屬性值將被視為一個(gè) jspfragment。 |
以下是指定相關(guān)的屬性實(shí)例:
..... <attribute> <name>attribute_name</name> <required>false</required> <type>java.util.date</type> <fragment>false</fragment> </attribute> .....
如果你使用了兩個(gè)屬性,修改tld文件,如下所示:
..... <attribute> <name>attribute_name1</name> <required>false</required> <type>java.util.boolean</type> <fragment>false</fragment> </attribute> <attribute> <name>attribute_name2</name> <required>true</required> <type>java.util.date</type> </attribute> .....