# JSTL
通過 EL 表達式只能解決單個對象的輸出問題,碰到了像集合的遍歷,復雜的判斷條件,就需要使用 JSTL 標簽。
## 如何引入JSTL標簽
1. 導入標準的JSTL標簽庫,將jstl.jar和standard.jar放置到項目構建路徑中(WEB-INF)或者放到Tomcat/lib目錄中。
2. 將tld文件夾放置到WEB-INF目錄下
3. 在web.xml文件中注冊要使用的taglib
~~~
<jsp-config>
<taglib>
<taglib-uri>http://www.ntqingniao.com/core/c</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
</jsp-config>
~~~
4. 在要使用標簽的JSP頁面導入tld
`<%@ taglib prefix="c" uri="http://www.ntqingniao.com/core/c"%>`
## 使用JSTL標簽
**判斷標簽**
使用執行判斷,如果條件成立,則執行標簽體內部的內容
語法:
~~~
<c:if test=”判斷條件 EL表達式”></c:if>
<c:if test="${msg == 'no' }">
nnnn
</c:if>
<c:if test="${msg != 'no' }">
yyyy
</c:if>
~~~
> c:if 沒有所謂的 else 一說,可以使用 == 和 != 進行邏輯判斷
**迭代輸出標簽**
用于迭代輸出集合變量的數據
標簽具有以下一些屬性
* var:迭代參數的名稱。在迭代體中可以使用的變量的名稱,用來表示每一個迭代變量。類型為String。
* items:要進行迭代的集合。對于它所支持的類型將在下面進行講解。
* varStatus:迭代變量的名稱,用來表示迭代的狀態,可以訪問到迭代自身的信息。
* begin:如果指定了items,那么迭代就從items\[begin\]開始進行迭代;如果沒有指定items,那么就從begin開始迭代。它的類型為整數。
* end:如果指定了items,那么就在items\[end\]結束迭代;如果沒有指定items,那么就在end結束迭代。它的類型也為整數。
* step:迭代的步長。
~~~
<c:forEach items="${stus }" var="stu1" varStatus="status">
<tr>
<td>${status.count }</td>
<td>${stu1.id }</td>
<td>${stu1.name }</td>
<td>${stu1.code }</td>
</tr>
</c:forEach>
~~~
> JSTL標簽一般都是和EL表達式結合起來使用的。