jstl <c:if>标签用法

作用

替代 if 语句, 但不支持 else if 和 else (如果需要,使用<c:choose>标签)

例子

创建 jsp,内容如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
pageContext.setAttribute("i",10);
pageContext.setAttribute("j",20);
%>
<c:if test="${i<j}">i小</c:if>

例子

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    int score=55;
    request.setAttribute("score",score);
%>


${score>=90}<br>

<c:if test="${score>=90}">优</c:if>
<c:if test="${score>=80 && score<90}">良</c:if>
<c:if test="${score>=70 && score<80}">中</c:if>
<c:if test="${score>=60 && score<70}">差</c:if>
<c:if test="${score<60}">不及格</c:if>

解释:

  • test属性,可以是 true、false,也可以是:返回true、false的表达式

原文出处:/show_1EF6NXuGTgJC.html