com_mycompany_mytaghandler.html
来自「java类库详细讲解」· HTML 代码 · 共 159 行
HTML
159 行
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>The Quintessential Custom Tag
(Java Developers Almanac Example)
</TITLE>
<META CONTENT="Patrick Chan" NAME="AUTHOR">
<META CONTENT="Code Examples from The Java Developers Almanac 1.4" NAME="DESCRIPTION">
<META CONTENT="Addison-Wesley/Patrick Chan" NAME="OWNER">
<META CONTENT="3/20/02" NAME="revision">
<STYLE TYPE="text/css">
<!-- BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
</STYLE>
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top"><font face="Times" size="6"><b>The Java Developers Almanac 1.4</b></font>
<br>
Order this book from <a href="/cgi-bin/scripts/redirect.pl?l=ex&url=http://www.amazon.com/exec/obidos/ASIN/0201752808/xeo">Amazon</a>.
</TD>
</TR>
<TR>
<TD align="right" valign="bottom">
<FORM method="get" action="/cgi-bin/search/find.pl">
<INPUT size="25" name="words" type="text"><INPUT value="Search" type="submit">
</FORM>
</TD>
</TR>
</TABLE>
<HR color="#6666cc">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
>
<A HREF="../index.html">List of Packages</A>
>
<B><A HREF="../javax.servlet.jsp.tagext/pkg.html">javax.servlet.jsp.tagext</A></B><font color="#666666" SIZE="-2">
[1 examples]
</font>
</DIV><P>
<h3>
e1069.
The Quintessential Custom Tag</h3>
A custom tag handler must implement the <code>Tag</code> interface. This
interface is implemented by many convenient support classes from which
a custom tag handler can extend. As a JSP page is executed and the
custom tag is encountered, the <code>doStartTag()</code> of the tag handler is
executed. If that method does not throw an exception, the tag
handler's <code>doEndTag()</code> method is then called.
<P> The JSP container may create many instances of the tag handler
but guarantees that an instance services only one request at a time.
A JSP container also typically reuses a tag handler instance and so
instance variables should be appropriately reset if necessary.
<P> This example implements a tag handler for the simplest type of
tag - one without attributes or a body. This custom tag simply prints
the current time and day.
<pre>
package com.mycompany;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
// This tag simply emits the current date and time.
public class MyTagHandler extends TagSupport {
public int doStartTag() throws JspException {
try {
// Print the current date and time
pageContext.getOut().print(<font color="#0066ff"><i>""+new java.util.Date()</i></font>);
} catch (java.io.IOException e) {
throw new JspTagException("MyTag: "+e.getMessage());
}
return SKIP_BODY;
}
public int doEndTag() {
return EVAL_PAGE;
}
}
</pre>
<P> A web application wishing to use the custom tag must not only
include the custom tag's class file but also its Tag Library
Descriptor (TLD) as well. The TLD is a file that contains information
about one or more custom tags and is needed by a JSP container to
process pages that use a custom tag. Here's the TLD file
(<code>mytaglib.tld</code>) for the custom tag:
<pre>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<%-- Version of this tag library; it should have the form 123 or 123.456 --%>
<tlib-version>0.96</tlib-version>
<%-- The required version of JSP for this tag library --%>
<jsp-version>1.2</jsp-version>
<%-- The preferred prefix for this tag library --%>
<short-name>my</short-name>
<%-- This is the URI that uniquely identifies this tag library.
The URI does not have to exist; it is merely an identifier --%>
<uri>http://mycompany.com/taglib/mytaglib</uri>
<%-- This name is used by tools to represent this tag library --%>
<display-name>My Tag Library</display-name>
<description>This is a description of the tag library.</description>
<tag>
<%-- Name of tag in this library --%>
<name>simple</name>
<tag-class>com.mycompany.MyTagHandler</tag-class>
<%-- Specify that the tag must not have a body --%>
<body-content>empty</body-content>
<description>
This is a description of the tag.
</description>
</tag>
</taglib>
</pre>
Here's an example of a JSP page that uses the custom tag. The TLD
file is assumed to be in <code>WEB-INF/tld</code> and the
<code>MyTagHandler.class</code> file in <code>WEB-INF/classes/com/mycompany</code>:
<pre>
<%@ taglib uri="/WEB-INF/tld/mytaglib.tld" prefix="my" %>
<my:simple/>
</pre>
<br>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="0">
© 2002 Addison-Wesley.
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?