📄 jspintro9.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Using Custom Tags</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JSPIntro8.html" /> <link rel="Next" href="JSPIntro10.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JSPIntro8.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JSPIntro10.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp73314"> </a><h2 class="pHeading1">Using Custom Tags</h2><a name="wp73785"> </a><p class="pBody">Custom tags are user-defined JSP language elements that encapsulate recurring tasks. Custom tags are distributed in a <em class="cEmphasis">tag library</em>, which defines a set of related custom tags and contains the objects that implement the tags.</p><a name="wp73757"> </a><p class="pBody">Custom tags have the syntax </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><prefix:tag attr1="value" ... attrN="value" /> <a name="wp75162"> </a></pre></div><a name="wp76810"> </a><p class="pBody">or</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><prefix:tag attr1="value" ... attrN="value" > <code class="cVariable">body</code></prefix:tag><a name="wp76813"> </a></pre></div><a name="wp75163"> </a><p class="pBody">where <code class="cCode">prefix</code> distinguishes tags for a library, <code class="cCode">tag</code> is the tag identifier, and <code class="cCode">attr1 ... attrN</code> are attributes that modify the behavior of the tag.</p><a name="wp73788"> </a><p class="pBody">To use a custom tag in a JSP page, you must: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp73316"> </a><div class="pSmartList1"><li>Declare the tag library containing the tag </li></div><a name="wp73317"> </a><div class="pSmartList1"><li>Make the tag library implementation available to the Web application</li></div></ul></div><a name="wp73795"> </a><p class="pBody">See Chapter <a href="JSPIntro.html#wp65706">12</a> for detailed information on the different types of tags and how to implement tags.</p><a name="wp73319"> </a><h3 class="pHeading2">Declaring Tag Libraries</h3><a name="wp73321"> </a><p class="pBody">You declare that a JSP page will use tags defined in a tag library by including a <code class="cCode">taglib</code> directive in the page before any custom tag from that tag library is used. If you forget to include the <code class="cCode">taglib</code> directive for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag from that library as template data, and simply insert the text of the custom tag call into the response.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ taglib prefix="tt" [tagdir=/WEB-INF/tags/<code class="cVariable">dir</code> | uri=<code class="cVariable">URI</code> ] %><a name="wp73323"> </a></pre></div><a name="wp73648"> </a><p class="pBody">The <code class="cCode">prefix</code> attribute defines the prefix that distinguishes tags defined by a given tag library from those provided by other tag libraries.</p><a name="wp73567"> </a><p class="pBody">If the tag library is defined with tag files (see <a href="JSPTags5.html#wp89664">Encapsulating Reusable Content using Tag Files</a>), you supply the <code class="cCode">tagdir</code> attribute to identify the location of the files. The value of the attribute must start with <code class="cCode">/WEB-INF/tags/</code> and a translation error will occur if the value points to a directory that doesn't exist or if used in conjunction with the <code class="cCode">uri</code> attribute.</p><a name="wp73636"> </a><p class="pBody">The <code class="cCode">uri</code> attribute refers to a URI that uniquely identifies the tag library descriptor (TLD), a document that describes the tag library (See <a href="JSPTags6.html#wp90086">Tag Library Descriptors</a>).</p><a name="wp73332"> </a><p class="pBody">Tag library descriptor file names must have the extension <code class="cCode">.tld</code>. TLD files are stored in the <code class="cCode">WEB-INF</code> directory or subdirectory of the WAR file or in the <code class="cCode">META-INF/</code> directory or subdirectory of a tag library packaged in a JAR. You can reference a TLD directly or indirectly.</p><a name="wp73333"> </a><p class="pBody">The following <code class="cCode">taglib</code> directive directly references a TLD filename: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ taglib prefix="tlt" uri="/WEB-INF/iterator.tld"%><a name="wp73334"> </a></pre></div><a name="wp73335"> </a><p class="pBody">This <code class="cCode">taglib</code> directive uses a short logical name to indirectly reference the TLD:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ taglib prefix="tlt" uri="/tlt"%><a name="wp73336"> </a></pre></div><a name="wp82055"> </a><p class="pBody">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -