天天看點

Struts自定義标簽

一、基本概念

1、标簽(Tag)

标簽是一種XML元素,通過标簽可以使JSP網頁變得簡潔并且易于維護,還可以友善地實作同一個JSP檔案支援多種語言版本。由于标簽是XML元素,是以它的名稱和屬性都是大小寫敏感的。

2、标簽庫(Tag library)

由一系列功能相似、邏輯上互相聯系的标簽構成的集合稱為标簽庫。

3、标簽庫描述檔案(Tag Library Descriptor)

标簽庫描述檔案是一個XML檔案,這個檔案提供了标簽庫中類和JSP中對标簽引用的映射關系。它是一個配置檔案,和web.xml是類似的。

4、标簽處理類(Tag Handle Class)

标簽處理類是一個Java類,這個類繼承了TagSupport或者擴充了SimpleTag接

口,通過這個類可以實作自定義JSP标簽的具體功能。

二 标簽開發

1.簡單的标簽開發

  1.1 寫處理類

 1

Struts自定義标簽

 1package com.dongjj.tag;

 2

Struts自定義标簽

 3

Struts自定義标簽

 3import javax.servlet.jsp.JspException;

 4

Struts自定義标簽

 4import javax.servlet.jsp.PageContext;

 5

Struts自定義标簽

 5import javax.servlet.jsp.tagext.Tag;

 6

Struts自定義标簽

 7

Struts自定義标簽
Struts自定義标簽

 7public class TimeTag implements Tag 

Struts自定義标簽

{

 8

Struts自定義标簽

 8    protected PageContext pageContext;

 9

Struts自定義标簽

 9    private Tag parent;

10

Struts自定義标簽

11

Struts自定義标簽

11    // ----标簽開始時調用此方法-------

12

Struts自定義标簽
Struts自定義标簽

12    public int doStartTag() throws JspException 

Struts自定義标簽

13

Struts自定義标簽
Struts自定義标簽

13        try 

Struts自定義标簽

14

Struts自定義标簽

14            pageContext.getOut().println("Now:" + new java.util.Date());

15

Struts自定義标簽
Struts自定義标簽

15        } catch (Exception e) 

Struts自定義标簽

16

Struts自定義标簽

16            throw new JspException(e.getMessage());

17

Struts自定義标簽

17        }

18

Struts自定義标簽

18        return SKIP_BODY;

19

Struts自定義标簽

19    }

20

Struts自定義标簽

21

Struts自定義标簽

21    // ----标簽結束時調用此方法-------

22

Struts自定義标簽
Struts自定義标簽

22    public int doEndTag() throws JspException 

Struts自定義标簽

23

Struts自定義标簽

23        return EVAL_PAGE;

24

Struts自定義标簽

24    }

25

Struts自定義标簽

26

Struts自定義标簽

26    // 實作

27

Struts自定義标簽
Struts自定義标簽

27    public void release() 

Struts自定義标簽

28

Struts自定義标簽

28    }

29

Struts自定義标簽

30

Struts自定義标簽

30    // 實作

31

Struts自定義标簽
Struts自定義标簽

31    public void setPageContext(PageContext pageContext) 

Struts自定義标簽

32

Struts自定義标簽

32        this.pageContext = pageContext;

33

Struts自定義标簽

33    }

34

Struts自定義标簽

35

Struts自定義标簽

35    // 實作

36

Struts自定義标簽
Struts自定義标簽

36    public Tag getParent() 

Struts自定義标簽

37

Struts自定義标簽

37        return parent;

38

Struts自定義标簽

38    }

39

Struts自定義标簽

40

Struts自定義标簽

40    // 實作

41

Struts自定義标簽
Struts自定義标簽

41    public void setParent(Tag parent) 

Struts自定義标簽

42

Struts自定義标簽

42        this.parent = parent;

43

Struts自定義标簽

43    }

44

Struts自定義标簽

45

Struts自定義标簽

45}

1.2 編寫tld檔案 放在WEB-INF目錄下

Struts自定義标簽
Struts自定義标簽

Code

<?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.1</tlib-version>

  <!-- 标簽庫名稱-->

  <short-name>fn</short-name>

  <!-- 标簽庫URI-->

  <uri>

  <tag>    

  <!-- 标簽名稱-->

    <name>time</name>

    <!-- 标簽對應的處理類-->

    <tag-class>com.allanlxf.tag.TimeTag</tag-class>

    <!-- 标簽體内容,沒有标簽體則設為empty-->

    <body-content>empty</body-content>

  </tag>

</taglib>

   1.3 在在Web應用的web.xml檔案中聲明标簽庫引用 web.xml version="2.4"的不用引入也可,能在路徑下搜尋

1

Struts自定義标簽

<taglib>

2

Struts自定義标簽

 <taglib-uri>

3

Struts自定義标簽

  <taglib-location>/WEB-INF/mytags.tld</taglib-location>

4

Struts自定義标簽

  </taglib>

5

Struts自定義标簽

  <taglib-uri>對應tld檔案中的<uri>,<taglib-location>指出tld檔案的位置

1.4 寫jsp

Struts自定義标簽
Struts自定義标簽

<%

Struts自定義标簽

@taglib uri=prefix="mt"%>

Struts自定義标簽
Struts自定義标簽

<h1 align="center"><mt:time/></h1>

調用 在頁面上顯示如下

Now:Tue Mar 24 16:33:50 CST 2009