JSP自定義標(biāo)簽基礎(chǔ)知識學(xué)習(xí)
在實際的開發(fā)中,如為了簡化JSP中出現(xiàn)大量的JSP腳本,那么我們需要使用標(biāo)準(zhǔn)標(biāo)簽庫和EL表達(dá)式,但是和新標(biāo)簽庫中提供的標(biāo)簽是有限的,不可能完全滿足開發(fā)的需要。如:分頁。因此需要學(xué)習(xí)如何自定義自己的標(biāo)簽庫。
如果要實現(xiàn)自定義標(biāo)簽,那么需要如下幾步:
編寫標(biāo)簽處理類
需要繼承或者實現(xiàn)相關(guān)的類或者接口
編寫標(biāo)簽描述文件
該文件是一個XML文件,而且必須放在網(wǎng)站的WEB-INF目錄中
在JSP中引入標(biāo)簽且使用
使用taglib指令引入標(biāo)簽庫,隨后使用。
自定標(biāo)簽的類體系
詳細(xì)了解下一下幾個類和接口:
---| JspTag接口
該接口是一個典型的標(biāo)記接口。主要標(biāo)記實現(xiàn)該接口的類可以處理標(biāo)簽。Seralizable
----| Tag接口
該接口主要描述的是標(biāo)簽處理類的共性,但是實現(xiàn)該接口的類不能處理標(biāo)簽體,該接口中定義了標(biāo)簽處理類和JSP頁面之間的通信協(xié)議。而且提供生命周期方法如:在標(biāo)簽開始和接結(jié)束的時候自動執(zhí)行的方法。
------| TagSupport類
主要負(fù)責(zé)處理標(biāo)簽的屬性。
-------| BodyTagSupport類
該類主要的是處理標(biāo)簽的標(biāo)簽體。
體驗
1. 處理類
public class HelloHanler implements Tag { private PageContext pageContext = null; // 標(biāo)簽結(jié)束的時候執(zhí)行 public int doEndTag() throws JspException { return 0; } // 標(biāo)簽開始的時候執(zhí)行 public int doStartTag() throws JspException { // 給頁面輸出一個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上下文對象 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ā)
自定義一個現(xiàn)實日期的標(biāo)簽。
1. 實現(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)簽開始的時候自動執(zhí)行 public int doStartTag() throws JspException { // 創(chuàng)建日期對象 Date date = new Date(); // 創(chuàng)建格式化對象 SimpleDateFormat format = new SimpleDateFormat(getPattern()); // 格式化 String str = format.format(date); // 獲取JSP上下文對象 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> 指定一個標(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"/>
實現(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)簽開始的時候自動執(zhí)行 public int doStartTag() throws JspException { // 創(chuàng)建日期對象 Date date = new Date(); // 創(chuàng)建格式化對象 SimpleDateFormat format = new SimpleDateFormat(getPattern()); // 格式化 String str = format.format(date); // 獲取JSP上下文對象 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)時間:</date:showdate2>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- 在asp.net(C#)中采用自定義標(biāo)簽和XML、XSL顯示數(shù)據(jù)
- jsp 自定義標(biāo)簽實例
- IE Firefox 使用自定義標(biāo)簽的區(qū)別
- JSP自定義標(biāo)簽Taglib實現(xiàn)過程重點總結(jié)
- jsp自定義標(biāo)簽技術(shù)(實現(xiàn)原理與代碼以及平臺搭建步驟)
- JSP自定義標(biāo)簽獲取用戶IP地址的方法
- jsp簡單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符示例
- ThinkPHP模板自定義標(biāo)簽使用方法
- ThinkPHP分組下自定義標(biāo)簽庫實例
- JSP自定義標(biāo)簽rtexprvalue屬性用法實例分析
相關(guān)文章
Spring MVC自定義日期類型轉(zhuǎn)換器實例詳解
這篇文章主要介紹了Spring MVC自定義日期類型轉(zhuǎn)換器實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04深入淺析Jsp中 out.print 和 out.write 的區(qū)別
本文簡明扼要的給大家介紹了jsp中 out.print 和 out.write 的區(qū)別,雖然本文簡短但是主要內(nèi)容給大家介紹清楚了,需要的朋友參考下吧2017-02-02web.xml中如何設(shè)置配置文件的加載路徑實例詳解
這篇文章主要介紹了web.xml中如何設(shè)置配置文件的加載路徑實例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06JDBC連接Oracle數(shù)據(jù)庫常見問題及解決方法
JDBC連接Oracle數(shù)據(jù)庫常見問題及解決方法...2006-12-12利用jsp+mysql實現(xiàn)好看的登錄與注冊頁面(動態(tài)背景)
最近在學(xué)jsp連接數(shù)據(jù)庫,存?zhèn)€檔吧,下面這篇文章主要給大家介紹了關(guān)于利用jsp+mysql實現(xiàn)好看的登錄與注冊頁面(動態(tài)背景)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01