您当前的位置:电脑,基础,教程,源码,下载,信息—问百度开发语言Java → 在线教程内容

JSP函数与对象学习之 <x:forEach>动作

  • 作者:佚名    来源:不详    发布时间:2007-8-27 14:41:55
  • 字体大小:

<x:forEach>动作用于表示XML循环标记,其语法格式如下:

<x:forEach  select="XPathExpression" 

    [var="varName"] 

    [varStatus="varStatus"] 

    [begin="startIndex"]  [end="stopIndex"]  [step="increment"]>

    JSP elements

</x:forEach>

<x:forEach>动作共有6个属性,包括var、select、varStatus、begin、end和step。

.1  var属性:指定变量名

【功能说明】var属性用于指定保存当前元素的变量名称,其类型依赖于select属性中XPath表达式的计算结果。var属性不可以接受动态值。

【实例演示】

<%@  page  contentType="text/html;charset=GB2312"  %>

<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core"  %>

<%@  taglib  prefix="x"  uri="http://java.sun.com/jsp/jstl/xml"  %>

<x:parse  var="book"  scope="application">

    <c:import  url="book.xml"  charEncoding="GB2312"  />

</x:parse>

<table border=0 width="65%">

  <tr bgcolor="lightgreen">

    <th>书名</th>

    <th>类型</th>

    <th>定价</th>

  </tr>

  <x:forEach  var="item"  select="$book/books/book">

  <tr>

    <td align=center><x:out  select="$item//name"  /></td>

    <td align=center><x:out  select="$item//type"  /></td>

    <td align=center><x:out  select="$item//price/@symbol"  /><x:out  select="$item//price"  /></td>

  </tr>

  </x:forEach>

</table>

示例代码的执行效果如图9.10所示。

图9.10  使用<x:forEach>动作的var属性

.2  select属性:指定XPath表达式

【功能说明】select属性用于指定要计算的XPath表达式,不可以接受动态值。

【实例演示】

<%@  page  contentType="text/html;charset=GB2312"  %>

<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core"  %>

<%@  taglib  prefix="x"  uri="http://java.sun.com/jsp/jstl/xml"  %>

<x:parse  var="book"  scope="application">

    <c:import  url="book.xml"  charEncoding="GB2312"  />

</x:parse>

<table border=0 width="65%">

  <tr bgcolor="lightgreen">

    <th>书名</th>

    <th>类型</th>

    <th>定价</th>

  </tr>

  <x:forEach  select="$book/books/book">

  <tr>

    <td align=center><x:out  select="name"  /></td>

    <td align=center><x:out  select="type"  /></td>

    <td align=center><x:out  select="price/@symbol"  /><x:out  select="price"  /></td>

  </tr>

  </x:forEach>

</table>

示例代码的执行效果参见9.8.1小节(请注意代码的差别)。

9.8.3  varStatus属性:指定状态变量名

【功能说明】varStatus属性用于指定保存循环状态的变量名称,其类型为javax.servlet. jsp.jstl.core.LoopTagStatus。varStatus不可以接受动态值。

【实例演示】

<%@  page  contentType="text/html;charset=GB2312"  %>

<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core"  %>

<%@  taglib  prefix="x"  uri="http://java.sun.com/jsp/jstl/xml"  %>

<x:parse  var="book"  scope="application">

    <c:import  url="book.xml"  charEncoding="GB2312"  />

</x:parse>

<table border=0 width="75%">

  <tr bgcolor="lightgreen">

    <th>序号</th>

    <th>书名</th>

    <th>类型</th>

    <th>定价</th>

  </tr>

  <x:forEach  varStatus="status"  select="$book/books/book">

  <tr>

    <td align=center><c:out  value="${status.index+1}"  /></td>

    <td align=center><x:out  select="name"  /></td>

    <td align=center><x:out  select="type"  /></td>

    <td align=center><x:out  select="price/@symbol"  /><x:out  select="price"  /></td>

  </tr>

  </x:forEach>

</table>



[1] [2] 下一页