📄 jsptags4.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>Types of 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="JSPTags3.html" /> <link rel="Next" href="JSPTags5.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="JSPTags3.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="JSPTags5.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="wp89569"> </a><h2 class="pHeading1">Types of Tags</h2><a name="wp89570"> </a><p class="pBody">JSP simple tags are invoked using XML syntax. They have a start tag and end tag, and possibly a body:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><tt:tag> <code class="cVariable">body</code></tt:tag><a name="wp89571"> </a></pre></div><a name="wp89572"> </a><p class="pBody">A custom tag with no body is expressed as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><tt:tag /> or <tt:tag></tt:tag><a name="wp89573"> </a></pre></div><a name="wp89574"> </a><h3 class="pHeading2">Tags with Attributes</h3><a name="wp89575"> </a><p class="pBody">A simple tag can have attributes. Attributes customize the behavior of a custom tag just as parameters customize the behavior of a method.</p><a name="wp89576"> </a><p class="pBody">There are three types of attributes:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp89577"> </a><div class="pSmartList1"><li>Simple attributes</li></div><a name="wp89578"> </a><div class="pSmartList1"><li>Fragment attributes</li></div><a name="wp89579"> </a><div class="pSmartList1"><li>Dynamic attributes</li></div></ul></div><a name="wp89580"> </a><h4 class="pHeading3">Simple Attributes</h4><a name="wp89581"> </a><p class="pBody">Simple attributes are evaluated by the container prior to being passed to the tag handler. Simple attributes are listed in the start tag and have the syntax <code class="cCode">attr="value"</code>. You can set a simple attribute value from a <code class="cCode">String</code> constant, an EL expression, or with a <code class="cCode">jsp:attribute</code> element (see <a href="JSPTags4.html#wp89622">jsp:attribute Element</a>). The conversion process between the constants and expressions and attribute types follows the rules described for JavaBeans component properties in <a href="JSPIntro8.html#wp70763">Setting JavaBeans Component Properties</a>. </p><a name="wp89589"> </a><p class="pBody">The Duke's Bookstore page <code class="cCode"><a href="../examples/web/bookstore3/web/bookcatalog.txt" target="_blank">bookcatalog.jsp</a></code> calls the <code class="cCode">catalog </code>tag which has two attributes. The first attribute, a reference to a book database object, is set by an EL expression. The second attribute, which sets the color of the rows in a table that represents the bookstore catalog, is set with a <code class="cCode">String</code> constant.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><sc:catalog bookDB ="${bookDB}" color="#cccccc"><a name="wp89590"> </a></pre></div><a name="wp89591"> </a><h4 class="pHeading3">Fragment Attributes</h4><a name="wp89593"> </a><p class="pBody">A <em class="cEmphasis">JSP fragment</em> is a portion of JSP code passed to a tag handler that can be invoked as many times as needed. You can think of a fragment as a template that is used by a tag handler to produce customized content. Thus, unlike simple attributes which are evaluated by the container, fragment attributes are evaluated by tag handlers during tag invocation.</p><a name="wp89594"> </a><p class="pBody">You declare an attribute to be a fragment by using the <code class="cCode">fragment</code> attribute in a tag file <code class="cCode">attribute</code> directive (see <a href="JSPTags5.html#wp89854">Declaring Tag Attributes in Tag Files</a>) or by using the <code class="cCode">fragment</code> subelement of the <code class="cCode">attribute</code> TLD element (see <a href="JSPTags6.html#wp90370">Declaring Tag Attributes for Tag Handlers</a>). You define the value of fragment attribute with a <code class="cCode">jsp:attribute</code> element. When used to specify a fragment attribute, the body of the <code class="cCode">jsp:attribute</code> element can only contain template text and standard and custom tags; it <em class="cEmphasis">cannot</em> contain scripting elements (see Chapter <a href="JSPAdvanced.html#wp65706">16</a>). </p><a name="wp89604"> </a><p class="pBody">JSP fragments can be parametrized via expression language (EL) variables in the JSP code that composes the fragment. The EL variables are set by the tag handler, thus allowing the handler to customize the fragment each time it is invoked (see <a href="JSPTags5.html#wp89909">Declaring Tag Variables in Tag Files</a> and <a href="JSPTags6.html#wp90424">Declaring Tag Variables for Tag Handlers</a>). </p><a name="wp89611"> </a><p class="pBody">The <code class="cCode">catalog</code> tag discussed earlier accepts two fragments: <code class="cCode">normalPrice</code>, which is displayed for a product that's full price, and <code class="cCode">onSale</code>, which is displayed for a product that's on sale.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><sc:catalog bookDB ="${bookDB}" color="#cccccc"> <jsp:attribute name="normalPrice"> <fmt:formatNumber value="${price}" type="currency"/> </jsp:attribute> <jsp:attribute name="onSale"> <strike><fmt:formatNumber value="${price}" type="currency"/></strike><br/> <font color="red"><fmt:formatNumber value="${salePrice}" type="currency"/></font> </jsp:attribute></sc:catalog><a name="wp89612"> </a></pre></div><a name="wp89613"> </a><p class="pBody">The tag executes the <code class="cCode">normalPrice</code> fragment, using the values for the <code class="cCode">price</code> EL variable, if the product is full price. If the product is on sale, the tag executes the <code class="cCode">onSale</code> fragment, using the <code class="cCode">price</code> and <code class="cCode">salePrice</code> variables.</p><a name="wp89615"> </a><h4 class="pHeading3">Dynamic Attributes</h4><a name="wp89617"> </a><p class="pBody">A <em class="cEmphasis">dynamic attribute</em> is an attribute that is not specified in the definition of the tag. Dynamic attributes are primarily used by tags whose attributes are treated in a uniform manner, but whose names are not necessarily known at development time.</p><a name="wp89618"> </a><p class="pBody">For example, this tag accepts an arbitrary number of attributes whose values are colors and outputs a bulleted list of the attributes colored according to the values:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><colored:colored color1="red" color2="yellow" color3="blue"/><a name="wp89619"> </a></pre></div><a name="wp89620"> </a><p class="pBody">You can also set the value of dynamic attributes with an EL expression or using the <code class="cCode">jsp:attribute</code> element.</p><a name="wp89622"> </a><h4 class="pHeading3">jsp:attribute Element
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -