English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The <c:choose> tag has the same function as the Java switch statement and is used to make a choice among many options.
In a switch statement, there is a case, and in the <c:choose> tag, there is a corresponding <c:when>. In a switch statement, there is a default, and in the <c:choose> tag, there is a <c:otherwise>.
<c:choose> <c:when test="<boolean>"> ... </c:when> <c:when test="<boolean>"> ... </c:when> ... ... <c:otherwise> ... </c:otherwise> </c:choose>
Attribute | Description | Is Necessary | Default Value |
---|---|---|---|
test | Condition | Yes | None |
<%@ 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 Tag Example</title> </head> <body> <c:set var="salary" scope="session" value="${2000*2"/> <p>Your salary is : <c:out value="${salary}"/></p> <c:choose> <c:when test="${salary} <= 0"> Too bad. </c:when> <c:when test="${salary}> 1000}> Good salary, can live well. </c:when> <c:otherwise> Nothing. </c:otherwise> </c:choose> </body> </html>
The running result is as follows:
Your salary is : 4000 Good salary, can live well.