<x:choose>, <x:when>, <x:otherwise> etiquetas
Biblioteca de etiquetas estándar de JSP
La etiqueta <x:choose> tiene la misma función que la sentencia switch de Java. La sentencia switch tiene sentencias case, mientras que la etiqueta <x:choose> tiene etiquetas <x:when>. La sentencia switch tiene una sentencia default, mientras que la etiqueta <x:choose> tiene la etiqueta <x:otherwise>.
Formato de sintaxis
<x:choose>
<x:when select="<string>">
...
</x:when>
<x:when select="<string>">
...
</x:when>
...
...
<x:otherwise>
...
</x:otherwise>
</x:choose>
Atributos
- La etiqueta <x:choose> no tiene atributos.
- Los atributos de <x:when> se indican en la siguiente tabla.
- La etiqueta <x:otherwise> no tiene atributos.
Atributos de la etiqueta <x:when>:
| Atributo | Descripción | Requerido | Valor por defecto |
|---|---|---|---|
| select | Condición | Sí | Ninguno |
Demostración de ejemplo
<%@ 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:choose 标签</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"/>
<x:choose>
<x:when select="$output//book/author = 'ZARA'">
Book is written by ZARA
</x:when>
<x:when select="$output//book/author = 'NUHA'">
Book is written by NUHA
</x:when>
<x:otherwise>
Unknown author.
</x:otherwise>
</x:choose>
</body>
</html>
El resultado de la ejecución es el siguiente:
BOOKS INFO: Book is written by ZARA
Otras extensiones