標簽庫描述文件(TLD)
采用了XML文件格式的標簽庫描述文件(TLD)定義了用戶(hù)的標簽庫, TLD文件中常用的元素有以下4個(gè)。
1.標簽庫元素<taglib>
<taglib>元素用來(lái)設置整個(gè)標簽庫的相關(guān)信息,如:
![]() |
2.標簽元素<tag>
<tag>元素用來(lái)定義一個(gè)標簽,它是<taglib>元素的子元素,如:
![]() |
3.標簽屬性元素<attribute>
<attribute>元素用來(lái)定義標簽的屬性,它是<tag>元素的子元素,如:
![]() |
4.標簽變量元素<variable>
<variable>元素用來(lái)定義標簽的變量,它是<tag>元素的子元素,如:
![]() |
自定義JSP標簽的一般步驟
在Java Web應用中自定義JSP標簽的一般經(jīng)過(guò)4個(gè)步驟。
(1)編寫(xiě)標簽處理類(lèi),如:
- public class Echo extends TagSupport {
- //標簽開(kāi)始時(shí)調用的處理方法
- public int doStartTag() throws JspException{
- try{
- //將信息內容輸出到JSP頁(yè)面
- pageContext.getOut().print(msg);
- }catch(Exception ex){
- throw new JspTagException(ex.getMessage());
- }
- //跳過(guò)標簽體的執行
- return SKIP_BODY;
- }
- //標簽結束時(shí)調用的處理方法
- public int doEndTag() {
- //繼續執行后續的JSP頁(yè)面內容
- return EVAL_PAGE;
- }
- }
(2)創(chuàng )建標簽庫描述符(TLD),如:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE taglib PUBLIC "-//Sun Microsystems,
- Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
- <taglib>
- <tlib-version>mytaglib 1.0</tlib-version>
- <jsp-version>jsp 2.0</jsp-version>
- <short-name>mytag</short-name>
- <uri>http://www.e868.net/mytag</uri>
- <tag>
- <name>echo</name>
- <tag-class>mytags.Echo</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>msg</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
(3)在JSP頁(yè)面中引用標簽庫,如:
- <%@ taglib prefix="mytag" uri="http://www.e868.net/mytag"%>
(4)在JSP頁(yè)面中使用標簽,如:
- <mytag:echo msg="劉斌,你好!"/>
- <mytag:echo msg="<%=(new Date()).toString()%>"/>
自定義JSP標簽的應用實(shí)例(1)
本實(shí)例中分別創(chuàng )建3個(gè)自定義JSP標簽:信息顯示標簽echo、格式化日期標簽formatdate與轉換大寫(xiě)標簽touppercase。
首先創(chuàng )建信息顯示標簽的處理類(lèi)Echo.java(見(jiàn)例程6-11) 、格式化日期標簽的處理類(lèi)FormatDate.java(見(jiàn)例程6-12)與轉化大寫(xiě)標簽的處理類(lèi)ToUpperCase.java(見(jiàn)例程6-13)。
然后創(chuàng )建標簽庫描述文件mytaglib.tld(見(jiàn)例程6-14),并在mytaglib.tld中分別描述信息顯示標簽echo、格式化日期標簽formatdate與轉換大寫(xiě)標簽touppercase。
最后創(chuàng )建一個(gè)使用自定義標簽的JSP測試頁(yè)面index.jsp(見(jiàn)例程6-15)。
本實(shí)例的視頻教程請參考配套光盤(pán)的"視頻教程"部分。
例程6-11 信息顯示標簽的處理類(lèi)Echo.java
- package mytags;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspTagException;
- import javax.servlet.jsp.tagext.TagSupport;
- public class Echo extends TagSupport {
- //定義一個(gè)屬性用于接收要顯示的信息
- String msg = "";
- //讀取msg的值
- public String getMsg() {
- return msg;
- }
- //設置msg的值
- public void setMsg(String msg) {
- this.msg = msg;
- }
- //標簽開(kāi)始時(shí)調用的處理方法
- public int doStartTag()throws JspException{
- try{
- //將信息內容輸出到JSP頁(yè)面
- pageContext.getOut().print(msg);
- }catch(Exception ex){
- throw new JspTagException(ex.getMessage());
- }
- //跳過(guò)標簽體的執行
- return SKIP_BODY;
- }
- //標簽結束時(shí)調用的處理方法
- public int doEndTag() {
- //繼續執行后續的JSP頁(yè)面內容
- return EVAL_PAGE;
- }
- }
例程6-12 格式化日期標簽的處理類(lèi)FormatDate.java
- package mytags;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspTagException;
- import javax.servlet.jsp.tagext.TagSupport;
- import java.util.*;
- import java.text.*;
- public class FormatDate extends TagSupport {
- //定義一個(gè)屬性用于接收傳入的日期
- Date date = null;
- //定義一個(gè)屬性用于接收傳入的類(lèi)型
- String type = null;
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- //標簽開(kāi)始時(shí)調用的處理方法
- public int doStartTag() throws JspException{
- try{
- SimpleDateFormat fmt = null;
- if (type.equals("date")){
- fmt = new SimpleDateFormat("yyyy年MM月dd日");
- }
- if (type.equals("time")){
- fmt = new SimpleDateFormat("hh時(shí)mm分ss秒");
- }
- if (type.equals("all")){
- fmt = new SimpleDateFormat("yyyy年MM月dd日 hh時(shí)mm分ss秒");
- }
- //將最后的結果輸出到頁(yè)面
- pageContext.getOut().print(fmt.format(date));
- }catch(Exception ex){
- throw new JspTagException(ex.getMessage());
- }
- //跳過(guò)標簽體的執行
- return SKIP_BODY;
- }
- //標簽結束時(shí)調用的處理方法
- public int doEndTag() {
- //繼續執行后續的JSP頁(yè)面內容
- return EVAL_PAGE;
- }
- }
自定義JSP標簽的應用實(shí)例(2)
例程6-13 轉化大寫(xiě)標簽的處理類(lèi)ToUpperCase.java
- package mytags;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.tagext.BodyContent;
- import javax.servlet.jsp.tagext.BodyTagSupport;
- public class ToUpperCase extends BodyTagSupport {
- //定義一個(gè)存放結果的變量
- String var = null;
- public String getVar() {
- return var;
- }
- public void setVar(String var) {
- this.var = var;
- }
- //標簽開(kāi)始時(shí)調用的處理方法
- public int doStartTag() throws JspException {
- //表示需要處理標簽體
- return EVAL_BODY_BUFFERED;
- }
- //判斷了標簽體內容之后調用的處理方法
- public int doAfterBody() throws JspException {
- //取得標簽體對象
- BodyContent body = getBodyContent();
- //取得標簽體的字符串內容
- String content = body.getString();
- //清除標簽體中的內容
- body.clearBody();
- //將內容轉換成大寫(xiě)
- content = content.toUpperCase();
- //在pageContext對象中保存變量
- pageContext.setAttribute(var,content);
- //結束對標簽體的處理
- return SKIP_BODY;
- }
- }
例程6-14 標簽庫描述文件mytaglib.tld
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE taglib PUBLIC "-//Sun Microsystems,
Inc.//DTD JSP Tag Library 1.2//EN"- "web-jsptaglibrary_1_2.dtd" >
- <taglib>
- <tlib-version>mytaglib 1.0</tlib-version>
- <jsp-version>jsp 2.0</jsp-version>
- <short-name>mytag</short-name>
- <uri>http://www.e868.net/mytag</uri>
- <tag>
- <name>echo</name>
- <tag-class>mytags.Echo</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>msg</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>formatdate</name>
- <tag-class>mytags.FormatDate</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>date</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>type</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>touppercase</name>
- <tag-class>mytags.ToUpperCase</tag-class>
- <body-content>tagdependent</body-content>
- <variable>
- <name-from-attribute>var</name-from-attribute>
- <variable-class>java.lang.String</variable-class>
- <scope>AT_BEGIN</scope>
- </variable>
- <attribute>
- <name>var</name>
- <required>true</required>
- </attribute>
- </tag>
- </taglib>
例程6-15 使用自定義標簽的JSP頁(yè)面index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
- <%@ taglib prefix="mytag" uri="http://www.e868.net/mytag"%>
- <html>
- <head><title>自定義JSP標簽應用實(shí)例</title></head>
- <body>
- <h2>自定義JSP標簽應用實(shí)例</h2><hr>
- <mytag:echo msg="劉斌,你好!"/><br>
- 現在的時(shí)間是:<mytag:echo msg="<%=(new Date()).toString()%>"/><br>
- 當前容器的版本為:<mytag:echo msg="<%=application.getServerInfo()%>"/><br>
- 當前日期是:<mytag:formatdate date="<%=new Date()%>" type="date"/><br>
- 當前時(shí)間是:<mytag:formatdate date="<%=new Date()%>" type="time"/><br>
- 當前日期和時(shí)間是:<mytag:formatdate date="<%=new Date()%>" type="all"/><br>
- <mytag:touppercase var="result">abcdefghijklmnopqrst</mytag:touppercase>
- 轉換結果:<%=result%>
- </body>
- </html>
運行效果如圖6-5所示。
(點(diǎn)擊查看大圖)圖6-5 index.jsp運行效果
聯(lián)系客服