English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Biblioteca de etiquetas estándar de JSP
<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>Etiqueta x:forEach de JSTL</title> </head> <body> <h2>Información de libros:</h2> <c:set var="xmltext"> <books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> <book> <name>Gran Mistry</name> <author>NUHA</author> <price>2000</price> </book> </books> </c:set> <x:parse xml="${xmltext}" var="output"/> <ul> <x:forEach select="$output/books/book/name" var="item"> <li>Nombre del libro: <x:out select="$item" /></li> </x:forEach> </ul> </body> </html>
El resultado de la ejecución es el siguiente:
INFORMACIÓN DE LIBROS: Nombre del libro: Padam History Nombre del libro: Gran Mistry