JSTL标签库 条件标签、循环标签、url标签等

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");

 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/technicist/p/12662961.html