------------------------------- JSTL标签if和choose----------------------------
if和choose(重点)
if标签的test属性必须是一个boolean类型的值,如果test的值为true,那么执行if标签的内容,否则不执行。
<c:set var="a" value="hello"/> <c:if test="${not empty a }"> <c:out value="${a }"/> </c:if> |
choose标签对应Java中的if/else if/else结构
when标签的test为true时,会执行这个when的内容。
当所有when标签的test都为false时,才会执行otherwise标签的内容
<c:set var="score" value="${param.score }"/> <c:choose> <c:when test="${score > 100 || score < 0}">错误的分数:${score }</c:when> <c:when test="${score >= 90 }">A级</c:when> <c:when test="${score >= 80 }">B级</c:when> <c:when test="${score >= 70 }">C级</c:when> <c:when test="${score >= 60 }">D级</c:when> <c:otherwise>E级</c:otherwise> </c:choose> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@page import="com.rl.model.*" %> <!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>Insert title here</title> </head> <body> <h1>JSTL核心库if</h1> <c:set var="pname3" value="likunpeng"></c:set> <c:if test="${not empty pname3 }"> <c:out value="${pname3 }"></c:out> </c:if> <hr> <h1>JSTL核心库choose</h1> <c:set var="score" value="70"></c:set> <c:choose> <c:when test="${score < 60 }"> <c:out value="${'没及格' }"></c:out> </c:when> <c:when test="${(score >= 60 && score < 80) }"> <c:out value="${'及格' }"></c:out> </c:when> <c:otherwise> <c:out value="${'你小子学的相当不错' }"></c:out> </c:otherwise> </c:choose> </body> </html> |