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

📄 jspx3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
&nbsp;&nbsp;&lt;jsp:useBean id=&quot;userNameBean&quot; class=&quot;hello.UserNameBean&quot; &nbsp;&nbsp;&nbsp;&nbsp;scope=&quot;request&quot;/&gt;&nbsp;&nbsp;&lt;jsp:setProperty name=&quot;userNameBean&quot; property=&quot;name&quot; &nbsp;&nbsp;&nbsp;&nbsp;value=&quot;${param.username}&quot; /&gt;&nbsp;&nbsp;&lt;c:if test=&quot;${fn:length(userNameBean.name) gt 0}&quot; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;jsp:directive.include=&quot;response.jsp&quot; /&gt;&nbsp;&nbsp;&lt;/c:if&gt;&nbsp;&nbsp;&lt;/body&gt;&lt;/html&gt;<a name="wp117983"> </a></pre></div><a name="wp117981"> </a><p class="pBody">As you can see, a number of constructs legal in standard syntax have been changed to comply with XML syntax:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp119052"> </a><div class="pSmartList1"><li>The <code class="cCode">taglib</code> directives have been removed. Tag libraries are now declared using XML namespaces, as shown in the <code class="cCode">html</code> element.</li></div><a name="wp119053"> </a><div class="pSmartList1"><li>The <code class="cCode">img</code> and <code class="cCode">input</code> tags did not have matching end-tags and have been made XML-compliant by the addition of a &quot;/&quot; to the start-tag.</li></div><a name="wp120478"> </a><div class="pSmartList1"><li>The &quot;&gt;&quot; symbol in the EL expression has been replaced with a <code class="cCode">gt</code></li></div><a name="wp118005"> </a><div class="pSmartList1"><li>The <code class="cCode">include</code> directive is changed to the XML-compliant <code class="cCode">jsp:directive.include</code> tag.</li></div></ul></div><a name="wp117507"> </a><p class="pBody">With only these few small changes and saving the file with a <code class="cCode">.jspx</code> extension, this page is a JSP document. </p><a name="wp118030"> </a><p class="pBody">Using the example described in <a  href="JSPX2.html#wp116707">The Example JSP Document</a>, the rest of this chapter gives you more details on how to transition from standard syntax to XML syntax. It explains how to use XML namespaces to declare tag libraries, include directives, and create static and dynamic content in your JSP documents. It also describes <code class="cCode">jsp:root</code> and <code class="cCode">jsp:output</code>, two elements that are used exclusively in JSP documents.</p><a name="wp118565"> </a><h3 class="pHeading2">Declaring Tag Libraries</h3><a name="wp118566"> </a><p class="pBody">This section explains how to use XML namespaces to declare tag libraries.</p><a name="wp119780"> </a><p class="pBody">In standard syntax, the <code class="cCode">taglib</code> directive declares tag libraries used in a JSP page. Here is an example of a <code class="cCode">taglib</code> directive:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; &nbsp;&nbsp;prefix=&quot;c&quot; %&gt;<a name="wp118567"> </a></pre></div><a name="wp118575"> </a><p class="pBody">This syntax is not allowed in JSP documents. To declare a tag library in a JSP document, you need to use the <code class="cCode">xmlns</code> attribute, which is used to declare namespaces according to the XML standard:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...xmlns:c=&quot;http://java.sun.com/jsp/jstl/core&quot;...<a name="wp118576"> </a></pre></div><a name="wp118580"> </a><p class="pBody">The value that identifies the location of the tag library can take three forms:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp121058"> </a><div class="pSmartList1"><li>A plain URI that is a unique identifier for the tag library. The container tries to match it against any <code class="cCode">&lt;taglib-uri&gt;</code> elements in the application&#39;s <code class="cCode">web.xml</code> or the <code class="cCode">&lt;uri&gt;</code> element of TLDs in JAR files in <code class="cCode">WEB-INF/lib</code> or TLDs under <code class="cCode">WEB-INF</code>.</li></div><a name="wp120136"> </a><div class="pSmartList1"><li>A URN of the form <code class="cCode">urn:jsptld:path</code></li></div><a name="wp120137"> </a><div class="pSmartList1"><li>A URN of the form <code class="cCode">urn:jsptagdir:path</code></li></div></ul></div><a name="wp120132"> </a><p class="pBody">The URN of the form <code class="cCode">urn:jsptld:path</code> points to one tag library packaged with the application:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">xmlns:u=&quot;urn:jsptld:/WEB-INF/tlds/my.tld&quot;<a name="wp120152"> </a></pre></div><a name="wp121093"> </a><p class="pBody">The URN of the form <code class="cCode">urn:jsptagdir:path</code> must start with <code class="cCode">/WEB-INF/tags/</code> and identifies tag extensions (implemented as tag files) installed in the <code class="cCode">WEB-INF/tags/</code> directory or a subdirectory of it:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">xmlns:u=&quot;urn:jsptagdir:/WEB-INF/tags/mytaglibs/&quot;<a name="wp120154"> </a></pre></div><a name="wp120151"> </a><p class="pBody">You can include the <code class="cCode">xmlns</code> attribute in any element in your JSP document, just as you can in an XML document. In JSP 1.2, you could only include it in a mandatory <code class="cCode">jsp:root</code> element. This new capability has many advantages, including:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp118581"> </a><div class="pSmartList1"><li>It follows the XML standard, making it easier to use any XML document as a JSP document</li></div><a name="wp118584"> </a><div class="pSmartList1"><li>It allows you to scope prefixes to an element and override them</li></div><a name="wp118808"> </a><div class="pSmartList1"><li>It allows you to use <code class="cCode">xmlns</code> to declare other namespaces, not just tag libraries. </li></div></ul></div><a name="wp119905"> </a><p class="pBody">The <code class="cCode">books.jspx</code> page declares the tag libraries it uses with the <code class="cCode">xmlns</code> attributes in the root element, <code class="cCode">books</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;books&nbsp;&nbsp;xmlns:c=&quot;http://java.sun.com/jsp/jstl/core&quot;&nbsp;&nbsp;xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot;&gt;<a name="wp118744"> </a></pre></div><a name="wp118750"> </a><p class="pBody">By doing this, all elements within the <code class="cCode">books</code> element have access to these tag libraries.</p><a name="wp118809"> </a><p class="pBody">As an alternative, you could scope the namespaces, like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;books&gt;...&nbsp;&nbsp;&lt;jsp:useBean &nbsp;&nbsp;&nbsp;&nbsp;xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id=&quot;bookDB&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class=&quot;database.BookDB&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scope=&quot;page&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;jsp:setProperty name=&quot;bookDB&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property=&quot;database&quot; value=&quot;${bookDBAO}&quot; /&gt;&nbsp;&nbsp;&lt;/jsp:useBean&gt;&nbsp;&nbsp;&lt;c:forEach xmlns:c=&quot;http://java.sun.com/jsp/jstl/core&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var=&quot;book&quot; begin=&quot;0&quot; items=&quot;${bookDB.books}&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&lt;/c:forEach&gt;&lt;/books&gt;<a name="wp118768"> </a></pre></div><a name="wp118793"> </a><p class="pBody">This way, the tag library referenced by the <code class="cCode">jsp</code> prefix is only available to the <code class="cCode">jsp:useBean</code> element and its subelements. Likewise, the tag library referenced by the <code class="cCode">c</code> prefix is only available to the <code class="cCode">c:forEach</code> element.</p><a name="wp119923"> </a><p class="pBody">Scoping the namespaces also allows you to override the prefix. For example, in another part of the page, you could bind the <code class="cCode">c</code> prefix to a different namespace or tag library. Conversely, the <code class="cCode">jsp</code> prefix must always be bound to the JSP namespace: <code class="cCode">http://java.sun.com/JSP/Page</code>.</p><a name="wp119784"> </a><h3 class="pHeading2">Including Directives in a JSP Document</h3><a name="wp119818"> </a><p class="pBody">Directives are elements that relay messages to the JSP container and affect how it compiles the JSP page. The directives themselves do not appear in the XML output. </p><a name="wp119841"> </a><p class="pBody">There are three directives: <code class="cCode">include</code>, <code class="cCode">page</code>, and <code class="cCode">taglib</code>. The <code class="cCode">taglib</code> directive is covered in the previous section.</p><a name="wp119819"> </a><p class="pBody">The <code class="cCode">jsp:directive.page</code> element defines a number of page-dependent properties and communicates these to the JSP container. This element must be a child of the root element. Its syntax is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:directive.page <span style="font-style: oblique">page_directive_attr_list </span>/&gt;<a name="wp119822"> </a></pre></div><a name="wp121113"> </a><p class="pBody">The <code class="cCode">page_directive_attr_list</code> is the same list of attributes that the <code class="cCode">&lt;@ page ...&gt;</code> directive has. These are described in Chapter <a  href="JSPIntro.html#wp65706">12</a>. All of the attributes are optional. Except for the <code class="cCode">import</code> and <code class="cCode">pageEncoding</code> attributes, there can be only one instance of each attribute in an element, but an element can contain more than one attribute.</p><a name="wp119927"> </a><p class="pBody">One example of a page directive is one that tells the JSP container to load an error page when it throws an exception. You can add this error page directive to the <code class="cCode">books.jspx</code> page: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;books xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot;&gt;&nbsp;&nbsp;&lt;jsp:directive.page errorPage=&quot;errorpage.jsp&quot; /&gt;&nbsp;&nbsp;...&lt;/books&gt;<a name="wp119946"> </a></pre></div><a name="wp119944"> </a><p class="pBody">If there is an error when you try to execute the page (perhaps when you want to see the XML output of <code class="cCode">books.jspx</code>), the error page is accessed.</p><a name="wp119828"> </a><p class="pBody">The <code class="cCode">jsp:directive.include</code> directive is used to insert the text contained in another file--either static content or another JSP page--in the including JSP document. You can place this element anywhere in a document. Its syntax is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:directive.include file=&quot;<span style="font-style: oblique">relativeURLspec&quot; </span>/&gt;<a name="wp119831"> </a></pre></div><a name="wp119833"> </a><p class="pBody">The XML view of a JSP document does not contain <code class="cCode">jsp:directive.include</code> elements; rather the included file is expanded in place. This is done to simplify validation.</p><a name="wp119989"> </a><p class="pBody">Suppose that you wanted to use an include directive to add a JSP document containing magazine data inside the JSP document containing the books data. To do this, you can add the following include directive to <code class="cCode">books.jspx</code>, assuming that <code class="cCode">magazines.jspx</code> generates the magazine XML data.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;jsp:root version=&quot;2.0&quot; &gt;&nbsp;&nbsp;&lt;books ...&gt;&nbsp;&nbsp;...&nbsp;&nbsp;&lt;/books&gt;&nbsp;&nbsp;&lt;jsp:directive.include file=&quot;magazine.jspx&quot; /&gt;&lt;/jsp:root&gt;<a name="wp120002"> </a></pre></div><a name="wp121134"> </a><p class="pBody">Note that <code class="cCode">jsp:root</code> is required because otherwise <code class="cCode">books.jspx </code>would have two root elements: <code class="cCode">&lt;books&gt;</code> and <code class="cCode">&lt;magazines&gt;</code>. The output generated from <code class="cCode">books.jspx</code> will be a sequence of XML documents: one with <code class="cCode">&lt;books&gt;</code> and the other with <code class="cCode">&lt;magazines&gt;</code> as its root element.</p><a name="wp120570"> </a><p class="pBody">The output of this example will not be well-formed XML because of the two root elements, so the client might refuse to process it. However, it is still a legal JSP document.</p><a name="wp120510"> </a><p class="pBody">In addition to including JSP documents in JSP documents, you can also include JSP pages written in standard syntax in JSP documents, and you can include JSP documents in JSP pages written in standard syntax. The container detects the page you are including and parses it as either a standard syntax JSP page or a JSP document and then places it into the XML view for validation.</p><a name="wp119501"> </a><h3 class="pHeading2">Creating Static and Dynamic Content</h3><a name="wp119509"> </a><p class="pBody">A template provides a way to separate the common elements that are part of each screen (the static content) from the elements that change with each screen (the dynamic content) of an application. See <a  href="JSPTags7.html#wp90689">A Template Tag Library</a> for more information on templates. This section explains how to represent static template text and dynamic content in a JSP document.</p><a name="wp119544"> </a><p class="pBody">You can represent static template text in a JSP document with uninterpreted XML tags or with the <code class="cCode">jsp:text</code> element. The <code class="cCode">jsp:text</code> element passes its content through to the output.</p><a name="wp119570"> </a><p class="pBody">If you use <code class="cCode">jsp:text</code>, all whitespace is preserved. For example, consider this example using XML tags:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;books&gt;&nbsp;&nbsp;&lt;book&gt;&nbsp;&nbsp;&nbsp;&nbsp;Web Servers for Fun and Profit&nbsp;&nbsp;&lt;/book&gt;&lt;/books&gt;<a name="wp119545"> </a></pre></div><a name="wp119528"> </a><p class="pBody">The output generated from this XML has all whitespace removed:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;books&gt;&lt;book&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Web Servers for Fun and Profit&nbsp;&nbsp;&lt;/book&gt;&lt;/books&gt;<a name="wp119529"> </a></pre></div><a name="wp119553"> </a><p class="pBody">If you wrap the example XML with a <code class="cCode">&lt;jsp:text&gt;</code> tag, all whitespace is preserved. The whitespace characters are <code class="cCode">#x20</code>, <code class="cCode">#x9</code>, <code class="cCode">#xD</code> and <code class="cCode">#xA</code>.</p><a name="wp121855"> </a><p class="pBody">You can also use <code class="cCode">jsp:text</code> to output template data that is not well-formed. The <code class="cCode">${counter}</code> expression in the following example would be illegal in a JSP document if it were not wrapped in a <code class="cCode">jsp:text</code> tag.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:forEach var=&quot;counter&quot; begin=&quot;1&quot; end=&quot;${3}&quot;&gt;&nbsp;&nbsp;&lt;jsp:text&gt;${counter}&lt;/jsp:text&gt;

⌨️ 快捷键说明

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