Balises <x:choose>, <x:when>, <x:otherwise>
Bibliothèque de balises standard JSP
La balise <x:choose> a la même fonctionnalité que l'instruction switch Java. L'instruction switch possède des instructions case, tandis que la balise <x:choose> possède des balises <x:when>. L'instruction switch possède une instruction default, tandis que la balise <x:choose> possède une balise <x:otherwise>.
Format de syntaxe
<x:choose>
<x:when select="<string>">
...
</x:when>
<x:when select="<string>">
...
</x:when>
...
...
<x:otherwise>
...
</x:otherwise>
</x:choose>
Attributes
- <x:choose> n'a aucun attribut.
- Les attributs de <x:when> sont donnés dans le tableau suivant.
- <x:otherwise> n'a aucun attribut.
Attributs de la balise <x:when> :
| Attribut | Description | Requis | Valeur par défaut |
|---|---|---|---|
| select | Condition | Oui | Aucune |
Démonstration d'exemple
<%@ 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>
La sortie est la suivante :
BOOKS INFO: Book is written by ZARA
Autres extensions