JSTL Core < c:choose >, < c:when >, < c:otherwise > 标签
<c:choose> 就像 Java switch 语句,它可以让你进行一些选择。正如 switch 语句有 case 语句,<c:choose> 标签有 <c:when> 标签。一个 switch 语句中有 default 子句来指定一个默认的操作,同样的方式<c:choose>有<c:otherwise>作为默认子句。
属性:
-
<c:choose>标签没有任何属性。 -
<c:when>标签有一个属性,在下面列出了。 <c:otherwise>标签没有任何属性。
<c:when> 标签具有以下属性:
| 属性 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
| test | 计算的条件 | 是 | 无 |
实例:
<%@ 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}">
Salary is very low to survive.
</c:when>
<c:when test="${salary > 1000}">
Salary is very good.
</c:when>
<c:otherwise>
No comment sir...
</c:otherwise>
</c:choose>
</body>
</html>
这将产生如下所示结果:
Your salary is : 4000 Salary is very good.