Balises <c:choose>, <c:when>, <c:otherwise>
Bibliothèque de balises standard JSP
La balise <c:choose> fonctionne comme l'instruction switch de Java, utilisée pour faire un choix parmi de nombreuses options.
L'instruction switch possède case, correspondant à <c:when> dans la balise <c:choose> ; l'instruction switch possède default, correspondant à <c:otherwise> dans la balise <c:choose>.
Format de syntaxe
<c:choose>
<c:when test="<boolean>">
...
</c:when>
<c:when test="<boolean>">
...
</c:when>
...
...
<c:otherwise>
...
</c:otherwise>
</c:choose>
Attributes
- La balise <c:choose> n'a pas d'attributs.
- La balise <c:when> n'a qu'un seul attribut, donné dans le tableau ci-dessous.
- La balise <c:otherwise> n'a pas d'attributs.
| Attribut | Description | Requis | Valeur par défaut |
|---|---|---|---|
| test | Condition | Oui | Aucune |
Exemple
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:choose 标签实例</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<p>你的工资为 : <c:out value="${salary}"/></p>
<c:choose>
<c:when test="${salary <= 0}">
太惨了。
</c:when>
<c:when test="${salary > 1000}">
不错的薪水,还能生活。
</c:when>
<c:otherwise>
什么都没有。
</c:otherwise>
</c:choose>
</body>
</html>
La sortie est la suivante :
你的工资为 : 4000 不错的薪水,还能生活。
Autres extensions