<c:if> 标签
<c:if>标签判断表达式的值,如果表达式的值为 true 则执行其主体内容。
语法格式
<c:if test="<boolean>" var="<string>" scope="<string>"> ... </c:if>
属性
<c:if>标签有如下属性:
| 属性 | 描述 | 是否必要 | 默认值 |
|---|---|---|---|
| test | 条件 | 是 | 无 |
| var | 用于存储条件结果的变量 | 否 | 无 |
| scope | var属性的作用域 | 否 | page |
演示实例
<%@ 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:if 标签实例</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>我的工资为: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
运行结果如下:
我的工资为: 4000
JSP 标准标签库
New Soul
139***2267@qq.com
模拟 if-else:
<c:if test="${salary > 2000}" var="flag" scope="session"> <p>我的工资为: <c:out value="${salary}"/><p> </c:if> <c:if test="${not flag}"> <p>我的工资为: <c:out value="${salary}"/><p> </c:if>New Soul
139***2267@qq.com
Janus
127***4983@qq.com
判断空和非空:
<c:if test="${ not empty bookOrder.appraise}"> <div class="J_KgRate_ReviewContent tb-tbcr-content "> ${bookOrder.appraise} </div> </c:if> <c:if test="${empty bookOrder.appraise}"> <div class="J_KgRate_ReviewContent tb-tbcr-content "> 该用户未填写评价 </div> </c:if>Janus
127***4983@qq.com
Janus
127***4983@qq.com
判断空和非空:
<c:if test="${ not empty bookOrder.appraise}"> <div class="J_KgRate_ReviewContent tb-tbcr-content "> ${bookOrder.appraise} </div> </c:if> <c:if test="${empty bookOrder.appraise}"> <div class="J_KgRate_ReviewContent tb-tbcr-content "> 该用户未填写评价 </div> </c:if>多条件判断:
<c:if test="${bookOrder.num>1&&bookOrder.num<10}"> <div class="J_KgRate_ReviewContent tb-tbcr-content "> ${bookOrder.appraise} </div> </c:if>Janus
127***4983@qq.com