本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[/*Java*/][转]XML Tag Library
nybon 发表于 2005/2/16 14:59:08

XML Tag Library The JSTL XML tag set is listed in Table 14-5.  Table 14-5 XML Tags  A key aspect of dealing with XML documents is to be able to easily access their content. XPath (see How XPath Works), a W3C recommendation since 1999, provides an easy notation for specifying and selecting parts of an XML document. In the JSTL XML tags, XPath expressions specified using the select attribute are used to select portions of XML data streams. Note that XPath is used as a local expression language only for the select attribute. This means that values specified for select attributes are evaluated using the XPath expression language but that values for all other attributes are evaluated using the rules associated with the JSP 2.0 expression language. In addition to the standard XPath syntax, the JSTL XPath engine supports the following scopes to access web application data within an XPath expression: $foo $param: $header: $cookie: $initParam: $pageScope: $requestScope: $sessionScope: $applicationScope: These scopes are defined in exactly the same way as their counterparts in the JSP expression language discussed in Implicit Objects. Table 14-6 shows some examples of using the scopes. Table 14-6 Example XPath Expressions XPath Expression Result $sessionScope:profile The session-scoped EL variable named profile $initParam:mycom.productId The String value of the mycom.productId context parameter The XML tags are illustrated in another version (bookstore5) of the Duke's Bookstore application. This version replaces the database with an XML representation of the bookstore database, which is retrieved from another web application. The directions for building and deploying this version of the application are in The Example JSP Document. A sample bookstore5.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. Core Tags The core XML tags provide basic functionality to easily parse and access XML data. The parse tag parses an XML document and saves the resulting object in the EL variable specified by attribute var. In bookstore5, the XML document is parsed and saved to a context attribute in parsebooks.jsp, which is included by all JSP pages that need access to the document: <c:if test="${applicationScope:booklist == null}" >   <c:import url="${initParam.booksURL}" var="xml" />   <x:parse doc="${xml}" var="booklist" scope="application" /> </c:if> The set and out tags parallel the behavior described in Variable Support Tags and Miscellaneous Tags for the XPath local expression language. The set tag evaluates an XPath expression and sets the result into a JSP EL variable specified by attribute var. The out tag evaluates an XPath expression on the current context node and outputs the result of the evaluation to the current JspWriter object. The JSP page bookdetails.jsp selects a book element whose id attribute matches the request parameter bookId and sets the abook attribute. The out tag then selects the book's title element and outputs the result. <x:set var="abook"   select="$applicationScope.booklist/     books/book[@id=$param:bookId]" />   <h2><x:out select="$abook/title"/></h2> As you have just seen, x:set stores an internal XML representation of a node retrieved using an XPath expression; it doesn't convert the selected node into a String and store it. Thus, x:set is primarily useful for storing parts of documents for later retrieval. If you want to store a String, you must use x:out within c:set. The x:out tag converts the node to a String, and c:set then stores the String as an EL variable. For example, bookdetails.jsp stores an EL variable containing a book price, which is later provided as the value of a fmt tag, as follows: <c:set var="price">   <x:out select="$abook/price"/> </c:set> <h4><fmt:message key="ItemPrice"/>:   <fmt:formatNumber value="${price}" type="currency"/> The other option, which is more direct but requires that the user have more knowledge of XPath, is to coerce the node to a String manually by using XPath's string function. <x:set var="price" select="string($abook/price)"/> Flow Control Tags The XML flow control tags parallel the behavior described in Flow Control Tags for XML data streams. The JSP page bookcatalog.jsp uses the forEach tag to display all the books contained in booklist as follows: <x:forEach var="book"   select="$applicationScope:booklist/books/*">   <tr>     <c:set var="bookId">       <x:out select="$book/@id"/>     </c:set>=     <td bgcolor="#ffffaa">       <c:url var="url"       value="/bookdetails" >         <c:param name="bookId" value="${bookId}" />         <c:param name="Clear" value="0" />       </c:url>       <a href="${url}">       <strong><x:out select="$book/title"/>&nbsp;       </strong></a></td>     <td bgcolor="#ffffaa" rowspan=2>       <c:set var="price">         <x:out select="$book/price"/>       </c:set>       <fmt:formatNumber value="${price}" type="currency"/>       &nbsp;     </td>     <td bgcolor="#ffffaa" rowspan=2>     <c:url var="url" value="/catalog" >       <c:param name="Add" value="${bookId}" />     </c:url>     <p><strong><a href="${url}">&nbsp;       <fmt:message key="CartAdd"/>&nbsp;</a>     </td>   </tr>   <tr>     <td bgcolor="#ffffff">     &nbsp;&nbsp;<fmt:message key="By"/> <em>       <x:out select="$book/firstname"/>&nbsp;       <x:out select="$book/surname"/></em></td></tr> </x:forEach> Transformation Tags The transform tag applies a transformation, specified by an XSLT stylesheet set by the attribute xslt, to an XML document, specified by the attribute doc. If the doc attribute is not specified, the input XML document is read from the tag's body content. The param subtag can be used along with transform to set transformation parameters. The attributes name and value are used to specify the parameter. The value attribute is optional. If it is not specified, the value is retrieved from the tag's body. 500)this.width=500'>

阅读全文(2290) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
 



The Neurotic Fishbowl

.: 公告

This blog focuses on:

Semantic Web && Java Technology


Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(171)
/*SemanticWeb*/(34)
/*Java*/(74)
/*FreeComments*/(59)
/*Agent*/(4)


In the Bowl

.: 最新日志

The End
使用Google Trends进行选型
怎样才能称为一次新的版本发行?
如何防止RSS信息过载
使用Excel作为用户接口
如何有效地报告Bug
sourceforge再次被封
趣文两篇
编写Firefox扩展
Jetspeed心得随笔


.: 最新回复

回复:Google API与yahoo 
回复:JADE 3.3的bug
回复:JADE 3.3的bug
回复:JADE 3.3的bug
回复:JADE 3.3的bug
回复:Jbpm和Shark比较的feat
回复:JADE 3.3的bug
回复:JADE 3.3的bug
回复:[转]批判性地看待一种可行的表示技
回复:JIRA破解


The Fishkeeper
blog名称:SW Portal
日志总数:171
评论数量:219
留言数量:8
访问次数:1048181
建立时间:2004年10月30日



Text Me

.: 留言板

签写新留言

路过
路过
页脚问题
RE:请问一下你的主页的下面部分是怎么关
请问一下你的主页的下面部分是怎么关闭的?
我是做Mobile Agent的
Gmail
不错
不错啊小倪同学


Other Fish in the Sea

.: 链接





站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144773946 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号