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

📄 jspadvanced8.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?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>Programming Tags That Accept Scripting Elements</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="JSPAdvanced7.html" />    <link rel="Next" href="WebI18N.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="JSPAdvanced7.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="WebI18N.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="wp74178"> </a><h2 class="pHeading1">Programming Tags That Accept Scripting Elements</h2><a name="wp73718"> </a><p class="pBody">Tag that accept scripting elements in attribute values or the body cannot be programmed as simple tags; they must be implemented as classic tags. The following sections describe the TLD elements and JSP tag extension API specific to classic tag handlers. All other TLD elements are the same as for simple tags.</p><a name="wp73798"> </a><h3 class="pHeading2">TLD Elements</h3><a name="wp73815"> </a><p class="pBody">You specify the character of a classic tag's body content using the <code class="cCode">body-content</code> element:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;body-content&gt;empty | JSP | tagdependent&lt;/body-content&gt;<a name="wp73816"> </a></pre></div><a name="wp73804"> </a><p class="pBody">You must declare the body content of tags that do not have a body as <code class="cCode">empty</code>. For tags that have a body, there are two options. Body content containing custom and core tags, scripting elements, and HTML text is categorized as <code class="cCode">JSP</code>. All other types of body content--for example, SQL statements passed to the query tag-- would be labeled <code class="cCode">tagdependent</code>. </p><a name="wp73802"> </a><h3 class="pHeading2">Tag Handlers</h3><a name="wp73696"> </a><p class="pBody">Classic tag handlers are written with the Java language and implement either the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/Tag.html" target="_blank">Tag</a></code>, <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/IterationTag.html" target="_blank">IterationTag</a></code>, or <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/BodyTag.html" target="_blank">BodyTag</a></code> interface. Interfaces can be used to take an existing Java object and make it a tag handler. For newly created handlers, you can use the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/TagSupport.html" target="_blank">TagSupport</a></code> and <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/BodyTagSupport.html" target="_blank">BodyTagSupport</a></code> classes as base classes. </p><a name="wp73727"> </a><p class="pBody">The classes and interfaces used to implement classic tag handlers are contained in the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/package-summary.html" target="_blank">javax.servlet.jsp.tagext</a></code> package. Classic tag handlers implement either the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/Tag.html" target="_blank">Tag</a></code>, <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/Tag.html" target="_blank">IterationTag</a></code>, or <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/BodyTag.html" target="_blank">BodyTag</a></code> interface. Interfaces can be used to take an existing Java object and make it a tag handler. For newly created classic tag handlers, you can use the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/TagSupport.html" target="_blank">TagSupport</a></code> and <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/BodyTagSupport.html" target="_blank">BodyTagSupport</a></code> classes as base classes. These classes and interfaces are contained in the <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/api/javax/servlet/jsp/tagext/package-summary.html" target="_blank">javax.servlet.jsp.tagext</a></code> package. </p><a name="wp73737"> </a><p class="pBody">Tag handler methods defined by the <code class="cCode">Tag</code> and <code class="cCode">BodyTag</code> interfaces are called by the JSP page's servlet at various points during the evaluation of the tag. When the start element of a custom tag is encountered, the JSP page's servlet calls methods to initialize the appropriate handler and then invokes the handler's <code class="cCode">doStartTag</code> method. When the end element of a custom tag is encountered, the handler's <code class="cCode">doEndTag</code> method is invoked for all but simple tags. Additional methods are invoked in between when a tag handler needs to manipulate the body of the tag. For further information, see <a  href="JSPAdvanced8.html#wp72582">Tags with Bodies</a>. In order to provide a tag handler implementation, you must implement the methods, summarized in <a  href="JSPAdvanced8.html#wp73752">Table 16-2</a>, that are invoked at various stages of processing the tag.</p><div align="left"><table border="1" summary="Tag Handler Methods" id="wp73752">  <caption><a name="wp73752"> </a><div class="pTableTitle">Table 16-2   Tag Handler Methods&nbsp;</div></caption>  <tr align="center">    <th><a name="wp73758"> </a><div class="pCellHeading">Tag Type</div></th>    <th><a name="wp73760"> </a><div class="pCellHeading">Interface</div></th>    <th><a name="wp73762"> </a><div class="pCellHeading">Methods</div></th></tr>  <tr align="left">    <td><a name="wp73764"> </a><div class="pCellBody">Basic</div></td>    <td><a name="wp73766"> </a><div class="pCellBody"><code class="cCode">Tag</code></div></td>    <td><a name="wp73768"> </a><div class="pCellBody"><code class="cCode">doStartTag, doEndTag</code></div></td></tr>  <tr align="left">    <td><a name="wp73770"> </a><div class="pCellBody">Attributes</div></td>    <td><a name="wp73772"> </a><div class="pCellBody"><code class="cCode">Tag</code></div></td>    <td><a name="wp73774"> </a><div class="pCellBody"><code class="cCode">doStartTag, doEndTag, setAttribute1,...,N, release</code></div></td></tr>  <tr align="left">    <td><a name="wp73776"> </a><div class="pCellBody">Body</div></td>    <td><a name="wp73778"> </a><div class="pCellBody"><code class="cCode">Tag</code></div></td>    <td><a name="wp73780"> </a><div class="pCellBody"><code class="cCode">doStartTag, doEndTag, release</code></div></td></tr>  <tr align="left">    <td><a name="wp73782"> </a><div class="pCellBody">Body, iterative evaluation</div></td>    <td><a name="wp73784"> </a><div class="pCellBody"><code class="cCode">IterationTag</code></div></td>    <td><a name="wp73786"> </a><div class="pCellBody"><code class="cCode">doStartTag, doAfterBody, doEndTag, release</code></div></td></tr>  <tr align="left">    <td><a name="wp73788"> </a><div class="pCellBody">Body, manipulation</div></td>    <td><a name="wp73790"> </a><div class="pCellBody"><code class="cCode">BodyTag</code></div></td>    <td><a name="wp73792"> </a><div class="pCellBody"><code class="cCode">doStartTag, doEndTag, release, doInitBody, doAfterBody</code></div></td></tr></table></div><p class="pBody"></p><a name="wp72375"> </a><p class="pBody">A tag handler has access to an API that allows it to communicate with the JSP page. The entry points to the API are two objects: the JSP context (<code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/JspContext.html" target="_blank">javax.servlet.jsp.JspContext)</a></code> for simple tag handlers and the page context (<code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/PageContext.html" target="_blank">javax.servlet.jsp.PageContext)</a></code> for classic tag handlers. <code class="cCode">JspContext</code> provides access to implicit objects. <code class="cCode">PageContext</code> extends <code class="cCode">JspContext</code> with HTTP-specific behavior. A tag handler can retrieve all the other implicit objects (request, session, and application) accessible from a JSP page through these objects. In addition, implicit objects can have named attributes associated with them. Such attributes are accessed using <code class="cCode">[set|get]Attribute</code> methods. </p><a name="wp72378"> </a><p class="pBody">If the tag is nested, a tag handler also has access to the handler (called the <em class="cEmphasis">parent</em>) associated with the enclosing tag.</p><a name="wp73344"> </a><h4 class="pHeading3">How Is a Classic Tag Handler Invoked?</h4><a name="wp73345"> </a><p class="pBody">The <code class="cCode">Tag</code> interface defines the basic protocol between a tag handler and a JSP page's servlet. It defines the life cycle and the methods to be invoked when the start and end tags are encountered. </p><a name="wp73347"> </a><p class="pBody">The JSP page's servlet invokes the <code class="cCode">setPageContext</code>, <code class="cCode">setParent</code>, and attribute setting methods before calling <code class="cCode">doStartTag</code>. The JSP page's servlet also guarantees that <code class="cCode">release</code> will be invoked on the tag handler before the end of the page. </p><a name="wp73348"> </a><p class="pBody">Here is a typical tag handler method invocation sequence: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">ATag t = new ATag();t.setPageContext(...);t.setParent(...);t.setAttribute1(value1);t.setAttribute2(value2);t.doStartTag();t.doEndTag();t.release();<a name="wp73349"> </a></pre></div><a name="wp73350"> </a><p class="pBody">The <code class="cCode">BodyTag</code> interface extends <code class="cCode">Tag</code> by defining additional methods that let a tag handler access its body. The interface provides three new methods: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp73351"> </a><div class="pSmartList1"><li><code class="cCode">setBodyContent</code>--Creates body content and adds to the tag handler</li></div><a name="wp73352"> </a><div class="pSmartList1"><li><code class="cCode">doInitBody</code>--Called before evaluation of the tag body </li></div><a name="wp73353"> </a><div class="pSmartList1"><li><code class="cCode">doAfterBody</code>--Called after evaluation of the tag body</li></div></ul></div><a name="wp73354"> </a><p class="pBody">A typical invocation sequence is: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">t.doStartTag();out = pageContext.pushBody();t.setBodyContent(out);// perform any initialization needed after body content is sett.doInitBody();t.doAfterBody();// while <code class="cCode">doAfterBody</code> returns <code class="cCode">EVAL_BODY_AGAIN</code> we // iterate body evaluation...t.doAfterBody();t.doEndTag();out = pageContext.popBody();t.release();<a name="wp73355"> </a></pre></div><a name="wp72582"> </a><h3 class="pHeading2">Tags with Bodies</h3><a name="wp72585"> </a><p class="pBody">A tag handler for a tag with a body is implemented differently depending on whether or not the tag handler needs to manipulate the body. A tag handler manipulates the body when it reads or modifies the contents of the body.</p><a name="wp73839"> </a><h4 class="pHeading3">Tag Handler Does Not Manipulate the Body</h4><a name="wp73205"> </a><p class="pBody">If the tag handler does not need to manipulate the body, the tag handler should implement the <code class="cCode">Tag</code> interface. If the tag handler implements the <code class="cCode">Tag</code> interface and the body of the tag needs to be evaluated, the <code class="cCode">doStartTag</code> method needs to return <code class="cCode">EVAL_BODY_INCLUDE</code>; otherwise it should return <code class="cCode">SKIP_BODY</code>. </p><a name="wp73206"> </a><p class="pBody">If a tag handler needs to iteratively evaluate the body, it should implement the <code class="cCode">IterationTag</code> interface. The tag handler should return <code class="cCode">EVAL_BODY_AGAIN</code> <code class="cCode">doAfterBody</code> method if it determines that the body needs to be evaluated again. </p><a name="wp72592"> </a><h4 class="pHeading3">Tag Handler Manipulates the Body</h4><a name="wp72593"> </a><p class="pBody">If the tag handler needs to manipulate the body, the tag handler must implement <code class="cCode">BodyTag</code> (or be derived from <code class="cCode">BodyTagSupport</code>).</p><a name="wp72596"> </a><p class="pBody">When a tag handler implements the <code class="cCode">BodyTag</code> interface, it must implement the <code class="cCode">doInitBody</code> and the <code class="cCode">doAfterBody</code> methods. These methods manipulate body content passed to the tag handler by the JSP page's servlet.</p><a name="wp72597"> </a><p class="pBody">Body content supports several methods to read and write its contents. A tag handler can use the body content's <code class="cCode">getString</code> or <code class="cCode">getReader</code> methods to extract information from the body, and the <code class="cCode">writeOut(out)</code> method to write the body contents to an out stream. The writer supplied to the <code class="cCode">writeOut</code> method is obtained using the tag handler's <code class="cCode">getPreviousOut</code> method. This method is used to ensure that a tag handler's results are available to an enclosing tag handler.

⌨️ 快捷键说明

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