⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 com_mycompany_mytaghandler.htm

📁 这个压缩包里的都是超级经典的java例子
💻 HTM
字号:
<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">
<META CONTENT="no-cache" HTTP-EQUIV="Pragma">
<LINK href="/almanac.css" media="screen" type="text/css" rel="stylesheet">
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD></TD>
</TR>
</TABLE>
<br>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD></TD>
</TR>
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top">
<h1>The Java Developers Almanac 1.4</h1>
<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">
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD valign="top"><script type="text/javascript">
<!--
google_ad_client = "pub-6001183370374757";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel = "4777242811";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "6666CC";
google_color_url = "6666CC";
google_color_text = "000000";
//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD valign="top">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
    &gt;
    <A HREF="../index.html">List of Packages</A>
    &gt;

    
    <B><A HREF="../javax.servlet.jsp.tagext/pkg.html">javax.servlet.jsp.tagext</A></B><font color="#666666" class="xsmall-font">
        &nbsp;[1 examples]
    </font>
</DIV><P>
  <h3>e1070. 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 <font color="#0066ff"><i>com.mycompany</i></font>;
    
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    
    // This tag simply emits the current date and time.
    public class <font color="#0066ff"><i>MyTagHandler</i></font> 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>    &lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
    &lt;!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"&gt;
    &lt;taglib&gt;
      &lt;%-- Version of this tag library; it should have the form 123 or 123.456 --%&gt;
      &lt;tlib-version&gt;0.96&lt;/tlib-version&gt;
    
      &lt;%-- The required version of JSP for this tag library --%&gt;
      &lt;jsp-version&gt;1.2&lt;/jsp-version&gt;
    
      &lt;%-- The preferred prefix for this tag library --%&gt;
      &lt;short-name&gt;my&lt;/short-name&gt;
    
      &lt;%-- This is the URI that uniquely identifies this tag library.
           The URI does not have to exist; it is merely an identifier --%&gt;
      &lt;uri&gt;http://com.mycompany/taglib/mytaglib&lt;/uri&gt;
    
      &lt;%-- This name is used by tools to represent this tag library --%&gt;
      &lt;display-name&gt;My Tag Library&lt;/display-name&gt;
    
      &lt;description&gt;This is a description of the tag library.&lt;/description&gt;
    
      &lt;tag&gt;
        &lt;%-- Name of tag in this library --%&gt;
        &lt;name&gt;simple&lt;/name&gt;
        &lt;tag-class&gt;com.mycompany.MyTagHandler&lt;/tag-class&gt;
    
        &lt;%-- Specify that the tag must not have a body --%&gt;
        &lt;body-content&gt;empty&lt;/body-content&gt;
        &lt;description&gt;
            This is a description of the tag.
        &lt;/description&gt;
      &lt;/tag&gt;
    &lt;/taglib&gt;
</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>    &lt;%@ taglib uri="/WEB-INF/tld/mytaglib.tld" prefix="my" %&gt;
    
    &lt;my:simple/&gt;
</pre>

<br>
<FONT class="xsmall-font">
&copy; 2002 Addison-Wesley.
</FONT></TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD valign="top"><A href="http://compositesw.com/devzone?ref=javaalmanac"><IMG alt="Click Here" height="600" width="120" border="0" src="/csw_oad_120x600_final.gif"></A></TD>
</TR>
</TABLE>
</BODY>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META CONTENT="NO-CACHE" HTTP-EQUIV="PRAGMA">
</HEAD>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -