📄 jsptags4.html
字号:
</h4><a name="wp89623"> </a><p class="pBody">The <code class="cCode">jsp:attribute</code> element allows you to define the value of a tag attribute in the <em class="cEmphasis">body</em> of an XML element instead of in the value of an XML attribute.</p><a name="wp89624"> </a><p class="pBody">For example, the Duke's Bookstore template page <code class="cCode">screendefinitions.jsp</code> uses <code class="cCode">jsp:attribute</code> to use the output of <code class="cCode">fmt:message</code> to set the value of the <code class="cCode">value</code> attribute of <code class="cCode">tt:parameter</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...<tt:screen id="/bookcatalog"> <tt:parameter name="title" direct="true"> <jsp:attribute name="value" > <fmt:message key="TitleBookCatalog"/> </jsp:attribute> </tt:parameter> <tt:parameter name="banner" value="/template/banner.jsp" direct="false"/> <tt:parameter name="body" value="/bookcatalog.jsp" direct="false"/></tt:screen>...<a name="wp89625"> </a></pre></div><a name="wp89626"> </a><p class="pBody"><code class="cCode">jsp:attribute</code> accepts a <code class="cCode">name</code> attribute and a <code class="cCode">trim</code> attribute. The <code class="cCode">name</code> attribute identifies which tag attribute is being specified. The optional <code class="cCode">trim</code> attribute determines whether whitespace appearing at the beginning and end of the element body should be discarded or not. By default, the leading and trailing whitespace is discarded. The whitespace is trimmed when the JSP page is translated. If a body contains a custom tag that produces leading or trailing whitespace, that whitespace is preserved regardless of the value of the <code class="cCode">trim</code> attribute.</p><a name="wp89627"> </a><p class="pBody">An empty body is equivalent to specifying "" as the value of the attribute. </p><a name="wp89628"> </a><p class="pBody">The body of <code class="cCode">jsp:attribute</code> is restricted according to the type of attribute being specified:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp89629"> </a><div class="pSmartList1"><li>For simple attributes that accept an EL expression, the body can be any JSP content.</li></div><a name="wp89630"> </a><div class="pSmartList1"><li>For simple attributes that do not accept an EL expression, the body can only contain template text.</li></div><a name="wp89631"> </a><div class="pSmartList1"><li>For fragment attributes, the body must not contain any scripting elements (See Chapter <a href="JSPAdvanced.html#wp65706">16</a>).</li></div></ul></div><a name="wp89636"> </a><h3 class="pHeading2">Tags with Bodies</h3><a name="wp89638"> </a><p class="pBody">A simple tag can contain custom and core tags, HTML text, and tag-dependent body content between the start and end tag.</p><a name="wp89640"> </a><p class="pBody">In the following example, the Duke's Bookstore application page <code class="cCode"><a href="../examples/web/bookstore3/web/bookshowcart.txt" target="_blank">bookshowcart.jsp</a></code> uses the JSTL <code class="cCode">c:if</code> tag to print the body if the request contains a parameter named <code class="cCode">Clear</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:if test="${param.Clear}"> <font color="#ff0000" size="+2"><strong> You just cleared your shopping cart! </strong><br>&nbsp;<br></font></c:if><a name="wp89641"> </a></pre></div><a name="wp89643"> </a><h4 class="pHeading3">jsp:body Element</h4><a name="wp89644"> </a><p class="pBody">You can also specify the body of a simple tag explicitly using the <code class="cCode">jsp:body</code> element. If one or more attributes are specified with the <code class="cCode">jsp:attribute</code> element, then <code class="cCode">jsp:body</code> is the only way to specify the body of the tag. If one or more <code class="cCode">jsp:attribute</code> elements appear in the body of a tag invocation but you don't include a <code class="cCode">jsp:body</code> element, the tag has an empty body.</p><a name="wp89646"> </a><h3 class="pHeading2">Tags That Define Variables</h3><a name="wp89648"> </a><p class="pBody">A simple tag can define an EL variable that can be used within the calling page. In the following example, the <code class="cCode">iterator</code> tag sets the value of the EL variable <code class="cCode">departmentName</code> as it iterates through a collection of department names.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><tlt:iterator var="departmentName" type="java.lang.String" group="${myorg.departmentNames}"> <tr> <td><a href="list.jsp?deptName=${departmentName}"> ${departmentName}</a></td> </tr></tlt:iterator><a name="wp89649"> </a></pre></div><a name="wp89650"> </a><h3 class="pHeading2">Communication Between Tags</h3><a name="wp89652"> </a><p class="pBody">Custom tags communicate with each other through shared objects. There are two types of shared objects: public and private.</p><a name="wp89653"> </a><p class="pBody">In the following example, the <code class="cCode">c:set</code> tag creates a public EL variable called <code class="cCode">aVariable</code>, which is then reused by <code class="cCode">anotherTag</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:set var="aVariable" value="aValue" /><tt:anotherTag attr1="${aVariable}" /><a name="wp89654"> </a></pre></div><a name="wp89655"> </a><p class="pBody">Nested tags can share private objects. In the next example, an object created by <code class="cCode">outerTag</code> is available to <code class="cCode">innerTag</code>. The inner tag retrieves its parent tag and then retrieves an object from the parent. Since the object is not named, the potential for naming conflicts is reduced.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><tt:outerTag> <tt:innerTag /></tt:outerTag><a name="wp89656"> </a></pre></div><a name="wp89658"> </a><p class="pBody">The Duke's Bookstore page <code class="cCode"><a href="../examples/web/bookstore3/web/template/template.txt" target="_blank">template.jsp</a></code> uses a set of cooperating tags that share public and private objects to define the screens of the application. These tags are described in <a href="JSPTags7.html#wp90689">A Template Tag Library</a>.</p> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <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"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -