JSTL:JavaServerPages Standard Tag Library(JSP標准標簽庫)
---JSTL的功能
用標簽代替JSP頁面中的Java代碼塊
簡化JSP頁面代碼結構
---需要在JSP中使用taglib指令指定使用的庫
---需要使用jstl.2.jar包
條件標簽<c:if>
eg:
JSTL標簽
1 <C:if test="${empty list}" var="isListEmpty" scope="session"> 2 列表為空 3 </c:if>
Java代碼
1 List list=(List)request.getAttribute("list"); 2 boolean isListEmpty=(list==null); 3 if(isListEmpty){ 4 out.print("列表為空"); 5 }
條件標簽<c:choose>、<c:when>、<c:otherwise>
eg:
1 <c:choose> 2 <c:when test=${b>5}> 3 b大於5 4 </when> 5 <c:when test=${b<5}> 6 b小於5 7 </when> 8 <c:otherwise> 9 b等於5 10 </c:otherwise> 11 </c:choose>
循環標簽<c:forEach>
eg:
JSTL標簽
1 <c:forEach var="obj" items="${list}"> 2 ${obj} 3 </c:forEach>
Java代碼
1 for(String obj :list){ 2 out.print(obj); 3 }
URL相關標簽<c:import>、<c:url>、<c:redirect>、<c:param>
<c:url>標簽:
JSTL標簽
1 <c:url value="index.jsp" var="myurl"> 2 <a href="${myurl}">鏈接到index.jsp</a>
java代碼
1 <%String myurl="index.jsp"%> 2 <a href="<%=myurl%>">鏈接到index.jsp</a>
<c:redirect>標簽和<c:param>
JSTL標簽
1 <c:redirect url="right.jsp"> 2 <c:param name="userName" value="張三"/> 3 </c:redirect>
java代碼
1 String name="userName"; 2 String value="張三"; 3 response.sendRedirect("right.jsp?"+name+"="_value);
<c:out>、<c:set>、<c:remove>標簽
eg:
JSTL標簽
1 <c:set var="a" value="${'hello'}"scope="session"/> 2 <c:out value="${a}"/> 3 <a:remove var="a" scope="session"/>
java代碼
1 session.setAttribute("a","hello"); 2 out.print(session.getAttribute("a")); 3 session.removeAttribute("a");
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------