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

📄 jspx3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
&lt;/c:forEach&gt;<a name="wp121924"> </a></pre></div><a name="wp121973"> </a><p class="pBody">This example will output 123.</p><a name="wp122002"> </a><p class="pBody">The <code class="cCode">jsp:text</code> tag must not contain any other elements. Therefore, if you need to nest a tag inside <code class="cCode">jsp:text</code>, you must wrap the tag inside CDATA.</p><a name="wp122036"> </a><p class="pBody">You also need to use CDATA if you need to output some elements that are not well-formed. The following example requires CDATA wrappers around the <code class="cCode">blockquote</code> start and end tags because the <code class="cCode">blockquote</code> element is not well-formed. This is because the <code class="cCode">blockquote</code> element overlaps with other elements in the example.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:forEach var=&quot;i&quot; begin=&quot;1&quot; end=&quot;${x}&quot;&gt;&nbsp;&nbsp;&lt;![CDATA[&lt;blockquote&gt;]]&gt;&lt;/c:forEach&gt;...&lt;c:forEach var=&quot;i&quot; begin=&quot;1&quot; end=&quot;${x}&quot;&gt;&nbsp;&nbsp;&lt;![CDATA[&lt;/blockquote&gt;]]&gt;&lt;/c:forEach&gt;<a name="wp121858"> </a></pre></div><a name="wp120172"> </a><p class="pBody">Just like JSP pages, JSP documents can generate dynamic template content with EL expressions, scripting elements, standard actions, and custom actions. The <code class="cCode">books.jspx</code> document uses EL expressions and custom actions to generate the XML book data.</p><a name="wp119602"> </a><p class="pBody">As shown in this snippet from <code class="cCode">books.jspx</code>, the <code class="cCode">c:forEach</code> JSTL tag iterates through the list of books and generates the XML data stream while the EL expressions access the JavaBeans component that in turn retrieves the data from the database:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:forEach xmlns:c=&quot;http://java.sun.com/jsp/jstl/core&quot;&nbsp;&nbsp;var=&quot;book&quot; begin=&quot;0&quot; items=&quot;${bookDB.books}&quot;&gt;&nbsp;&nbsp;&lt;book id=&quot;${book.bookId}&quot; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;surname&gt;${book.surname}&lt;/surname&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;firstname&gt;${book.firstName}&lt;/firstname&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;${book.title}&lt;/title&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;price&gt;${book.price}&lt;/price&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;year&gt;${book.year}&lt;/year&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;description&gt;${book.description}&lt;/description&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;inventory&gt;${book.inventory}&lt;/inventory&gt;&nbsp;&nbsp;&lt;/book&gt;&lt;/c:forEach&gt;<a name="wp119665"> </a></pre></div><a name="wp120206"> </a><p class="pBody">When using the expression language in your JSP documents, you will need to substitute some of the operators for alternative notation so that they will not be interpreted as XML markup. <a  href="JSPX3.html#wp120981">Table 13-3</a> enumerates the more common operators and their alternative syntax in JSP documents.</p><div align="left"><table border="1" summary="EL operators and JSP document-compliant alternative notation" id="wp120981">  <caption><a name="wp120981"> </a><div class="pTableTitle">Table 13-3   EL operators and JSP document-compliant alternative notation</div></caption>  <tr align="center">    <th><a name="wp120985"> </a><div class="pCellHeading">EL operator</div></th>    <th><a name="wp120987"> </a><div class="pCellHeading">JSP document notation</div></th></tr>  <tr align="left">    <td><a name="wp120989"> </a><div class="pCellBody">&lt;</div></td>    <td><a name="wp120991"> </a><div class="pCellBody">lt</div></td></tr>  <tr align="left">    <td><a name="wp120993"> </a><div class="pCellBody">&gt;</div></td>    <td><a name="wp120995"> </a><div class="pCellBody">gt</div></td></tr>  <tr align="left">    <td><a name="wp120997"> </a><div class="pCellBody">&lt;=</div></td>    <td><a name="wp120999"> </a><div class="pCellBody">le</div></td></tr>  <tr align="left">    <td><a name="wp121001"> </a><div class="pCellBody">&gt;=</div></td>    <td><a name="wp121003"> </a><div class="pCellBody">ge</div></td></tr>  <tr align="left">    <td><a name="wp121005"> </a><div class="pCellBody">!=</div></td>    <td><a name="wp121007"> </a><div class="pCellBody">ne</div></td></tr></table></div><p class="pBody"></p><a name="wp120941"> </a><p class="pBody">You can also use EL expressions with <code class="cCode">jsp:element</code> to generate tags dynamically rather than hardcoding them. This example could be used to generate an HTML header tag with a <code class="cCode">lang</code> attribute: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:element name=&quot;${content.headerName}&quot; &nbsp;&nbsp;&nbsp;&nbsp;xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot;&gt;&nbsp;&nbsp;&lt;jsp:attribute name=&quot;lang&quot;&gt;${content.lang}&lt;/jsp:attribute&gt;&nbsp;&nbsp;&lt;jsp:body&gt;${content.body}&lt;/jsp:body&gt;&lt;/jsp:element&gt;<a name="wp120207"> </a></pre></div><a name="wp119673"> </a><p class="pBody">The <code class="cCode">name</code> attribute identifies the generated tag's name. The <code class="cCode">jsp:attribute</code> generates the <code class="cCode">lang</code> attribute. The body of the <code class="cCode">jsp:attribute</code> tag identifies the value of the <code class="cCode">lang</code> attribute. The <code class="cCode">jsp:body</code> tag generates the body of the tag. The output of this example <code class="cCode">jsp:element</code> could be:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;h1 lang=&quot;fr&quot;&gt;Heading in French&lt;/h1&gt;<a name="wp120414"> </a></pre></div><a name="wp122280"> </a><p class="pBody">As shown in <a  href="JSPX3.html#wp117797">Table 13-2</a>, scripting elements (described in Chapter&nbsp;<a  href="JSPAdvanced.html#wp65706">16</a>) are represented as XML elements when they appear in a JSP document. The only exception is a scriplet expression used to specify a request-time attribute value. Instead of using <code class="cCode">&lt;%=expr %&gt;</code>, a JSP document uses <code class="cCode">%= expr %</code> to represent a request-time attribute value.</p><a name="wp122281"> </a><p class="pBody">The three scripting elements are: declarations, scriptlets, and expressions.</p><a name="wp119680"> </a><p class="pBody">A <code class="cCode">jsp:declaration</code> element declares a scripting language construct available to other scripting elements. A <code class="cCode">jsp:declaration</code> element has no attributes and its body is the declaration itself. Its syntax is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:declaration&gt; declaration goes here &lt;/jsp:declaration&gt;<a name="wp119685"> </a></pre></div><a name="wp119686"> </a><p class="pBody">A <code class="cCode">jsp:scriptlet</code> element contains a Java program fragment called a scriptlet. This element has no attributes and its body is the program fragment that comprises the scriptlet. Its syntax is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:scriptlet&gt; code fragment goes here &lt;/jsp:scriptlet&gt;<a name="wp119690"> </a></pre></div><a name="wp119691"> </a><p class="pBody">The <code class="cCode">jsp:expression</code> element inserts the value of a scripting language expression, converted into a string, into the data stream returned to the client. A <code class="cCode">jsp:expression</code> element has no attributes and its body is the expression. Its syntax is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:expression&gt; expression goes here &lt;/jsp:expression&gt;<a name="wp119678"> </a></pre></div><a name="wp119667"> </a><h3 class="pHeading2">Using the jsp:root Element</h3><a name="wp118502"> </a><p class="pBody">The <code class="cCode">jsp:root</code> element represents the root element of a JSP document. A <code class="cCode">jsp:root</code> element is not required for JSP documents. You can specify your own root element, enabling you to take any XML document and use it as a JSP document. The root element of the <code class="cCode">books.jspx</code> example JSP document is <code class="cCode">books</code>.</p><a name="wp118507"> </a><p class="pBody">Although the <code class="cCode">jsp:root</code> element is not required, it is still useful in these cases:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp118508"> </a><div class="pSmartList1"><li>You want to identify the document as a JSP document to the JSP container without having to add any configuration attributes to the deployment descriptor or name the document with a <code class="cCode">.jspx</code> extension</li></div><a name="wp118512"> </a><div class="pSmartList1"><li>You want to generate--from a single JSP document--more than one XML document or XML content mixed with non-XML content</li></div></ul></div><a name="wp118518"> </a><p class="pBody">The <code class="cCode">version</code> attribute is the only required attribute of the <code class="cCode">jsp:root</code> element. It specifies the JSP specification version that the JSP document is using.</p><a name="wp118528"> </a><p class="pBody">The <code class="cCode">jsp:root</code> element can also include <code class="cCode">xmlns</code> attributes for specifying tag libraries used by the other elements in the page.</p><a name="wp118531"> </a><p class="pBody">The <code class="cCode">books.jspx</code> page does not need a <code class="cCode">jsp:root</code> element, and therefore doesn't include one. However, suppose that you want to generate two XML documents from <code class="cCode">books.jspx</code>, one that lists books and the other that lists magazines, assuming magazines are in the database. This example is similar to the one in the section <a  href="JSPX3.html#wp119784">Including Directives in a JSP Document</a>. To do this, you can use this <code class="cCode">jsp:root</code> element:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:root &nbsp;&nbsp;xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot; version=&quot;2.0&quot; &gt;&nbsp;&nbsp;&lt;books&gt;...&lt;/books&gt;&nbsp;&nbsp;&lt;magazines&gt;...&lt;/magazines&gt;&lt;/jsp:root&gt;<a name="wp118532"> </a></pre></div><a name="wp118044"> </a><p class="pBody">Notice in this example that <code class="cCode">jsp:root</code> defines the JSP namespace because both the <code class="cCode">books</code> and <code class="cCode">magazines</code> elements use the elements defined in this namespace.</p><a name="wp118539"> </a><h3 class="pHeading2">Using the jsp:output Element</h3><a name="wp118047"> </a><p class="pBody">The <code class="cCode">jsp:output</code> element specifies the XML declaration or the document type declaration in the request output of the JSP document. For more information on the XML declaration see <a  href="IntroXML2.html#wp63920">The XML Prolog</a>. For more information on the document type declaration see <a  href="IntroXML4.html#wp68053">Referencing the DTD</a>.</p><a name="wp118250"> </a><p class="pBody">The XML declaration and document type declaration that are declared by the <code class="cCode">jsp:output</code> element are not interpreted by the JSP container. Instead, the container simply directs them to the request output. </p><a name="wp118242"> </a><p class="pBody">To illustrate this, here is an example of specifying a document type declaration with <code class="cCode">jsp:output</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:output &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doctype-root-element=&quot;books&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doctype-system=&quot;books.dtd&quot; /&gt;<a name="wp118052"> </a></pre></div><a name="wp118067"> </a><p class="pBody">The resulting output is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;!DOCTYPE books SYSTEM &quot;books.dtd&quot; &gt;<a name="wp121195"> </a></pre></div><a name="wp121196"> </a><p class="pBody">Specifying the document type declaration in the <code class="cCode">jsp:output</code> element will not cause the JSP container to validate the JSP document against the <code class="cCode">books.dtd</code>.</p><a name="wp118078"> </a><p class="pBody">If you want the JSP document to be validated against the DTD, you need to manually include the document type declaration within the JSP document, just as you would with any XML document.</p><a name="wp118176"> </a><p class="pBody">

⌨️ 快捷键说明

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