一、配置引用
1)需要准备依赖的jar包【jstl.jar】
- 官方下载地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
- 解压开来,将 jstl.jar 放置在web项目里
2)在页面引入标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
二、示例
1)条件判断 <c:if test=""/>,判断对象是否为空
not empty 对象不为空
empty 对象为空
<c:if test="${not empty name}">
欢迎您:${name }
</c:if>
<c:if test="${empty name}">
欢迎您:游客
</c:if>
2)条件判断<c:choose><c:when test=""/></c:otherwise></c:choose>,判断相当于 if else
<c:choose>
<c:when test="${not empty name }">
欢迎您:${name }
</c:when>
...
<c:when test="">
...
</c:when>
...
<c:otherwise>
欢迎您:游客
</c:otherwise>
</c:choose>
3)迭代<c:forEach items="" var="" varStatus=""/>
<c:if test="${list.size()>0 }">
当前用户有:
<!-- items="${list }" 取出后台request中存放的用户列表 -->
<!-- var="user" 遍历的list中的对象 -->
<!-- varStatus="sta" 循环的变量,sta.index为循环对象的下标-->
<c:forEach items="${list }" var="user" varStatus="sta">
${user.username }
<c:if test="${sta.index != list.size()-1 }">
,
</c:if>
</c:forEach>
</c:if>
4)对象赋值<c:set var="" value="" scope=""/>
<!-- 为 password 设置值为123456 -->
<!-- scope="session"默认作用域为:Page,定义当前作用域为 session -->
<!-- scope作用域有:Page、request、session、application -->
<c:set var="password" value="123456" scope="session"></c:set>
5)输出 <c:out value="" default=""/>
<!-- value="${}" 输出变量值 -->
<!-- default="" 默认值 -->
密码为:<c:out value="${password }" default="112233"></c:out>
6)<c:url value=""/>
<!-- <c:url>将url格式为字符串 -->
<a href="<c:url value="${url }"/>">点击进入</a>