<x:forEach> 標籤
<x:forEach>標籤用來迴圈遍曆XML文檔的節點。
語法格式
<x:forEach var="<string>" select="<string>" begin="<int>" end="<int>" step="<int>" varStatus="<string>">
屬性
<x:forEach>標籤有如下屬性:
| 屬性 | 描述 | 是否必要 | 默認值 |
|---|---|---|---|
| select | 需要計算的XPath運算式 | 是 | 無 |
| var | 用於存儲當前專案的變數 | 否 | 無 |
| begin | 迭代器的開始索引 | 否 | 無 |
| end | 迭代器的結束索引 | 否 | 無 |
| step | 迭代的步長 | 否 | 無 |
| varStatus | 代表迭代器所存儲的狀態的變數 | 否 | 無 |
實例演示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>JSTL x:forEach 標籤</title>
</head>
<body>
<h3>Books Info:</h3>
<c:set var="xmltext">
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
</c:set>
<x:parse xml="${xmltext}" var="output"/>
<ul class="list">
<x:forEach select="$output/books/book/name" var="item">
<li>Book Name: <x:out select="$item" /></li>
</x:forEach>
</ul>
</body>
</html>
運行結果如下:
BOOKS INFO: Book Name: Padam History Book Name: Great Mistry

JSP 標準標籤庫