JSP自定義標(biāo)簽基礎(chǔ)知識(shí)學(xué)習(xí)
在實(shí)際的開發(fā)中,如為了簡(jiǎn)化JSP中出現(xiàn)大量的JSP腳本,那么我們需要使用標(biāo)準(zhǔn)標(biāo)簽庫(kù)和EL表達(dá)式,但是和新標(biāo)簽庫(kù)中提供的標(biāo)簽是有限的,不可能完全滿足開發(fā)的需要。如:分頁(yè)。因此需要學(xué)習(xí)如何自定義自己的標(biāo)簽庫(kù)。

如果要實(shí)現(xiàn)自定義標(biāo)簽,那么需要如下幾步:
編寫標(biāo)簽處理類
需要繼承或者實(shí)現(xiàn)相關(guān)的類或者接口
編寫標(biāo)簽描述文件
該文件是一個(gè)XML文件,而且必須放在網(wǎng)站的WEB-INF目錄中
在JSP中引入標(biāo)簽且使用
使用taglib指令引入標(biāo)簽庫(kù),隨后使用。
自定標(biāo)簽的類體系
詳細(xì)了解下一下幾個(gè)類和接口:
---| JspTag接口
該接口是一個(gè)典型的標(biāo)記接口。主要標(biāo)記實(shí)現(xiàn)該接口的類可以處理標(biāo)簽。Seralizable
----| Tag接口
該接口主要描述的是標(biāo)簽處理類的共性,但是實(shí)現(xiàn)該接口的類不能處理標(biāo)簽體,該接口中定義了標(biāo)簽處理類和JSP頁(yè)面之間的通信協(xié)議。而且提供生命周期方法如:在標(biāo)簽開始和接結(jié)束的時(shí)候自動(dòng)執(zhí)行的方法。
------| TagSupport類
主要負(fù)責(zé)處理標(biāo)簽的屬性。
-------| BodyTagSupport類
該類主要的是處理標(biāo)簽的標(biāo)簽體。

體驗(yàn)
1. 處理類
public class HelloHanler implements Tag {
private PageContext pageContext = null;
// 標(biāo)簽結(jié)束的時(shí)候執(zhí)行
public int doEndTag() throws JspException {
return 0;
}
// 標(biāo)簽開始的時(shí)候執(zhí)行
public int doStartTag() throws JspException {
// 給頁(yè)面輸出一個(gè)hello信息
JspWriter out = pageContext.getOut();
// 輸出信息
String info = "hello custom tag";
try {
out.write(info);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
// 獲取其父標(biāo)簽
public Tag getParent() {
return null;
}
// 釋放
public void release() {
}
// 設(shè)置jsp上下文對(duì)象
public void setPageContext(PageContext pc) {
this.pageContext = pc;
}
// 設(shè)置父標(biāo)簽
public void setParent(Tag t) {
}
}
2. 描述文件
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <!-- 2. 編寫標(biāo)簽庫(kù)描述文件 --> <tlib-version>1.0</tlib-version> <short-name>jnb</short-name> <tag> <name>hello</name> <tag-class>cn.itcast.test.HelloHanler</tag-class> <body-content>empty</body-content> </tag> </taglib>
3. 引入
<%@taglib uri="/WEB-INF/test.tld" prefix="jnb"%> <br/> <jnb:hello/>
JSP1.2進(jìn)行自定義標(biāo)簽開發(fā)
自定義一個(gè)現(xiàn)實(shí)日期的標(biāo)簽。
1. 實(shí)現(xiàn)可以處理標(biāo)簽屬性的標(biāo)簽處理類
public class ShowDate extends TagSupport {
// 為了便于獲取屬性,那么直接在處理類中定義和屬性同名的屬性變量即可且提供get和set方法
private String pattern;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
// 標(biāo)簽開始的時(shí)候自動(dòng)執(zhí)行
public int doStartTag() throws JspException {
// 創(chuàng)建日期對(duì)象
Date date = new Date();
// 創(chuàng)建格式化對(duì)象
SimpleDateFormat format = new SimpleDateFormat(getPattern());
// 格式化
String str = format.format(date);
// 獲取JSP上下文對(duì)象
PageContext pageContext = this.pageContext;
// 獲取JSP的OUT輸出流
JspWriter out = pageContext.getOut();
// 輸出
try {
out.write(str);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
2. 描述文件
<taglib 標(biāo)簽庫(kù)描述文件的根元素
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 2. 編寫標(biāo)簽庫(kù)描述文件 -->
<tlib-version>1.0</tlib-version> 指定標(biāo)簽庫(kù)的版本(必須)
<short-name>jnb</short-name> 指定標(biāo)簽庫(kù)的簡(jiǎn)稱(必須)
<tag> 指定一個(gè)標(biāo)簽開始
<name>showdate</name> 標(biāo)簽名
<tag-class>cn.itcast.custom.ShowDate</tag-class> 指定標(biāo)簽處理類
<body-content>empty</body-content> 指定標(biāo)簽體,JSP(有)empty(沒有)
<attribute> 描述屬性
<name>pattern</name> 屬性名
<required>true</required> 屬性的說(shuō)明信息
<rtexprvalue>true</rtexprvalue> 屬性值的說(shuō)明信息
</attribute>
</tag>
</taglib>
3. 引入和使用
<%@taglib uri="/WEB-INF/date.tld" prefix="date"%> <date:showdate pattern="yyyy年MM月dd日 a E"/>
實(shí)現(xiàn)帶標(biāo)簽體的自定義標(biāo)簽
1. 標(biāo)簽處理類
public class ShowDateByBody extends BodyTagSupport {
// 為了便于獲取屬性,那么直接在處理類中定義和屬性同名的屬性變量即可且提供get和set方法
private String pattern;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
// 標(biāo)簽開始的時(shí)候自動(dòng)執(zhí)行
public int doStartTag() throws JspException {
// 創(chuàng)建日期對(duì)象
Date date = new Date();
// 創(chuàng)建格式化對(duì)象
SimpleDateFormat format = new SimpleDateFormat(getPattern());
// 格式化
String str = format.format(date);
// 獲取JSP上下文對(duì)象
PageContext pageContext = this.pageContext;
// 獲取JSP的OUT輸出流
JspWriter out = pageContext.getOut();
// 獲取標(biāo)簽提的內(nèi)容
BodyContent body = this.getBodyContent();
String tag_body = body.getString();
str = "<font color='red'>"+tag_body+"</font>"+str;
// 輸出
try {
out.write(str);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
2. 描述文件
<tag>
<name>showdate2</name>
<tag-class>cn.itcast.custom.ShowDateByBody</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>pattern</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
3. 引入和使用
<date:showdate2 pattern="yyyy-MM-dd">系統(tǒng)時(shí)間:</date:showdate2>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- 在asp.net(C#)中采用自定義標(biāo)簽和XML、XSL顯示數(shù)據(jù)
- jsp 自定義標(biāo)簽實(shí)例
- IE Firefox 使用自定義標(biāo)簽的區(qū)別
- JSP自定義標(biāo)簽Taglib實(shí)現(xiàn)過(guò)程重點(diǎn)總結(jié)
- jsp自定義標(biāo)簽技術(shù)(實(shí)現(xiàn)原理與代碼以及平臺(tái)搭建步驟)
- JSP自定義標(biāo)簽獲取用戶IP地址的方法
- jsp簡(jiǎn)單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符示例
- ThinkPHP模板自定義標(biāo)簽使用方法
- ThinkPHP分組下自定義標(biāo)簽庫(kù)實(shí)例
- JSP自定義標(biāo)簽rtexprvalue屬性用法實(shí)例分析
相關(guān)文章
Spring MVC自定義日期類型轉(zhuǎn)換器實(shí)例詳解
這篇文章主要介紹了Spring MVC自定義日期類型轉(zhuǎn)換器實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
深入淺析Jsp中 out.print 和 out.write 的區(qū)別
本文簡(jiǎn)明扼要的給大家介紹了jsp中 out.print 和 out.write 的區(qū)別,雖然本文簡(jiǎn)短但是主要內(nèi)容給大家介紹清楚了,需要的朋友參考下吧2017-02-02
web.xml中如何設(shè)置配置文件的加載路徑實(shí)例詳解
這篇文章主要介紹了web.xml中如何設(shè)置配置文件的加載路徑實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
jsp頁(yè)面中顯示word/excel格式的文檔的方法
為了滿足用戶的個(gè)性化需求,在一些情況下需要個(gè)性設(shè)置.比如在jsp頁(yè)面輸出word格式的文檔,excel格式的文檔,都需要進(jìn)行特殊的設(shè)置2013-01-01
JDBC連接Oracle數(shù)據(jù)庫(kù)常見問題及解決方法
JDBC連接Oracle數(shù)據(jù)庫(kù)常見問題及解決方法...2006-12-12
利用jsp+mysql實(shí)現(xiàn)好看的登錄與注冊(cè)頁(yè)面(動(dòng)態(tài)背景)
最近在學(xué)jsp連接數(shù)據(jù)庫(kù),存?zhèn)€檔吧,下面這篇文章主要給大家介紹了關(guān)于利用jsp+mysql實(shí)現(xiàn)好看的登錄與注冊(cè)頁(yè)面(動(dòng)態(tài)背景)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

