- 概念:JSP Standard Tag Library JSP标准标签库,jstl通过嵌入到jsp页面中使用标签的形式完成一定的业务逻辑,一般情况下JSTL和EL是搭配使用的,可以简化jsp脚本代码
- 在jstl中有五个子库,使用频率最高的是核心库core
标签库 | 标签库的url | 前缀 |
---|---|---|
core | http://java.sun.com/jsp/jstl/core | c |
I18N | http://java.sun.com/jsp/jstl/fmt | fmt |
sql | http://java.sun.com/jsp/jstl/sql | sql |
xml | http://java.sun.com/jsp/jstl/xml | x |
Functions | http://java.sun.com/jsp/jstl/functions | fn |
###jstl常用标签
<C:url=“路径”>
<body>
<form action="${pageContext.request.contextPath}/selectAll" method="post">
<input type="submit" value="提交">
</form>
<%--c标签url请求路径 可以优化虚拟路径 缺点在分页中的请求资源路径可能无效--%>
<form action="<c:url value='/selectAll'/>" method="post">
<input type="submit" value="提交">
</form>
<a href="${pageContext.request.contextPath}/update">超链接1</a>
<%--c标签url请求路径 可以优化虚拟路径 缺点在分页中的请求资源路径可能无效--%>
<a href="<c:url value='/update' />">超链接2</a>
</body>
<c:if test=“条件表达式”>单条件判断标签
<h3>c:if判断标签</h3>
<%--<c:if text="条件判断语句"--%>
<%
ArrayList<Object> list = new ArrayList<>();
pageContext.setAttribute("list", list);
%>
<c:if test="${1!=2}">
<h3>1不等于2</h3>
</c:if>
<c:if test="${empty list}">
<h3>list值为空......</h3>
</c:if>
c:choose 多条件判断标签 结合c:when标签
<h3>c:choose</h3>
<%--多支判断 情形互斥--%>
<c:choose>
<c:when test="${1 == 2}">
<h3>1等于2</h3>
</c:when>
<c:when test="${1 > 2}">
<h3>1大于2</h3>
</c:when>
<%--以上情况下均不满足,执行c:otherwise中的内容--%>
<c:otherwise>
<h3>1小于2</h3>
</c:otherwise>
</c:choose>
注意事项:不能单独使用c:choose标签
c:forEach 循环遍历标签(普通for循环)
- begin:表示索引开始值
- end:表示索引结束值 默认是lenght-1
- step:表示频率,默认值1,可以指定频率值
- varStatus:表示当前的状态,①index:表示循环的当前索引值,索引值默认从0开始②count:表示循环的当前次数,当前次数从1开始
- items:需要遍历的集合元素
- var:从每次循环中拿出来的元素起一个变量名
<h3>c:forEach</h3>
<%
ArrayList<People> peoples = new ArrayList<>();
<%--people实体类自行设计 就两个字段值一个username 一个 password--%>
peoples.add(new People("小孙1", "111"));
peoples.add(new People("小孙2", "222"));
peoples.add(new People("小孙3", "333"));
peoples.add(new People("小孙4", "444"));
peoples.add(new People("小孙5", "555"));
peoples.add(new People("小孙6", "666"));
peoples.add(new People("小孙7", "777"));
peoples.add(new People("小孙8", "888"));
peoples.add(new People("小孙9", "999"));
peoples.add(new People("小孙10", "1000"));
pageContext.setAttribute("peoples", peoples);
%>
<table border="1px" align="center" width="500px">
<%--end 不包含 [begin,end) 左闭右开--%>
<c:forEach varStatus="status" var="pe" items="${peoples}" begin="2" end="4" step="2">
<tr>
<td>${status.index}</td>
<td>${status.count}</td>
<td>${pe.username}</td>
<td>${pe.password}</td>
</tr>
</c:forEach>
fmt:formatDate 格式化日期标签
<h3>format日期格式化标签</h3>
<%
pageContext.setAttribute("date", new Date());
%>
<tr>
<td>
<fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss" />
</td>
</tr>