English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The '<c:choose>', '<c:when>', '<c:otherwise>' tags

JSP-Standard-Tag-Bibliothek

The '<c:choose>' tag has the same function as the Java switch statement and is used to make a choice among many options.

In the switch statement, there is a case, and the corresponding '<c:when>' is in the '<c:choose>' tag. In the switch statement, there is a default, and in the '<c:choose>' tag, there is a '<c:otherwise>'.

Syntax format

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

Eigenschaft

  • The '<c:choose>' tag has no attributes.
  • The '<c:when>' tag has only one attribute, which is listed in the table below.
  • The '<c:otherwise>' tag has no attributes.

<c:when>-Tag-Eigenschaften sind wie folgt:

Eigenschaft Beschreibung Notwendig Standardwert
test Bedingung Ja Kein

Beispiel演示

<%@ 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 Tagbeispiel</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2"/>
<p>Dein Gehalt beträgt : <c:out value="${salary}"/></p>
<c:choose>
    <c:when test="${salary} <= 0}>
       Schrecklich.
    </c:when>
    <c:when test="${salary}> 1000}>
       Gute Bezahlung, noch kann man leben.
    </c:when>
    <c:otherwise>
        Nichts.
    </c:otherwise>
</c:choose>
</body>
</html>

Das Laufzeitergebnis ist wie folgt:

Dein Gehalt beträgt : 4000
Gute Bezahlung, noch kann man leben.

JSP-Standard-Tag-Bibliothek