Classification of a Conditional Label
1 <c:if>
2 <c:choose>
3 <c:when>
4 <c:otherwise>
Two <c:if> tags applied
1 Grammar
For conditional judgment, if its test property is true, then process its body.
2 Examples
<%@ page language="java" import="java.util.*,com.cakin.domain.*" pageEncoding="utf-8" %>
<!-- Introduce jstl tag library -->
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html>
<head>
<title>My JSP 'c_out.jsp' starting page</title>
</head>
<body>
<%
request.setAttribute( "abc" , "Beijing, China" );
request.setAttribute("age",23);
Rat rat=new Rat();
rat.setName( "Xiaobao" );
rat.setAge(1);
request.setAttribute("rat1",rat);
%>
< h1 > judgment string </ h1 >
< c:if test = " ${abc=='Beijing, China'} " >
OK
</c:if>
< c:if test = " ${abc!='Beijing, China'} " >
NO OK
</c:if>
< h1 > Judgment value </ h1 >
<c:if test="${age==56}">
equal to 56
</c:if>
<c:if test="${age<56}">
less than 56
</c:if>
< h1 > Judgment range </ h1 >
<c:if test="${age>10 and age<34}">
age>10 and age<34
</c:if>
< h1 > Judging the properties of the object </ h1 >
<c:if test="${rat1.age<10}">
小老鼠年龄小于10岁
</c:if>
</body>
</html>
3 测试结果
三 <c:choose> 标签应用
1、语法
2、举例
<%@ page language="java" import="java.util.*,com.cakin.domain.*" pageEncoding="utf-8" %>
<!-- 引入jstl标签库-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html>
<head>
<title>My JSP 'c_out.jsp' starting page</title>
</head>
<body>
<%
Rat rat=new Rat();
rat.setName("小宝");
rat.setAge(1);
request.setAttribute("rat1",rat);
%>
<h1>判断字符串</h1>
<c:choose>
<c:when test="${rat1.age<2}">
老鼠很小,不能吃
</c:when>
<c:when test="${rat1.age>2 and rat1.age<8}">
老鼠很壮,好吃
</c:when>
<c:when test="${rat1.age>8}">
老鼠很老,不好吃
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
</body>
</html>
3、测试结果