<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 |
评估条件 | 是 | None |
文件:c_choose.jsp -
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>c:choose 示例</title>
</head>
<body>
<div style="margin: auto; width: 80%">
<c:set var="salary" scope="session" value="${5600*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 > 5000}">
Salary is very good.
</c:when>
<c:otherwise>
No comment sir...
</c:otherwise>
</c:choose>
</div>
</body>
</html>
执行上面示例代码,得到以下输出结果 -
Your salary is : 11200
Salary is very good.