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

📄 jsptags5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<?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>Encapsulating Reusable Content using Tag Files</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="JSPTags4.html" />    <link rel="Next" href="JSPTags6.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="JSPTags4.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="JSPTags6.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="wp89664"> </a><h2 class="pHeading1">Encapsulating Reusable Content using Tag Files</h2><a name="wp89666"> </a><p class="pBody">A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.</p><a name="wp89667"> </a><p class="pBody">The recommended file extension for a tag file is <code class="cCode">.tag</code>. As is the case with JSP files, the tag may be composed of a top file that includes other files that contain either a complete tag or a fragment of a tag file. Just as the recommended extension for a fragment of a JSP file is <code class="cCode">.jspf</code>, the recommended extension for a fragment of a tag file is <code class="cCode">.tagf</code>.</p><a name="wp89671"> </a><p class="pBody">The following version of the Hello, World application introduced in Chapter&nbsp;<a  href="WebApp.html#wp83291">3</a> uses a tag to generate the response. The <code class="cCode">response</code> tag, which accepts two attributes--a greeting string and a name--is encapsulated in <code class="cCode">response.tag</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ attribute name=&quot;greeting&quot; required=&quot;true&quot; %&gt;&lt;%@ attribute name=&quot;name&quot; required=&quot;true&quot; %&gt;&lt;h2&gt;&lt;font color=&quot;black&quot;&gt;${greeting}, ${name}!&lt;/font&gt;&lt;/h2&gt;<a name="wp89672"> </a></pre></div><a name="wp89673"> </a><p class="pBody">The highlighted line in <code class="cCode">greeting.jsp</code> page invokes the <code class="cCode">response</code> tag if the length of the <code class="cCode">username</code> request parameter is greater than <code class="cCode">0</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ taglib tagdir=&quot;/WEB-INF/tags&quot; prefix=&quot;h&quot; %&gt;&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; &nbsp;&nbsp;prefix=&quot;c&quot; %&gt;&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/functions&quot;&nbsp;&nbsp;prefix=&quot;fn&quot; %&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;/head&gt;&lt;body bgcolor=&quot;white&quot;&gt;&lt;img src=&quot;duke.waving.gif&quot;&gt; &lt;c:set var=&quot;greeting&quot; value=&quot;Hello&quot; /&gt; &lt;h2&gt;${greeting}, my name is Duke. What&#39;s yours?&lt;/h2&gt;&lt;form method=&quot;get&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;username&quot; size=&quot;25&quot;&gt;&lt;p&gt;&lt;/p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;&lt;input type=&quot;reset&quot; value=&quot;Reset&quot;&gt;&lt;/form&gt;&lt;c:if test=&quot;${fn:length(param.username) &gt; 0}&quot; &gt;<code class="cCodeBold">&nbsp;&nbsp;&lt;h:response greeting=&quot;${greeting}&quot;&nbsp;&nbsp;&nbsp;&nbsp;name=&quot;${param.username}&quot;/&gt;</code>&lt;/c:if&gt;&lt;/body&gt;&lt;/html&gt;<a name="wp89674"> </a></pre></div><a name="wp89675"> </a><p class="pBody">A sample <code class="cCode">hello3.war</code> is provided in <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/provided-wars/</code>. To build, package, deploy, and run the <code class="cCode">hello3</code> application:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp89676"> </a><div class="pSmartList1"><li>In a terminal window, go to<code class="cCode"> &lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/hello3/.</code></li></div><a name="wp89677"> </a><div class="pSmartList1"><li>Run <code class="cCode">asant</code> <code class="cCode">build</code>. This target will spawn any necessary compilations and copy files to the <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/hello3/build/</code> directory. </li></div><a name="wp89678"> </a><div class="pSmartList1"><li>Start the J2EE application server.</li></div><a name="wp92403"> </a><div class="pSmartList1"><li>Start <code class="cCode">deploytool</code>.</li></div><a name="wp92404"> </a><div class="pSmartList1"><li>Create a Web application called <code class="cCode">hello3</code> by running the New Web Component Wizard. Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Web Component.</li></div><a name="wp92405"> </a><div class="pSmartList1"><li>New Web Component Wizard</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp92406"> </a><div class="pSmartList2"><li>Select the Create New Stand-Alone WAR Module radio button.</li></div><a name="wp92407"> </a><div class="pSmartList2"><li>In the WAR Location field, enter <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/hello3/hello3.war</code>. </li></div><a name="wp92408"> </a><div class="pSmartList2"><li>In the WAR Name field enter <code class="cCode">hello3</code>.</li></div><a name="wp92409"> </a><div class="pSmartList2"><li>In the Context Root field, enter <code class="cCode">/hello3</code>.</li></div><a name="wp92410"> </a><div class="pSmartList2"><li>Click Edit to add the content files.</li></div><a name="wp92411"> </a><div class="pSmartList2"><li>In the Edit Contents dialog, navigate to<code class="cCode"> &lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/hello3/build/</code>.<code class="cCode"> </code>Select <code class="cCode">duke.waving.gif</code>, <code class="cCode">greeting.jsp</code>, and <code class="cCode">response.tag</code> and click Add. Click OK.</li></div><a name="wp92412"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp92413"> </a><div class="pSmartList2"><li>Select the JSP radio button. </li></div><a name="wp92414"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp92415"> </a><div class="pSmartList2"><li>Select <code class="cCode">greeting.jsp</code> from the Servlet Class combo box. </li></div><a name="wp92416"> </a><div class="pSmartList2"><li>Click Finish.</li></div></ol></div><a name="wp92420"> </a><div class="pSmartList1"><li>Set <code class="cCode">greeting.jsp</code> to be a welcome file (see <a  href="WebApp11.html#wp85892">Declaring Welcome Files</a>).</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp92421"> </a><div class="pSmartList2"><li>Select the File Refs tab. </li></div><a name="wp92422"> </a><div class="pSmartList2"><li>Click Add to add a welcome file.</li></div><a name="wp92423"> </a><div class="pSmartList2"><li>Select <code class="cCode">greeting.jsp</code> from the drop-down list.</li></div></ol></div><a name="wp92424"> </a><div class="pSmartList1"><li>Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Save.</li></div><a name="wp92425"> </a><div class="pSmartList1"><li>Deploy the application. </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp92426"> </a><div class="pSmartList2"><li>Select Tools<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Deploy. </li></div><a name="wp92427"> </a><div class="pSmartList2"><li>In the Connection Settings frame, enter the user name and password you specified when you installed the J2EE 1.4 Application Server.</li></div><a name="wp92428"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp92429"> </a><div class="pSmartList2"><li>A popup dialog will display the results of the deployment. Click Close.</li></div></ol></div><a name="wp89707"> </a><div class="pSmartList1"><li>Open your browser to http://localhost:<code class="cCode">8080</code>/hello3</li></div></ol></div><a name="wp89709"> </a><h3 class="pHeading2">Tag File Location</h3><a name="wp89710"> </a><p class="pBody">Tag files can be placed in one of two locations: in the <code class="cCode">/WEB-INF/tags/</code> directory or subdirectory of a Web application or in a JAR file (see <a  href="JSPTags6.html#wp90271">Packaged Tag Files</a>) in the <code class="cCode">/WEB-INF/lib/ </code>directory of a Web application. Packaged tag files require a <em class="cEmphasis">tag library descriptor</em> (see <a  href="JSPTags6.html#wp90086">Tag Library Descriptors</a>), an XML document that contains information about a library as a whole and about each tag contained in the library. Tag files that appear in any other location are not considered tag extensions and are ignored by the Web container.</p><a name="wp89718"> </a><h3 class="pHeading2">Tag File Directives</h3><a name="wp89719"> </a><p class="pBody">Directives are used to control aspects of tag file translation to a tag handler, specify aspects of the tag, attributes of the tag, and variables exposed by the tag. <a  href="JSPTags5.html#wp89730">Table 15-1</a> lists the directives that you can use in tag files.</p><div align="left"><table border="1" summary="Tag File Directives" id="wp89730">  <caption><a name="wp89730"> </a><div class="pTableTitle">Table 15-1   Tag File Directives&nbsp;</div></caption>  <tr align="center">    <th><a name="wp89734"> </a><div class="pCellHeading">Directive</div></th>    <th><a name="wp89736"> </a><div class="pCellHeading">Description</div></th></tr>  <tr align="left">    <td><a name="wp89738"> </a><div class="pCellBody"><code class="cCode">taglib</code></div></td>    <td><a name="wp89743"> </a><div class="pCellBody">Identical to <code class="cCode">taglib</code> directive (see <a  href="JSPIntro9.html#wp73319">Declaring Tag Libraries</a>) for JSP pages.</div></td></tr>  <tr align="left">    <td><a name="wp89745"> </a><div class="pCellBody"><code class="cCode">include</code></div></td>    <td><a name="wp89750"> </a><div class="pCellBody">Identical to <code class="cCode">include</code> directive (see <a  href="JSPIntro10.html#wp82812">Reusing Content in JSP Pages</a>) for JSP pages. Note that if the included file contains syntax unsuitable for tag files, a translation error will occur.</div></td></tr>  <tr align="left">    <td><a name="wp89752"> </a><div class="pCellBody"><code class="cCode">tag</code></div></td>    <td><a name="wp89754"> </a><div class="pCellBody">Similar to the <code class="cCode">page</code> directive in a JSP page, but applies to tag files instead of JSP pages. Like the <code class="cCode">page</code> directive, a translation unit can contain more than one instance of the <code class="cCode">tag</code> directive. All the attributes apply to the complete translation unit. However, there can be only one occurrence of any attribute/value defined by this directive in a given translation unit. With the exception of the <code class="cCode">import</code> attribute, multiple attribute/value (re)definitions result in a translation error. </div><a name="wp89755"> </a><div class="pCellBody">&#160;</div><a name="wp89756"> </a><div class="pCellBody">Also used for declaring custom tag properties such as display name. See <a  href="JSPTags5.html#wp89775">Declaring Tags</a>.</div></td></tr>  <tr align="left">    <td><a name="wp89761"> </a><div class="pCellBody"><code class="cCode">attribute</code></div></td>    <td><a name="wp89765"> </a><div class="pCellBody">Declares attributes of the custom tag defined in the tag file. See <a  href="JSPTags5.html#wp89848">body-content Attribute</a>.</div></td></tr>  <tr align="left">    <td><a name="wp89768"> </a><div class="pCellBody"><code class="cCode">variable</code></div></td>    <td><a name="wp89772"> </a><div class="pCellBody">Declares an EL variable exposed by the tag to the calling page. See <a  href="JSPTags5.html#wp89909">Declaring Tag Variables in Tag Files</a>.</div></td></tr></table></div><p class="pBody"></p><a name="wp89775"> </a><h4 class="pHeading3">Declaring Tags</h4><a name="wp89776"> </a><p class="pBody">The <code class="cCode">tag</code> directive is similar to the JSP page's <code class="cCode">page</code> directive, but applies to tag files. Some of the elements in the <code class="cCode">tag</code> directive appear in the <code class="cCode">tag</code> element of a TLD (see <a  href="JSPTags6.html#wp90277">Declaring Tag Handlers</a>). <a  href="JSPTags5.html#wp89790">Table 15-2</a> lists the <code class="cCode">tag</code> directive attributes.</p><div align="left"><table border="1" summary="tag Directive Attributes" id="wp89790">  <caption><a name="wp89790"> </a><div class="pTableTitle">Table 15-2   tag Directive Attributes&nbsp;</div></caption>  <tr align="center">    <th><a name="wp89794"> </a><div class="pCellHeading">Attribute</div></th>    <th><a name="wp89796"> </a><div class="pCellHeading">Description</div></th></tr>  <tr align="left">    <td><a name="wp89798"> </a><div class="pCellBody"><code class="cCode">display-name</code></div></td>    <td><a name="wp89800"> </a><div class="pCellBody">

⌨️ 快捷键说明

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