Skip to content

Files

Latest commit

5142ebd · Oct 15, 2019

History

History
56 lines (46 loc) · 1.78 KB

7.md

File metadata and controls

56 lines (46 loc) · 1.78 KB

JSTL<c:choose><c:when><c:otherwise>核心标签

原文: https://beginnersbook.com/2013/11/jstl-cchoose-cwhen-cotherwise-core-tags/

在本文中,我们讨论<c:choose><c:when><c:otherwise> JSTL 的核心标签。这些标签一起使用,如 java 中的switch-casedefault语句。<c:choose>就像switch一样,<c:when>就像可以在里面多次使用的case,用于评估不同的两个条件。<c:otherwise>类似于默认语句,当所有<c:when>语句是false的。

语法:

基本结构看起来像这样:

<c:choose>
    <c:when test="${condition1}">
       //do something if condition1 is true
    </c:when>
    <c:when test="${condition2}">
        //do something if condition2 is true
    </c:when>
    <c:otherwise>
        //Statements which gets executed when all <c:when> tests are false.
    </c:otherwise>
</c:choose>

在这个例子中,我们有三个数字,我们使用这三个核心标签来比较它们。示例非常简单易懂。在示例代码之后提供输出的屏幕截图。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:choose, c:when and c:otherwise Tag Example</title>
</head>
<body>
<c:set var="number1" value="${222}"/>
<c:set var="number2" value="${12}"/>
<c:set var="number3" value="${10}"/>
<c:choose>
 <c:when test="${number1 < number2}">
     ${"number1 is less than number2"}
 </c:when>
 <c:when test="${number1 <= number3}">
     ${"number1 is less than equal to number2"}
 </c:when>
 <c:otherwise>
     ${"number1 is largest number!"}
 </c:otherwise>
</c:choose>
</body>
</html>

输出:

c-choose-example