由于項(xiàng)目需要靶橱,重新學(xué)習(xí)了下jstl郎任,用于后續(xù)在jsp頁面中綁定數(shù)據(jù)辟犀。
- 環(huán)境依賴
Servlet 2.4且JSP 2.0以上
-
<%@ taglib %>引入標(biāo)簽庫:
1)钉迷、以classPath中降传,加入jar包: standard-1.1.2.jar 篷朵, jstl-1.1.2.jar
2)、在相目\WEB-INF\tld\文件夾中放入常用的tld文件:c.tld婆排,fmt.tld
3)声旺、在jsp文件的頂部加入以下內(nèi)容:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
3.具體寫法
- <c:>核心標(biāo)簽庫
JSTL 核心標(biāo)簽庫(C標(biāo)簽)標(biāo)簽共有13個(gè),功能上分為4類:
1.表達(dá)式控制標(biāo)簽:out段只、set腮猖、remove、catch
2.流程控制標(biāo)簽:if赞枕、choose澈缺、when、otherwise
3.循環(huán)標(biāo)簽:forEach炕婶、forTokens
4.URL操作標(biāo)簽:import姐赡、url、redirect
- <c:forEach> 標(biāo)簽
<c:forEach [var="varName"] [varStatus="varStatusName"] begin="begin" end="end" [step="step"]>
本體內(nèi)容
</c:forEach>
<c:forEach items="${domainList }" var="item">
<tr>
<td align="center" valign="middle">${item["domain"]==null?" ":item["domain"]}</td>
<td align="center" valign="middle"><fmt:formatDate value="${item['bind_date']}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td align="center" valign="middle">
<c:if test="${item['domain']!=null}">
<a href="javascript:;" id="${item['domain']}" class="del"> </a>
</c:if>
</td>
</tr>
</c:forEach>
- <c:out> 標(biāo)簽
<c:out value="value" [escapeXml="{true|false}"]>
default value
</c:out>
一般來說柠掂,<c:out>默認(rèn)會將<项滑、>、’涯贞、” 和 & 轉(zhuǎn)換為 <枪狂、>、'肩狂、" 和&摘完。假若不想轉(zhuǎn)換時(shí),只需要設(shè)定<c:out>的escapeXml屬性為fasle就可以了傻谁。
- <c:if> 標(biāo)簽
<c:if test="${not empty item.publish_time}">
內(nèi)容
</c:if>
<c:if test="${item['domain']!=null}">
內(nèi)容
</c:if>
<c:if test="${!empty permissionMap}">
內(nèi)容
</c:if>
- <c:choose> <c:when> <c:otherwise> 標(biāo)簽
<c:set var="score">85</c:set>
<c:choose>
<c:when test="${score>=90}">
你的成績?yōu)閮?yōu)秀孝治!
</c:when>
<c:when test="${score>=70&&score<90}">
您的成績?yōu)榱己?
</c:when>
<c:when test="${score>60&&score<70}">
您的成績?yōu)榧案? </c:when>
<c:otherwise>
對不起,您沒有通過考試审磁!
</c:otherwise>
</c:choose>