濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > jsp中自定義Taglib詳解

jsp中自定義Taglib詳解

熱門標(biāo)簽:好操作的電話機(jī)器人廠家 南昌市地圖標(biāo)注app 泰州泰興400電話 怎么申請(qǐng) 南京新思維電話機(jī)器人 如何用中國(guó)地圖標(biāo)注數(shù)字點(diǎn) 地圖標(biāo)注市場(chǎng)怎么樣 百度地圖添加標(biāo)注圖標(biāo)樣式 聊城智能電銷機(jī)器人外呼 企業(yè)怎么在聯(lián)通申請(qǐng)400電話

一、自定義標(biāo)簽入門之無(wú)參數(shù)自定義標(biāo)簽

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

當(dāng)我們?cè)贘SP頁(yè)面使用一個(gè)簡(jiǎn)單的標(biāo)簽時(shí),底層實(shí)際上由標(biāo)簽處理類提供支持,從而可以使用簡(jiǎn)單的標(biāo)簽來(lái)封裝復(fù)雜的功能,從而使團(tuán)隊(duì)更好地協(xié)作開發(fā)(能讓美工人員更好地參與JSP頁(yè)面的開發(fā))。

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

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

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

首先介紹是不帶屬性的標(biāo)簽以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這兩個(gè)方法有什么區(qū)別
doStartTag是在掃描到起始標(biāo)簽時(shí)調(diào)用,doEndTag是在掃描到結(jié)束標(biāo)簽是調(diào)用。
例如:helloWorld> helloWorld/helloWorld>
則jsp引擎分析到helloWorld> 時(shí)調(diào)用doStratTag, 分析到/helloWorld>時(shí)調(diào)用doEndTag

2、建立TLD文件

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

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

將該文件復(fù)制到Web應(yīng)用的WEB-INF/路徑,或WEB-INF的任意子路徑下,并對(duì)該文件進(jìn)行簡(jiǎn)單修改,修改后的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>

  !-- 定義該標(biāo)簽庫(kù)的URI 必須添加但可以空-->

  uri>/uri>

    !-- 定義第一個(gè)標(biāo)簽 -->

    tag>

      !-- 定義標(biāo)簽名 -->

      name>helloWorld/name>

      !-- 定義標(biāo)簽處理類 -->

      tag-class>org.lxh.taglib.HelloWorldTag/tag-class>

      !-- 定義標(biāo)簽體為空 -->

      body-content>empty/body-content>

    /tag>

/taglib>

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

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

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

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

二、自定義JSP標(biāo)簽的處理過程:

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

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

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

四、TagSupport類簡(jiǎn)介:

1.處理標(biāo)簽的類必須擴(kuò)展javax.servlet.jsp.TagSupport.

2.TagSupport類的主要屬性:

A.parent屬性:代表嵌套了當(dāng)前標(biāo)簽的上層標(biāo)簽的處理類

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

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

4.在TagSupport的構(gòu)造方法中不能訪問pageContext成員變量,因?yàn)榇藭r(shí)JSP容器還沒有調(diào)用setPageContext方法對(duì)pageContext進(jìn)行初始化

五、TagSupport處理標(biāo)簽的方法:

1.TagSupport類提供了兩個(gè)處理標(biāo)簽的方法:

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

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

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

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

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

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

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

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

1.創(chuàng)建標(biāo)簽處理器類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標(biāo)簽

tag>

  !-- 定義標(biāo)簽名 -->

  name>field/name>

  !-- 定義標(biāo)簽處理類 -->

  tag-class>com.able.tag.FieldTag/tag-class>

  !-- 定義標(biāo)簽體為空 -->

  body-content>empty/body-content>

  attribute>

    name>field/name>

    required>true/required> !-- 是否必須賦值 -->

    rtexprvalue>true/rtexprvalue>!-- 表示是否接受jsp語(yǔ)法或者el語(yǔ)言或其他動(dòng)態(tài)語(yǔ)言,默認(rèn)false -->

  /attribute>

  attribute>

    name>count/name>

    rtexprvalue>true/rtexprvalue>

  /attribute>

/tag>

3.jsp中定義標(biāo)簽:

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

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

1、引入必需的資源

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

3、從ServletContext對(duì)象中獲取java.util.Properties對(duì)象

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

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

創(chuàng)建標(biāo)簽庫(kù)描述文件(Tag Library Descriptor)

1、標(biāo)簽庫(kù)描述文件,簡(jiǎn)稱TLD,采用XML文件格式,定義了用戶的標(biāo)簽庫(kù)。TLD文件中的元素可以分成3類:

A.標(biāo)簽庫(kù)元素
B.標(biāo)簽元素
C.標(biāo)簽屬性元素

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

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

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

3、標(biāo)簽元素用來(lái)定義一個(gè)標(biāo)簽,它的常見屬性有:

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

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

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

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

4、標(biāo)簽屬性元素用來(lái)定義標(biāo)簽的屬性,它的常見屬性有:

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

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

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

/sometaglib
/WEB-INF/someTLD.tld

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

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

4、在JSP文件中需要加入!-- taglib% >指令來(lái)聲明對(duì)標(biāo)簽庫(kù)的引用。

5、prefix表示在JSP網(wǎng)頁(yè)中引用這個(gè)標(biāo)簽庫(kù)的標(biāo)簽時(shí)的前綴,uri用來(lái)指定Tag Library的標(biāo)識(shí)符,它必須和web.xml中的屬性保持一致。

九、案例:

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

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

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

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

}

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

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

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

 

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

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

循環(huán)標(biāo)簽體類: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  //遇到開始標(biāo)簽執(zhí)行

24  public int doStartTag() throws JspException

25  {

26    Collection coll = (Collection) pageContext.findAttribute(collection);

27    // 表示如果未找到指定集合,則不用處理標(biāo)簽體,直接調(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)有的輸出流對(duì)象中處理標(biāo)簽體,但繞過setBodyContent()和doInitBody()方法

33    // 這里一定要返回EVAL_BODY_INCLUDE,否則標(biāo)簽體的內(nèi)容不會(huì)在網(wǎng)頁(yè)上輸出顯示

34    return EVAL_BODY_INCLUDE;

35  }

36  

37  //在doInitBody方法之前執(zhí)行,在這里被繞過不執(zhí)行

38  @Override

39  public void setBodyContent(BodyContent arg0)

40  {

41    System.out.println("setBodyContent");

42    super.setBodyContent(arg0);

43  }

44  //此方法被繞過不會(huì)被執(zhí)行

45  @Override

46  public void doInitBody() throws JspException

47  {

48    System.out.println("doInitBody");

49    super.doInitBody();

50  }

51  

52  //遇到標(biāo)簽體執(zhí)行

53  public int doAfterBody() throws JspException

54  {

55    if(iter.hasNext()) 

56    {

57      pageContext.setAttribute(id, iter.next());

58      return EVAL_BODY_AGAIN;// 如果集合中還有對(duì)像,則循環(huán)執(zhí)行標(biāo)簽體

59    }

60    return SKIP_BODY;//迭代完集合后,跳過標(biāo)簽體,調(diào)用doEndTag()方法。

61  }

62  

63  //遇到結(jié)束標(biāo)簽執(zhí)行

64  public int doEndTag() throws JspException

65  {

66    System.out.println("doEndTag");

67    return EVAL_PAGE;

68  }

69

70}

 

 獲取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;

11

12  public void setName(String name)

13  {

14    this.name = name;

15  }

16

17  public void setProperty(String property)

18  {

19    this.property = property;

20  }

21

22  @SuppressWarnings("unchecked")

23  public int doStartTag() throws JspException

24  {

25    try

26    {

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+屬性名(屬性名第一個(gè)字母大寫)

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  }

45

46  public int doEndTag() throws JspException

47  {

48    return EVAL_PAGE;

49  }

50}

51

52表達(dá)式直接訪問此類中靜態(tài)的方法:ELFunction.java

53public class ELFunction 

54{

55 public static int add( int i,int j )

56 {

57  return i+j;

58 }

59}

 

  寫一個(gè)測(cè)試用的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"?>

 2taglib 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>自定義標(biāo)簽/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>

13

14 !-- 創(chuàng)建自定義 迭代標(biāo)簽 -->

15 tag>

16  name>forEach/name>

17  tag-class>exercise.taglib.ForEach/tag-class>

18  !-- 如果沒有標(biāo)簽體,設(shè)置empty , 如果有標(biāo)簽休必須設(shè)置JSP-->

19  body-content>JSP/body-content>

20  attribute>

21   name>id/name>

22   required>true/required>!-- 標(biāo)識(shí)屬性是否是必須的 -->

23   rtexprvalue>true/rtexprvalue>!-- 標(biāo)識(shí)屬性值是否可以用表達(dá)式語(yǔ)言 -->

24  /attribute>

25  attribute>

26   name>collection/name>

27   required>true/required>

28   rtexprvalue>true/rtexprvalue>

29  /attribute>

30 /tag>

31

32 !-- 創(chuàng)建自定義獲得屬性標(biāo)簽 -->

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>

48

49 !-- 配置一個(gè)表達(dá)式調(diào)用 的函數(shù) -->

50    function>

51     name>add/name>!-- 配置一個(gè)標(biāo)簽,在JSP頁(yè)面通過引用前綴調(diào)用 -->

52     function-class>exercise.taglib.ELFunction/function-class>!-- 實(shí)現(xiàn)類 -->

53     function-signature>int add(int,int)/function-signature>!-- 靜態(tài)的方法:包括返回類型,方法名,入?yún)⒌念愋?-->

54    /function>

55/taglib>

在web.xml文件中配置自定義標(biāo)簽

jsp-config>

 taglib>

 taglib-uri>firstTag/taglib-uri>

 taglib-location>/WEB-INF/tag.tld/taglib-location>

 /taglib>

/jsp-config>

 
在jsp文件中使用標(biāo)簽: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:測(cè)試taglib./h2> 

 hr>

 

 h2>自定義迭代標(biāo)簽:/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>表達(dá)式調(diào)用類的靜態(tài)方法:/h2>

 2+5=${my:add(2,5)} 

 /body>

/html>

以上就是小編為大家?guī)?lái)的jsp中自定義Taglib詳解全部?jī)?nèi)容了,希望大家多多支持腳本之家~

您可能感興趣的文章:
  • JSP自定義標(biāo)簽Taglib實(shí)現(xiàn)過程重點(diǎn)總結(jié)

標(biāo)簽:白銀 自貢 臨汾 開封 山南 銅川 烏蘭察布 吉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp中自定義Taglib詳解》,本文關(guān)鍵詞  jsp,中,自定義,Taglib,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp中自定義Taglib詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于jsp中自定義Taglib詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    河北省| 中阳县| 手机| 涟源市| 景东| 富平县| 农安县| 徐汇区| 宁国市| 南京市| 日土县| 吐鲁番市| 澄迈县| 莫力| 吐鲁番市| 长顺县| 安阳县| 公安县| 塔河县| 石渠县| 江山市| 平度市| 永顺县| 西乡县| 乐昌市| 栾城县| 富平县| 五常市| 陵水| 蒙自县| 镶黄旗| 双城市| 尤溪县| 泗阳县| 郓城县| 晴隆县| 黄大仙区| 崇义县| 连平县| 呼图壁县| 武义县|