📄 jspx3.html
字号:
<a href="JSPX3.html#wp118141">Table 13-4</a> shows all of the <code class="cCode">jsp:output</code> attributes. They are all optional, but some attributes depend on other attributes occurring in the same <code class="cCode">jsp:output</code> element, as explained in the table. The rest of this section explains more about using <code class="cCode">jsp:output</code> to generate an XML declaration and a document type declaration.</p><div align="left"><table border="1" summary="jsp:output attributes" id="wp118141"> <caption><a name="wp118141"> </a><div class="pTableTitle">Table 13-4 jsp:output attributes</div></caption> <tr align="center"> <th><a name="wp118145"> </a><div class="pCellHeading">attribute</div></th> <th><a name="wp118147"> </a><div class="pCellHeading">what it specifies</div></th></tr> <tr align="left"> <td><a name="wp118149"> </a><div class="pCellBody"><code class="cCode">omit-xml-declaration</code></div></td> <td><a name="wp118151"> </a><div class="pCellBody">A value of true or yes omits the XML declaration. A value of false or no generates an XML declaration</div></td></tr> <tr align="left"> <td><a name="wp118157"> </a><div class="pCellBody"><code class="cCode">doctype-root-element</code></div></td> <td><a name="wp118159"> </a><div class="pCellBody">Indicates the root element of the XML document in the DOCTYPE. Can only be specified if <code class="cCode">doctype-system</code> is specified</div></td></tr> <tr align="left"> <td><a name="wp118161"> </a><div class="pCellBody"><code class="cCode">doctype-system</code></div></td> <td><a name="wp118163"> </a><div class="pCellBody">Specifies that a DOCTYPE is generated in output and gives the SYSTEM literal</div></td></tr> <tr align="left"> <td><a name="wp118165"> </a><div class="pCellBody"><code class="cCode">doctype-public</code></div></td> <td><a name="wp118167"> </a><div class="pCellBody">Specifies the value for the Public ID of the generated DOCTYPE. Can only be specified if <code class="cCode">doctype-system</code> is specified</div></td></tr></table></div><p class="pBody"></p><a name="wp118286"> </a><h4 class="pHeading3">Generating XML Declarations</h4><a name="wp118325"> </a><p class="pBody">Here is an example of an XML declaration:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version="1.0" encoding="UTF-8" ?><a name="wp118288"> </a></pre></div><a name="wp118279"> </a><p class="pBody">This declaration is the default XML declaration, meaning that--if the JSP container is generating an XML declaration--this is what the JSP container will include in the output of your JSP document.</p><a name="wp119336"> </a><p class="pBody">Neither a JSP document nor its request output is required to have an XML declaration. In fact, if the JSP document is not producing XML output then it shouldn't have an XML declaration.</p><a name="wp118328"> </a><p class="pBody">The JSP container will <span style="font-style: italic">not</span> include the XML declaration in the output when:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp118332"> </a><div class="pSmartList1"><li>You set the <code class="cCode">omit-xml-declaration</code> attribute of the <code class="cCode">jsp:output</code> element to either "true" or "yes", or</li></div><a name="wp118333"> </a><div class="pSmartList1"><li>You have a <code class="cCode">jsp:root</code> element in your JSP document, and you do not specify <code class="cCode">omit-xml-declaration="false"</code> in <code class="cCode">jsp:output</code>.</li></div></ul></div><a name="wp118348"> </a><p class="pBody">The JSP container will include the XML declaration in the output when:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp118352"> </a><div class="pSmartList1"><li>You set the <code class="cCode">omit-xml-declaration</code> attribute of the <code class="cCode">jsp:output</code> element to either "false" or "no", or</li></div><a name="wp118353"> </a><div class="pSmartList1"><li>You do not have a <code class="cCode">jsp:root</code> action in your JSP document, and you do not specify the <code class="cCode">omit-xml-declaration</code> attribute in <code class="cCode">jsp:output</code>.</li></div></ul></div><a name="wp118361"> </a><p class="pBody">The <code class="cCode">books.jspx</code> JSP document does not include a <code class="cCode">jsp:root</code> action or a <code class="cCode">jsp:output</code>. Therefore, the default XML declaration is generated in the output.</p><a name="wp118365"> </a><h4 class="pHeading3">Generating a Document Type Declaration</h4><a name="wp118366"> </a><p class="pBody">The document type declaration specifies a DTD, which defines the structural rules for the XML document in which the document type declaration occurs. XML documents are not required to have a DTD associated with them. In fact, the <code class="cCode">books</code> example does not include one. </p><a name="wp119339"> </a><p class="pBody">This section shows you how to add a document type declaration to the XML output of <code class="cCode">books.jspx</code> using the <code class="cCode">jsp:output</code> element. It also shows you how to enter the document type declaration manually into <code class="cCode">books.jspx</code> so that the JSP container will interpret it and validate the document against the DTD.</p><a name="wp118376"> </a><p class="pBody">As shown in <a href="JSPX3.html#wp118141">Table 13-4</a>, the <code class="cCode">jsp:output</code> element has three attributes that you use to generate the document type declaration:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp118409"> </a><div class="pSmartList1"><li><code class="cCode">doctype-root-element</code>: Indicates the root element of the XML document</li></div><a name="wp118410"> </a><div class="pSmartList1"><li><code class="cCode">doctype-system</code>: Indicates the URI reference to the DTD</li></div><a name="wp120844"> </a><div class="pSmartList1"><li><code class="cCode">doctype-public</code>: A more flexible way to reference the DTD. This identifier gives more information about the DTD without giving a specific location. A public identifier resolves to the same actual document on any system even though the location of that document on each system may vary. See the<a href="http://www.w3.org/XML/" target="_blank"> XML 1.0 specification</a> for more information.</li></div></ul></div><a name="wp120845"> </a><p class="pBody">The rules for using the attributes are:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp118388"> </a><div class="pSmartList1"><li>The doctype attributes can appear in any order</li></div><a name="wp118389"> </a><div class="pSmartList1"><li>The <code class="cCode">doctype-root</code> attribute must be specified if the <code class="cCode">doctype-system</code> attribute is specified</li></div><a name="wp118390"> </a><div class="pSmartList1"><li>The <code class="cCode">doctype-public</code> attribute must not be specified unless <code class="cCode">doctype-system</code> is specified</li></div></ul></div><a name="wp118375"> </a><p class="pBody">This syntax notation summarizes these rules:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><jsp:output (omit-xml-declaration="yes"|"no"|"true"|"false")(doctypeDecl) /><a name="wp118407"> </a>doctypeDecl:= (doctype-root-element="rootElement" doctype-public="PublicLiteral" doctype-system="SystemLiteral") | (doctype-root-element="rootElement" doctype-system="SystemLiteral")<a name="wp118408"> </a></pre></div><a name="wp118417"> </a><p class="pBody">Suppose that you wanted to reference a DTD, called <code class="cCode">books.DTD</code>, from the output of the <code class="cCode">books.jspx</code> page. The DTD would look like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><!ELEMENT books (book+) ><!ELEMENT book (surname, firstname, title, price, year, description, inventory) ><!ATTLIST book id CDATA #REQUIRED ><!ELEMENT surname (#PCDATA) ><!ELEMENT firstname (#PCDATA) ><!ELEMENT title (#PCDATA) ><!ELEMENT price (#PCDATA) ><!ELEMENT year (#PCDATA) ><!ELEMENT description (#PCDATA) ><!ELEMENT inventory (#PCDATA) ><a name="wp118433"> </a></pre></div><a name="wp118080"> </a><p class="pBody">To add a document type declaration that references the DTD to the XML request output generated from <code class="cCode">books.jspx</code>, include this <code class="cCode">jsp:output</code> element in <code class="cCode">books.jspx</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><jsp:output doctype-root-element="books" doctype-system="books.DTD" /><a name="wp118467"> </a></pre></div><a name="wp118471"> </a><p class="pBody">With this <code class="cCode">jsp:output</code> action, the JSP container generates this document type declaration in the request output:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><!DOCTYPE books SYSTEM "books.DTD" /><a name="wp118474"> </a></pre></div><a name="wp118473"> </a><p class="pBody">The <code class="cCode">jsp:output</code> does not need to be located before the root element of the document. The JSP container will automatically place the resulting document type declaration before the start of the output of the JSP document.</p><a name="wp118489"> </a><p class="pBody">Note that the JSP container will not interpret anything provided by <code class="cCode">jsp:output</code>. This means that the JSP container will not validate the XML document against the DTD. It only generates the document type declaration in the XML request output. To see the XML output, run <code class="cCode">http://localhost:8080/books/books.jspx</code> in your browser after you have updated <code class="cCode">books.WAR</code> with <code class="cCode">books.DTD</code> and the <code class="cCode">jsp:output</code> element. When using some browsers, you might need to view the source of the page to actually see the output.</p><a name="wp118486"> </a><p class="pBody">Directing the document type declaration to output without interpreting it is useful in situations where another system receiving the output expects to see it. For example, two companies who do business via a Web service might make use of a standard DTD, against which any XML content exchanged between the companies is validated. The document type declaration tells the receiver the nature of the data in the XML file.</p><a name="wp121597"> </a><p class="pBody">For the JSP container to validate <code class="cCode">books.jspx</code> against <code class="cCode">book.DTD</code>, you need to manually include the document type declaration in the <code class="cCode">books.jspx</code> file rather than using <code class="cCode">jsp:output</code>. However, you need to add definitions for all tags in your DTD, including definitions for standard elements and custom tags, like <code class="cCode">jsp:useBean</code> and <code class="cCode">c:forEach</code>. You also need to ensure that the DTD is located in the <code class="cVariable"><J2EE_HOME>/</code><code class="cCode">domains/domain1/config</code> directory so that the JSP container will validate the JSP document against the DTD.</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="JSPX2.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="JSPX4.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 + -