JSP
JSTL
核心标签库
使用JSTL标签库需要先导入包在jsp中导入核心标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
常用标签
<body>
set标签:设置属性-----setAttribute(); scope属性有四个值,默认是page,还有request,session,application
<br>
<c:set var="name" value="admin" scope="request"></c:set>
${requestScope.name }
<br>
<hr>
out标签:
<br>
<c:out value="${name }"></c:out>
<br>
<hr>
remove标签:
<br>
<c:remove var="name" scope="request"/>
${name }
<br>
<hr>
catch标签:
<br>
<c:catch var="e">
<%
int i = 9/0;
%>
</c:catch>
${e }
</body>
条件标签
if标签:
<br>
<c:set var="age" value="18"></c:set>
<c:if test="${age==18 }">
age=18
</c:if>
<br>
<hr>
choose、when、otherwise标签:
<br>
<c:choose>
<c:when test="${age>18 }">
age大于18
</c:when>
<c:when test="${age==18 }">
age等于18
</c:when>
<c:otherwise>
age小于18
</c:otherwise>
</c:choose>
循环标签
forEach标签:
<br>
<c:forEach var="i" begin="1" end="10" step="2">
${i }
</c:forEach>
<br>
<%
List
<String> list = new ArrayList<>();
for(int i=1; i<=3; i++){
list.add("String"+i);
}
request.setAttribute("list", list);
%>
<c:forEach var="str" items="${list }" varStatus="status">
${str },${status.index },${status.count }------
</c:forEach>
分割标签
forTokens标签:
<br>
<c:set var="s" value="a:b:c:d"></c:set>
<c:forTokens items="${s }" delims=":" var="item">
${item }
</c:forTokens>
包含和重定向标签
import标签:文件包含功能
<br>
<c:import url="jstl1.jsp">
<c:param name="name" value="admin"></c:param>
</c:import>
redirect标签:重定向
<br>
<c:redirect url="jstl1.jsp">
<c:param name="name" value="admin"></c:param>
</c:redirect>
url标签
url标签:
<br>
<c:url var="myurl" value="jstl1.jsp"></c:url>
<a href="${myurl }">jstl1.jsp
</a>