<c:choose>, <c:when>, <c:otherwise> Tags

JSP 标准标签库JSP Standard Tag Library

The <c:choose> tag functions like the Java switch statement, used to make a choice among many options.

The switch statement has case, corresponding to <c:when> in the <c:choose> tag; the switch statement has default, corresponding to <c:otherwise> in the <c:choose> tag.

Syntax Format

<c:choose>
    <c:when test="<boolean>">
        ...
    </c:when>
    <c:when test="<boolean>">
        ...
    </c:when>
    ...
    ...
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

Attributes

  • The <c:choose> tag has no attributes.
  • The <c:when> tag has only one attribute, given in the table below.
  • The <c:otherwise> tag has no attributes.
The attributes of the <c:when> tag are as follows:
Attribute Description Required Default Value
test Condition Yes None

Example

<%@ 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>

The output is as follows:

你的工资为 : 4000

不错的薪水,还能生活。

JSP 标准标签库JSP Standard Tag Library

Other Extensions