国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp中自定義Taglib詳解

2024-09-05 00:22:35
字體:
供稿:網(wǎng)友

一、自定義標簽入門之無參數(shù)自定義標簽

1.開發(fā)自定義標簽類

當我們在JSP頁面使用一個簡單的標簽時,底層實際上由標簽處理類提供支持,從而可以使用簡單的標簽來封裝復雜的功能,從而使團隊更好地協(xié)作開發(fā)(能讓美工人員更好地參與JSP頁面的開發(fā))。

自定義標簽類都必須繼承一個父類:javax.servlet.jsp.tagext.SimpleTagSupport,或者TagSupport除此之外,JSP自定義標簽類還有如下要求。

如果標簽類包含屬性,每個屬性都有對應(yīng)的getter和setter方法。

重寫doTag()或者doStartTag()或doEndTag()方法方法,這個方法負責生成頁面內(nèi)容。

首先介紹是不帶屬性的標簽以HelloWorld為例:

Java代碼如下:

public class HelloWorldTag extends TagSupport {   private static final long serialVersionUID = -3382691015235241708L;  @Override  public int doEndTag() throws JspException {    try {      pageContext.getOut().write("Hello World !");      return super.doEndTag();    } catch (JspException e) {      e.printStackTrace();      return 0;    } catch (IOException e) {      e.printStackTrace();      return 0;    }  }   @Override  public int doStartTag() {    try {      pageContext.getOut().write("Hello World");      return super.doStartTag();    } catch (JspException e) {      e.printStackTrace();      return 0;    } catch (IOException e) {      e.printStackTrace();      return 0;    }  }}

注意:

問題1:tagsupport中的dostartTag和doEndTag這兩個方法有什么區(qū)別
doStartTag是在掃描到起始標簽時調(diào)用,doEndTag是在掃描到結(jié)束標簽是調(diào)用。
例如:<helloWorld> helloWorld</helloWorld>
則jsp引擎分析到<helloWorld> 時調(diào)用doStratTag, 分析到</helloWorld>時調(diào)用doEndTag

2、建立TLD文件

TLD是Tag Library Definition的縮寫,即標簽庫定義,文件的后綴是tld,每個TLD文件對應(yīng)一個標簽庫,一個標簽庫中可包含多個標簽,TLD文件也稱為標簽庫定義文件。

標簽庫定義文件的根元素是taglib,它可以包含多個tag子元素,每個tag子元素都定義一個標簽。通常我們可以到Web容器下復制一個標簽庫定義文件,并在此基礎(chǔ)上進行修改即可。例如Tomcat6.0,在webapps/examples/WEB-INF/jsp2路徑下包含了一個jsp2-example-taglib.tld文件,這就是示范用的標簽庫定義文件。

將該文件復制到Web應(yīng)用的WEB-INF/路徑,或WEB-INF的任意子路徑下,并對該文件進行簡單修改,修改后的helloworld.tld文件代碼如下:

<?xml version="1.0" encoding="UTF-8"?><taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"  version="2.0">  <tlib-version>1.0</tlib-version>  <short-name>myhelloworld</short-name>  <!-- 定義該標簽庫的URI 必須添加但可以空-->  <uri></uri>    <!-- 定義第一個標簽 -->    <tag>      <!-- 定義標簽名 -->      <name>helloWorld</name>      <!-- 定義標簽處理類 -->      <tag-class>org.lxh.taglib.HelloWorldTag</tag-class>      <!-- 定義標簽體為空 -->      <body-content>empty</body-content>    </tag></taglib>

問題1: 為什么要用TagSupport與BodyTagSupport的區(qū)別主要是標簽處理類是否需要與標簽體交互,如果不需要交互的就用TagSupport,否則就用BodyTagSupport。

交互就是標簽處理類是否要讀取標簽體的內(nèi)容和改變標簽體返回的內(nèi)容。用TagSupport實現(xiàn)的標簽,都可以用BodyTagSupport來實現(xiàn),因為BodyTagSupport繼承了TagSupport而不去實現(xiàn)IterationTag接口的,因為BodyTagSupport繼承了TagSupport類,并且該類已經(jīng)實現(xiàn)了IterationTag接口并且實現(xiàn)了功能.

doStartTag()方法在標簽開始時執(zhí)行,要記住每次都要對類進行初始化,避免上一次的遺留數(shù)據(jù)對操作造成影響。然后判斷是否有數(shù)據(jù)需要處理,如果有,則返回EVAL_BODY_INCLUDE開始處理標簽里的內(nèi)容,如果沒有,返回 EVAL_PAGE跳過標簽內(nèi)容執(zhí)行標簽下面的內(nèi)容。

doAfterBody()方法在每次處理完標簽內(nèi)部內(nèi)容后執(zhí)行,判斷循環(huán)是否已經(jīng)結(jié)束,如果可以繼續(xù)循環(huán),返回EVAL_BODY_AGAIN用循環(huán)得到新的數(shù)據(jù)再次處理標簽內(nèi)部內(nèi)容,如果循環(huán)結(jié)束就返回EVAL_PAGE結(jié)束標簽。

二、自定義JSP標簽的處理過程:

1.在JSP中引入標簽庫:  
2.在JSP中使用標簽庫標簽
3.Web容器根據(jù)第二個步驟中的prefix,獲得第一個步驟中聲明的taglib的uri屬性值
4.Web容器根據(jù)uri屬性在web.xml找到對應(yīng)的元素
5.從元素中獲得對應(yīng)的元素的值
6.Web容器根據(jù)元素的值從WEB-INF/目錄下找到對應(yīng)的.tld文件
7.從.tld文件中找到與tagname對應(yīng)的元素
8.湊元素中獲得對應(yīng)的元素的值
9.Web容器根據(jù)元素的值創(chuàng)建相應(yīng)的tag handle class的實例
10. Web容器調(diào)用這個實例的doStartTag/doEndTag方法完成相應(yīng)的處理

三、創(chuàng)建和使用一個Tag Library的基本步驟:

1.創(chuàng)建標簽的處理類(Tag Handler Class)
2.創(chuàng)建標簽庫描述文件(Tag Library Descrptor File)
3.在web.xml文件中配置元素
4.在JSP文件中引人標簽庫

四、TagSupport類簡介:

1.處理標簽的類必須擴展javax.servlet.jsp.TagSupport.

2.TagSupport類的主要屬性:

A.parent屬性:代表嵌套了當前標簽的上層標簽的處理類

B.pageContex屬性:代表Web應(yīng)用中的javax.servlet.jsp.PageContext對象

3.JSP容器在調(diào)用doStartTag或者doEndTag方法前,會先調(diào)用setPageContext和setParent方法,設(shè)置pageContext和parent。因此在標簽處理類中可以直接訪問pageContext變量

4.在TagSupport的構(gòu)造方法中不能訪問pageContext成員變量,因為此時JSP容器還沒有調(diào)用setPageContext方法對pageContext進行初始化

五、TagSupport處理標簽的方法:

1.TagSupport類提供了兩個處理標簽的方法:

public int doStartTag() throws JspException
public int doEndTag() throws JspException

2.doStartTag:但JSP容器遇到自定義標簽的起始標志,就會調(diào)用doStartTag()方法,doStartTag()方法返回一個整數(shù)值,用來決定程序的后續(xù)流程。

A.Tag.SKIP_BODY:表示跳過了開始和結(jié)束標簽之間的代碼
B.Tag.EVAL_BODY_INCLUDE:表示標簽之間的內(nèi)容被正常執(zhí)行
C.Tag.EVAL_BODY_BUFFERED :對包含的內(nèi)容進行解析

3.doEndTag:但JSP容器遇到自定義標簽的結(jié)束標志,就會調(diào)用doEndTag()方法。doEndTag()方法也返回一個整數(shù)值,用來決定程序后續(xù)流程。

A.Tag.SKIP_PAGE:表示立刻停止執(zhí)行網(wǎng)頁,網(wǎng)頁上未處理的靜態(tài)內(nèi)容和JSP程序均被忽略任何已有的輸出內(nèi)容立刻返回到客戶的瀏覽器上。
B.Tag.EVAL_PAGE:表示按照正常的流程繼續(xù)執(zhí)行JSP網(wǎng)頁

4.doAfterTag:遇到標簽體執(zhí)行

A.Tag.EVAL_BODY_AGAIN;// 如果集合中還有對像,則循環(huán)執(zhí)行標簽體,對標簽體循環(huán)處理,(存在于javax.servlet.jsp.tagext.IterationTag接口中)
B.Tag.SKIP_BODY

六、創(chuàng)建含有字段的標簽:

1.創(chuàng)建標簽處理器類FieldTag

package com.able.tag; import java.io.IOException; import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport; public class FieldTag extends TagSupport {   private static final long serialVersionUID = 1540529069962423355L;     private String field;     private Integer count;   @Override  public int doEndTag() throws JspException {    try {      JspWriter out = pageContext.getOut();      out.print(field);      out.print(count);    } catch (IOException e) {      e.printStackTrace();    }    return super.doEndTag();  }     public String getField() {    return field;  }   public void setField(String field) {    this.field = field;  }   public Integer getCount() {    return count;  }   public void setCount(Integer count) {    this.count = count;  }  }

2.在tag.tld文件中天劍tag標簽

<tag>  <!-- 定義標簽名 -->  <name>field</name>  <!-- 定義標簽處理類 -->  <tag-class>com.able.tag.FieldTag</tag-class>  <!-- 定義標簽體為空 -->  <body-content>empty</body-content>  <attribute>    <name>field</name>    <required>true</required> <!-- 是否必須賦值 -->    <rtexprvalue>true</rtexprvalue><!-- 表示是否接受jsp語法或者el語言或其他動態(tài)語言,默認false -->  </attribute>  <attribute>    <name>count</name>    <rtexprvalue>true</rtexprvalue>  </attribute></tag>

3.jsp中定義標簽:

<tm:field field="11" count="22"/>

七、如何創(chuàng)建標簽處理類

1、引入必需的資源

import javax.servlet.jsp.*; import javax.servlet.http.*; import java.util.*; import java.io.*;
2、繼承TagSupport類并覆蓋doStartTag()/doEndTag()方法

3、從ServletContext對象中獲取java.util.Properties對象

4、從Properties對象中獲取key對應(yīng)的屬性值

5、對獲取的屬性進行相應(yīng)的處理并輸出結(jié)果

創(chuàng)建標簽庫描述文件(Tag Library Descriptor)

1、標簽庫描述文件,簡稱TLD,采用XML文件格式,定義了用戶的標簽庫。TLD文件中的元素可以分成3類:

A.標簽庫元素
B.標簽元素
C.標簽屬性元素

2、標簽庫元素用來設(shè)定標簽庫的相關(guān)信息,它的常用屬性有:

A.shortname:指定Tag Library默認的前綴名(prefix);

B.uri:設(shè)定Tag Library的惟一訪問表示符。

3、標簽元素用來定義一個標簽,它的常見屬性有:

A.name:設(shè)定Tag的名字;

B.tagclass:設(shè)定Tag的處理類;

C.bodycontent:設(shè)定標簽的主體(body)內(nèi)容。

1)empty:表示標簽中沒有body;
2)JSP:表示標簽的body中可以加入JSP程序代碼;
3)tagdependent:表示標簽中的內(nèi)容由標簽自己去處理。

4、標簽屬性元素用來定義標簽的屬性,它的常見屬性有:

A.name:屬性名稱;
B.required:屬性是否必需的,默認為false;
C.rtexprvalue:屬性值是否可以為request-time表達式,也就是類似于< %=…% >的表達式。

八、在Web應(yīng)用中使用標簽

1、如果Web應(yīng)用中用到了自定義JSP標簽,則必須在web.xml文件中加入元素,它用于聲明所引用的標簽所在的標簽庫

/sometaglib
/WEB-INF/someTLD.tld

2、設(shè)定Tag Library的惟一標示符,在Web應(yīng)用中將根據(jù)它來引用Tag Libray;

3、指定和Tag Library對應(yīng)的TLD文件的位置;

4、在JSP文件中需要加入<!-- taglib% >指令來聲明對標簽庫的引用。

5、prefix表示在JSP網(wǎng)頁中引用這個標簽庫的標簽時的前綴,uri用來指定Tag Library的標識符,它必須和web.xml中的屬性保持一致。

九、案例:

4.1.創(chuàng)建標簽描述符文件

在WEB-INF文件下創(chuàng)建*.tld標簽描述符文件:如

<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>eRedLab JSPTag Library</shortname>
<uri>/testTag</uri>
<info>自定義標簽測試</info>
<tag>
 <name>hello</name>
 <tagclass>com.eredlab.taglib.test.TestTld</tagclass>
 <bodycontent>empty</bodycontent>
 <info>自定義標簽測試</info>
 <attribute>
 <name>begin</name>
 <required>true</required>
 </attribute>
 <attribute>
 <name>end</name>
 <required>true</required>
 </attribute>
</tag>
</taglib>

4.2.創(chuàng)建標簽處理器
/**
* @desc 自定義標簽測試類 實現(xiàn)一個簡單的Hello World標簽
* @author  夏中偉
* @version eRedLab 2007-9-10
*/
public class TestTld extends TagSupport{
//標簽屬性begin
private String begin = null;
//標簽屬性end
private String end = null;
//構(gòu)造函數(shù)
public TestTld(){

}

/* 標簽初始方法 */
public int doStartTag() throws JspTagException{
 return super.EVAL_BODY_INCLUDE;
}

/* 標簽結(jié)束方法 */
public int doEndTag() throws JspTagException{
 JspWriter out = pageContext.getOut();
 String sum = begin + end;
 try{
 //標簽的返回值
out.println(sum);
 }catch(IOException e){
 e.printStackTrace();
 }
 return super.SKIP_BODY;
}

/* 釋放資源 */
public void release(){
 super.release();
}
/********************************************
屬性get()、set()方法
*******************************************/
}

 

在Web.XML中加載標簽描述符文件.
<!-- 加載標簽描述符文件 -->
<taglib>
 <taglib-uri>/WEB-INF/test.tld</taglib-uri>
 <taglib-location>/WEB-INF/test.tld</taglib-location>
</taglib>

5.2.在JSP中使用此標簽
<%@ taglib uri="/testTag" prefix="mytag"%>
<mytag:hello end="夏中偉!" begin="自定義標簽輸出流:Hello,"/>
<mytag:hello end="World!" begin="Hi,"/>
WEB頁面輸出結(jié)果如下:
自定義標簽輸出流:Hello,夏中偉! Hi,World!

循環(huán)標簽體類:ForEach.java  1import java.util.Collection; 2import java.util.Iterator; 3 4import javax.servlet.jsp.JspException; 5import javax.servlet.jsp.tagext.BodyContent; 6import javax.servlet.jsp.tagext.BodyTagSupport; 7 8public class ForEach  extends BodyTagSupport 9{10  private String id;11  private String collection;12  private Iterator iter;13  14  public void setCollection(String collection)15  {16    this.collection = collection;17  }18  public void setId(String id)19  {20    this.id = id;21  }22  23  //遇到開始標簽執(zhí)行24  public int doStartTag() throws JspException25  {26    Collection coll = (Collection) pageContext.findAttribute(collection);27    // 表示如果未找到指定集合,則不用處理標簽體,直接調(diào)用doEndTag()方法。28    if(coll==null||coll.isEmpty()) return SKIP_BODY;29    30    iter = coll.iterator();31    pageContext.setAttribute(id, iter.next());32    // 表示在現(xiàn)有的輸出流對象中處理標簽體,但繞過setBodyContent()和doInitBody()方法33    // 這里一定要返回EVAL_BODY_INCLUDE,否則標簽體的內(nèi)容不會在網(wǎng)頁上輸出顯示34    return EVAL_BODY_INCLUDE;35  }36  37  //在doInitBody方法之前執(zhí)行,在這里被繞過不執(zhí)行38  @Override39  public void setBodyContent(BodyContent arg0)40  {41    System.out.println("setBodyContent");42    super.setBodyContent(arg0);43  }44  //此方法被繞過不會被執(zhí)行45  @Override46  public void doInitBody() throws JspException47  {48    System.out.println("doInitBody");49    super.doInitBody();50  }51  52  //遇到標簽體執(zhí)行53  public int doAfterBody() throws JspException54  {55    if(iter.hasNext()) 56    {57      pageContext.setAttribute(id, iter.next());58      return EVAL_BODY_AGAIN;// 如果集合中還有對像,則循環(huán)執(zhí)行標簽體59    }60    return SKIP_BODY;//迭代完集合后,跳過標簽體,調(diào)用doEndTag()方法。61  }62  63  //遇到結(jié)束標簽執(zhí)行64  public int doEndTag() throws JspException65  {66    System.out.println("doEndTag");67    return EVAL_PAGE;68  }6970}  獲取VO屬性類:GetProperty.java  1import java.lang.reflect.Method; 2 3import javax.servlet.jsp.JspException; 4import javax.servlet.jsp.tagext.BodyTagSupport; 5 6public class GetProperty extends BodyTagSupport 7{ 8 9  private String name;10  private String property;1112  public void setName(String name)13  {14    this.name = name;15  }1617  public void setProperty(String property)18  {19    this.property = property;20  }2122  @SuppressWarnings("unchecked")23  public int doStartTag() throws JspException24  {25    try26    {27      Object obj = pageContext.findAttribute(name);28      29      if (obj == null) return SKIP_BODY;30      31      Class c = obj.getClass();32      //構(gòu)造GET方法名字 get+屬性名(屬性名第一個字母大寫)33      String getMethodName = "get" + property.substring(0, 1).toUpperCase() 34                              + property.substring(1, property.length());35      Method getMethod = c.getMethod(getMethodName, new Class[]{});36      37      pageContext.getOut().print(getMethod.invoke(obj));38      System.out.print(property + ":" + getMethod.invoke(obj) + "t");39    } catch (Exception e)40    {41      e.printStackTrace();42    }43    return SKIP_BODY;44  }4546  public int doEndTag() throws JspException47  {48    return EVAL_PAGE;49  }50}5152表達式直接訪問此類中靜態(tài)的方法:ELFunction.java53public class ELFunction 54{55 public static int add( int i,int j )56 {57  return i+j;58 }59}   寫一個測試用的VO類:UserVo.java  1public class UserVo 2{ 3  private String name; 4  private String password; 5   6  public String getName() 7  { 8    return name; 9  }10  public void setName(String name)11  {12    this.name = name;13  }14  public String getPassword()15  {16    return password;17  }18  public void setPassword(String password)19  {20    this.password = password;21  }22}   建好TLD文件tag.tld,放在WEB-INF目錄下  1<?xml version="1.0" encoding="utf-8"?> 2<taglib version="2.0" 3 xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:shcemalocation="http://java.sun.com/xml/ns/j2ee  6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 7 8 <description>自定義標簽</description> 9 <display-name>JSTL core</display-name>10 <tlib-version>1.1</tlib-version>11 <short-name>firstLabel</short-name>12 <uri>http://java.sun.com/jsp/jstl/core</uri>1314 <!-- 創(chuàng)建自定義 迭代標簽 -->15 <tag>16  <name>forEach</name>17  <tag-class>exercise.taglib.ForEach</tag-class>18  <!-- 如果沒有標簽體,設(shè)置empty , 如果有標簽休必須設(shè)置JSP-->19  <body-content>JSP</body-content>20  <attribute>21   <name>id</name>22   <required>true</required><!-- 標識屬性是否是必須的 -->23   <rtexprvalue>true</rtexprvalue><!-- 標識屬性值是否可以用表達式語言 -->24  </attribute>25  <attribute>26   <name>collection</name>27   <required>true</required>28   <rtexprvalue>true</rtexprvalue>29  </attribute>30 </tag>3132 <!-- 創(chuàng)建自定義獲得屬性標簽 -->33 <tag>34  <name>getProperty</name>35  <tag-class>exercise.taglib.GetProperty</tag-class>36  <body-content>empty</body-content>37  <attribute>38   <name>name</name>39   <required>true</required>40   <rtexprvalue>true</rtexprvalue>41  </attribute>42  <attribute>43   <name>property</name>44   <required>true</required>45   <rtexprvalue>true</rtexprvalue>46  </attribute>47 </tag>4849 <!-- 配置一個表達式調(diào)用 的函數(shù) -->50    <function>51     <name>add</name><!-- 配置一個標簽,在JSP頁面通過引用前綴調(diào)用 -->52     <function-class>exercise.taglib.ELFunction</function-class><!-- 實現(xiàn)類 -->53     <function-signature>int add(int,int)</function-signature><!-- 靜態(tài)的方法:包括返回類型,方法名,入?yún)⒌念愋?-->54    </function>55</taglib>

在web.xml文件中配置自定義標簽

<jsp-config> <taglib> <taglib-uri>firstTag</taglib-uri> <taglib-location>/WEB-INF/tag.tld</taglib-location> </taglib></jsp-config> 在jsp文件中使用標簽:tag.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="firstTag" prefix="my"%><jsp:useBean id="userVo1" class="exercise.vo.UserVo" scope="request"> <jsp:setProperty name="userVo1" property="name" value="Hackiller"/> <jsp:setProperty name="userVo1" property="password" value="123"/></jsp:useBean><jsp:useBean id="userVo2" class="exercise.vo.UserVo" scope="request"> <jsp:setProperty name="userVo2" property="name" value="YangYang"/> <jsp:setProperty name="userVo2" property="password" value="456"/></jsp:useBean><% List list = new ArrayList(); list.add(userVo1); list.add(userVo2); pageContext.setAttribute("voList",list);%><html> <head> <title>My JSP 'tag.jsp' starting page</title> </head>  <body> <h2 align="center">This is my JSP page:測試taglib.</h2>  <hr>  <h2>自定義迭代標簽:</h2> <table> <tr><td>姓名</td><td>密碼</td></tr> <my:forEach collection="voList" id="uservo"> <tr> <td><my:getProperty name="uservo" property="name"/></td> <td><my:getProperty name="uservo" property="password"/></td> </tr> </my:forEach> </table> <hr>  <h2>表達式調(diào)用類的靜態(tài)方法:</h2> 2+5=${my:add(2,5)}  </body></html>

以上就是小編為大家?guī)淼膉sp中自定義Taglib詳解全部內(nèi)容了,希望大家多多支持VeVb武林網(wǎng)~


注:相關(guān)教程知識閱讀請移步到JSP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 侯马市| 萝北县| 新巴尔虎右旗| 崇礼县| 秦安县| 仁怀市| 高要市| 苗栗市| 南雄市| 东阳市| 富蕴县| 吉林市| 霞浦县| 平江县| 南华县| 中江县| 石阡县| 独山县| 荣成市| 右玉县| 天等县| 岢岚县| 新沂市| 冷水江市| 海伦市| 始兴县| 永福县| 邵东县| 神木县| 孟连| 陕西省| 行唐县| 客服| 隆德县| 晋江市| 清新县| 元氏县| 平远县| 平湖市| 洪洞县| 蓬安县|