亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JSP自定義標(biāo)簽基礎(chǔ)知識學(xué)習(xí)

 更新時間:2016年03月15日 16:41:34   作者:鄭宏鑫erke  
這篇文章主要為大家詳細(xì)介紹了JSP自定義標(biāo)簽基礎(chǔ)知識,如何實現(xiàn)自定義標(biāo)簽,請參考本文進(jìn)行學(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í)有所幫助。

相關(guān)文章

最新評論