<x:forEach>-Tag
Das <x:forEach>-Tag wird verwendet, um Knoten eines XML-Dokuments zu durchlaufen.
Syntaxformat
<x:forEach var="<string>" select="<string>" begin="<int>" end="<int>" step="<int>" varStatus="<string>">
Attribute
Das <x:forEach>-Tag hat die folgenden Attribute:
| Attribute | Beschreibung | Erforderlich | Standardwert |
|---|---|---|---|
| select | Der auszuwertende XPath-Ausdruck | Ja | Keine |
| var | Die Variable, die zum Speichern des aktuellen Elements verwendet wird | nein | Keine |
| begin | Der Startindex des Iterators | nein | Keine |
| end | Der Endindex des Iterators | nein | Keine |
| Schrittweite | Die Schrittweite der Iteration | nein | Keine |
| varStatus | Die Variable, die den vom Iterator gespeicherten Zustand darstellt | nein | Keine |
Beispiel-Demonstration
<%@ 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>
Das Ausführungsergebnis ist wie folgt:
BOOKS INFO: Book Name: Padam History Book Name: Great Mistry
Andere Erweiterungen
JSP-Standard-Tag-Bibliothek