欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP自定義標簽基礎知識學習

 更新時間:2016年03月15日 16:41:34   作者:鄭宏鑫erke  
這篇文章主要為大家詳細介紹了JSP自定義標簽基礎知識,如何實現(xiàn)自定義標簽,請參考本文進行學習

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

如果要實現(xiàn)自定義標簽,那么需要如下幾步:

編寫標簽處理類
需要繼承或者實現(xiàn)相關的類或者接口

編寫標簽描述文件
該文件是一個XML文件,而且必須放在網(wǎng)站的WEB-INF目錄中

在JSP中引入標簽且使用
使用taglib指令引入標簽庫,隨后使用。

自定標簽的類體系

詳細了解下一下幾個類和接口:

---| JspTag接口

該接口是一個典型的標記接口。主要標記實現(xiàn)該接口的類可以處理標簽。Seralizable

----| Tag接口

該接口主要描述的是標簽處理類的共性,但是實現(xiàn)該接口的類不能處理標簽體,該接口中定義了標簽處理類和JSP頁面之間的通信協(xié)議。而且提供生命周期方法如:在標簽開始和接結(jié)束的時候自動執(zhí)行的方法。

------| TagSupport類

主要負責處理標簽的屬性。

-------| BodyTagSupport類

該類主要的是處理標簽的標簽體。

  體驗

1. 處理類

public class HelloHanler implements Tag {
  private PageContext pageContext = null;
  // 標簽結(jié)束的時候執(zhí)行
  public int doEndTag() throws JspException {
    return 0;
  }
  // 標簽開始的時候執(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;
  }
  // 獲取其父標簽
  public Tag getParent() {
    return null;
  }
  // 釋放
  public void release() {

  }
  // 設置jsp上下文對象
  public void setPageContext(PageContext pc) {
    this.pageContext = pc;
  }
  // 設置父標簽
  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. 編寫標簽庫描述文件 --> 
 <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進行自定義標簽開發(fā)

自定義一個現(xiàn)實日期的標簽。

1. 實現(xiàn)可以處理標簽屬性的標簽處理類

public class ShowDate extends TagSupport {
  // 為了便于獲取屬性,那么直接在處理類中定義和屬性同名的屬性變量即可且提供get和set方法
  private String pattern;
  public String getPattern() {
    return pattern;
  }
  public void setPattern(String pattern) {
    this.pattern = pattern;
  }

  // 標簽開始的時候自動執(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        標簽庫描述文件的根元素
  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. 編寫標簽庫描述文件 --> 
 <tlib-version>1.0</tlib-version>     指定標簽庫的版本(必須)
 <short-name>jnb</short-name>       指定標簽庫的簡稱(必須)
 <tag>                    指定一個標簽開始
  <name>showdate</name>           標簽名
  <tag-class>cn.itcast.custom.ShowDate</tag-class>   指定標簽處理類
  <body-content>empty</body-content>   指定標簽體,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)帶標簽體的自定義標簽

1. 標簽處理類

public class ShowDateByBody extends BodyTagSupport {
  // 為了便于獲取屬性,那么直接在處理類中定義和屬性同名的屬性變量即可且提供get和set方法
  private String pattern;
  public String getPattern() {
    return pattern;
  }
  public void setPattern(String pattern) {
    this.pattern = pattern;
  }

  // 標簽開始的時候自動執(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();
    // 獲取標簽提的內(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)容,希望對大家的學習有所幫助。

相關文章

最新評論