一、自定義標簽入門之無參數自定義標簽
1.開發自定義標簽類
當我們在JSP頁面使用一個簡單的標簽時,底層實際上由標簽處理類提供支持,從而可以使用簡單的標簽來封裝復雜的功能,從而使團隊更好地協作開發(能讓美工人員更好地參與JSP頁面的開發)。
自定義標簽類都必須繼承一個父類:javax.servlet.jsp.tagext.SimpleTagSupport,或者TagSupport除此之外,JSP自定義標簽類還有如下要求。
如果標簽類包含屬性,每個屬性都有對應的getter和setter方法。
重寫doTag()或者doStartTag()或doEndTag()方法方法,這個方法負責生成頁面內容。
首先介紹是不帶屬性的標簽以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這兩個方法有什么區別
doStartTag是在掃描到起始標簽時調用,doEndTag是在掃描到結束標簽是調用。
例如:helloWorld> helloWorld/helloWorld>
則jsp引擎分析到helloWorld> 時調用doStratTag, 分析到/helloWorld>時調用doEndTag
2、建立TLD文件
TLD是Tag Library Definition的縮寫,即標簽庫定義,文件的后綴是tld,每個TLD文件對應一個標簽庫,一個標簽庫中可包含多個標簽,TLD文件也稱為標簽庫定義文件。
標簽庫定義文件的根元素是taglib,它可以包含多個tag子元素,每個tag子元素都定義一個標簽。通常我們可以到Web容器下復制一個標簽庫定義文件,并在此基礎上進行修改即可。例如Tomcat6.0,在webapps\examples\WEB-INF\jsp2路徑下包含了一個jsp2-example-taglib.tld文件,這就是示范用的標簽庫定義文件。
將該文件復制到Web應用的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的區別主要是標簽處理類是否需要與標簽體交互,如果不需要交互的就用TagSupport,否則就用BodyTagSupport。
交互就是標簽處理類是否要讀取標簽體的內容和改變標簽體返回的內容。用TagSupport實現的標簽,都可以用BodyTagSupport來實現,因為BodyTagSupport繼承了TagSupport而不去實現IterationTag接口的,因為BodyTagSupport繼承了TagSupport類,并且該類已經實現了IterationTag接口并且實現了功能.
doStartTag()方法在標簽開始時執行,要記住每次都要對類進行初始化,避免上一次的遺留數據對操作造成影響。然后判斷是否有數據需要處理,如果有,則返回EVAL_BODY_INCLUDE開始處理標簽里的內容,如果沒有,返回 EVAL_PAGE跳過標簽內容執行標簽下面的內容。
doAfterBody()方法在每次處理完標簽內部內容后執行,判斷循環是否已經結束,如果可以繼續循環,返回EVAL_BODY_AGAIN用循環得到新的數據再次處理標簽內部內容,如果循環結束就返回EVAL_PAGE結束標簽。
二、自定義JSP標簽的處理過程:
1.在JSP中引入標簽庫:
2.在JSP中使用標簽庫標簽
3.Web容器根據第二個步驟中的prefix,獲得第一個步驟中聲明的taglib的uri屬性值
4.Web容器根據uri屬性在web.xml找到對應的元素
5.從元素中獲得對應的元素的值
6.Web容器根據元素的值從WEB-INF/目錄下找到對應的.tld文件
7.從.tld文件中找到與tagname對應的元素
8.湊元素中獲得對應的元素的值
9.Web容器根據元素的值創建相應的tag handle class的實例
10. Web容器調用這個實例的doStartTag/doEndTag方法完成相應的處理
三、創建和使用一個Tag Library的基本步驟:
1.創建標簽的處理類(Tag Handler Class)
2.創建標簽庫描述文件(Tag Library Descrptor File)
3.在web.xml文件中配置元素
4.在JSP文件中引人標簽庫
四、TagSupport類簡介:
1.處理標簽的類必須擴展javax.servlet.jsp.TagSupport.
2.TagSupport類的主要屬性:
A.parent屬性:代表嵌套了當前標簽的上層標簽的處理類
B.pageContex屬性:代表Web應用中的javax.servlet.jsp.PageContext對象
3.JSP容器在調用doStartTag或者doEndTag方法前,會先調用setPageContext和setParent方法,設置pageContext和parent。因此在標簽處理類中可以直接訪問pageContext變量
4.在TagSupport的構造方法中不能訪問pageContext成員變量,因為此時JSP容器還沒有調用setPageContext方法對pageContext進行初始化
五、TagSupport處理標簽的方法:
1.TagSupport類提供了兩個處理標簽的方法:
public int doStartTag() throws JspException
public int doEndTag() throws JspException
2.doStartTag:但JSP容器遇到自定義標簽的起始標志,就會調用doStartTag()方法,doStartTag()方法返回一個整數值,用來決定程序的后續流程。
A.Tag.SKIP_BODY:表示跳過了開始和結束標簽之間的代碼
B.Tag.EVAL_BODY_INCLUDE:表示標簽之間的內容被正常執行
C.Tag.EVAL_BODY_BUFFERED :對包含的內容進行解析
3.doEndTag:但JSP容器遇到自定義標簽的結束標志,就會調用doEndTag()方法。doEndTag()方法也返回一個整數值,用來決定程序后續流程。
A.Tag.SKIP_PAGE:表示立刻停止執行網頁,網頁上未處理的靜態內容和JSP程序均被忽略任何已有的輸出內容立刻返回到客戶的瀏覽器上。
B.Tag.EVAL_PAGE:表示按照正常的流程繼續執行JSP網頁
4.doAfterTag:遇到標簽體執行
A.Tag.EVAL_BODY_AGAIN;// 如果集合中還有對像,則循環執行標簽體,對標簽體循環處理,(存在于javax.servlet.jsp.tagext.IterationTag接口中)
B.Tag.SKIP_BODY
六、創建含有字段的標簽:
1.創建標簽處理器類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語言或其他動態語言,默認false -->
/attribute>
attribute>
name>count/name>
rtexprvalue>true/rtexprvalue>
/attribute>
/tag>
3.jsp中定義標簽:
tm:field field="11" count="22"/>
七、如何創建標簽處理類
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對應的屬性值
5、對獲取的屬性進行相應的處理并輸出結果
創建標簽庫描述文件(Tag Library Descriptor)
1、標簽庫描述文件,簡稱TLD,采用XML文件格式,定義了用戶的標簽庫。TLD文件中的元素可以分成3類:
A.標簽庫元素
B.標簽元素
C.標簽屬性元素
2、標簽庫元素用來設定標簽庫的相關信息,它的常用屬性有:
A.shortname:指定Tag Library默認的前綴名(prefix);
B.uri:設定Tag Library的惟一訪問表示符。
3、標簽元素用來定義一個標簽,它的常見屬性有:
A.name:設定Tag的名字;
B.tagclass:設定Tag的處理類;
C.bodycontent:設定標簽的主體(body)內容。
1)empty:表示標簽中沒有body;
2)JSP:表示標簽的body中可以加入JSP程序代碼;
3)tagdependent:表示標簽中的內容由標簽自己去處理。
4、標簽屬性元素用來定義標簽的屬性,它的常見屬性有:
A.name:屬性名稱;
B.required:屬性是否必需的,默認為false;
C.rtexprvalue:屬性值是否可以為request-time表達式,也就是類似于 %=…% >的表達式。
八、在Web應用中使用標簽
1、如果Web應用中用到了自定義JSP標簽,則必須在web.xml文件中加入元素,它用于聲明所引用的標簽所在的標簽庫
/sometaglib
/WEB-INF/someTLD.tld
2、設定Tag Library的惟一標示符,在Web應用中將根據它來引用Tag Libray;
3、指定和Tag Library對應的TLD文件的位置;
4、在JSP文件中需要加入!-- taglib% >指令來聲明對標簽庫的引用。
5、prefix表示在JSP網頁中引用這個標簽庫的標簽時的前綴,uri用來指定Tag Library的標識符,它必須和web.xml中的屬性保持一致。
九、案例:
4.1.創建標簽描述符文件
在WEB-INF文件下創建*.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.創建標簽處理器
/**
* @desc 自定義標簽測試類 實現一個簡單的Hello World標簽
* @author 夏中偉
* @version eRedLab 2007-9-10
*/
public class TestTld extends TagSupport{
//標簽屬性begin
private String begin = null;
//標簽屬性end
private String end = null;
//構造函數
public TestTld(){
}
/* 標簽初始方法 */
public int doStartTag() throws JspTagException{
return super.EVAL_BODY_INCLUDE;
}
/* 標簽結束方法 */
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頁面輸出結果如下:
自定義標簽輸出流:Hello,夏中偉! Hi,World!
循環標簽體類: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 //遇到開始標簽執行
24 public int doStartTag() throws JspException
25 {
26 Collection coll = (Collection) pageContext.findAttribute(collection);
27 // 表示如果未找到指定集合,則不用處理標簽體,直接調用doEndTag()方法。
28 if(coll==null||coll.isEmpty()) return SKIP_BODY;
29
30 iter = coll.iterator();
31 pageContext.setAttribute(id, iter.next());
32 // 表示在現有的輸出流對象中處理標簽體,但繞過setBodyContent()和doInitBody()方法
33 // 這里一定要返回EVAL_BODY_INCLUDE,否則標簽體的內容不會在網頁上輸出顯示
34 return EVAL_BODY_INCLUDE;
35 }
36
37 //在doInitBody方法之前執行,在這里被繞過不執行
38 @Override
39 public void setBodyContent(BodyContent arg0)
40 {
41 System.out.println("setBodyContent");
42 super.setBodyContent(arg0);
43 }
44 //此方法被繞過不會被執行
45 @Override
46 public void doInitBody() throws JspException
47 {
48 System.out.println("doInitBody");
49 super.doInitBody();
50 }
51
52 //遇到標簽體執行
53 public int doAfterBody() throws JspException
54 {
55 if(iter.hasNext())
56 {
57 pageContext.setAttribute(id, iter.next());
58 return EVAL_BODY_AGAIN;// 如果集合中還有對像,則循環執行標簽體
59 }
60 return SKIP_BODY;//迭代完集合后,跳過標簽體,調用doEndTag()方法。
61 }
62
63 //遇到結束標簽執行
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 //構造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 }
45
46 public int doEndTag() throws JspException
47 {
48 return EVAL_PAGE;
49 }
50}
51
52表達式直接訪問此類中靜態的方法:ELFunction.java
53public 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"?>
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>自定義標簽/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 !-- 創建自定義 迭代標簽 -->
15 tag>
16 name>forEach/name>
17 tag-class>exercise.taglib.ForEach/tag-class>
18 !-- 如果沒有標簽體,設置empty , 如果有標簽休必須設置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>
31
32 !-- 創建自定義獲得屬性標簽 -->
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 !-- 配置一個表達式調用 的函數 -->
50 function>
51 name>add/name>!-- 配置一個標簽,在JSP頁面通過引用前綴調用 -->
52 function-class>exercise.taglib.ELFunction/function-class>!-- 實現類 -->
53 function-signature>int add(int,int)/function-signature>!-- 靜態的方法:包括返回類型,方法名,入參的類型 -->
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>表達式調用類的靜態方法:/h2>
2+5=${my:add(2,5)}
/body>
/html>
以上就是小編為大家帶來的jsp中自定義Taglib詳解全部內容了,希望大家多多支持腳本之家~