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

如果要實(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)簽庫,隨后使用。
自定標(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頁面之間的通信協(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 {
// 給頁面輸出一個(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)簽庫描述文件 --> <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)簽庫描述文件的根元素
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)簽庫描述文件 -->
<tlib-version>1.0</tlib-version> 指定標(biāo)簽庫的版本(必須)
<short-name>jnb</short-name> 指定標(biāo)簽庫的簡稱(必須)
<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> 屬性的說明信息
<rtexprvalue>true</rtexprvalue> 屬性值的說明信息
</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>
以上就是本文的全部內(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)過程重點(diǎn)總結(jié)
- jsp自定義標(biāo)簽技術(shù)(實(shí)現(xiàn)原理與代碼以及平臺(tái)搭建步驟)
- JSP自定義標(biāo)簽獲取用戶IP地址的方法
- jsp簡單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符示例
- ThinkPHP模板自定義標(biāo)簽使用方法
- ThinkPHP分組下自定義標(biāo)簽庫實(shí)例
- JSP自定義標(biāo)簽rtexprvalue屬性用法實(shí)例分析
相關(guān)文章
javascript通過url向jsp頁面?zhèn)鬟f中文參數(shù)導(dǎo)致亂碼解決方案
javascript通過url向jsp頁面?zhèn)鬟f中文參數(shù)亂碼,一直都是從事jsp開發(fā)者比較煩心的是,于是本文介紹了一些解決方法,感興趣的朋友可以了解下2013-01-01
Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)
這篇文章主要為大家詳細(xì)介紹了Jsp+Servlet實(shí)現(xiàn)文件上傳下載功能的第二部分,文件列表展示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
基于jsp實(shí)現(xiàn)新聞管理系統(tǒng) 附完整源碼
這篇文章主要介紹了基于jsp的新聞管理系統(tǒng),具有一定的參考價(jià)值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

