JSP–JSTL(JSP標準標籤庫)
博客說明
文章所涉及的資料來自互聯網整理和個人總結,意在於個人學習和經驗彙總,如有什麼地方侵權,請聯繫本人刪除,謝謝!
概念
JavaServer Pages Tag Library JSP標準標籤庫
是由Apache組織提供的開源的免費的jsp標籤
作用
用於簡化和替換jsp頁面上的java代碼
安裝
菜鳥教程文檔地址 https://www.runoob.com/jsp/jsp-jstl.html
下載地址
- 官方下載地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
- 菜鳥教程下載地址:jakarta-taglibs-standard-1.1.2.zip
下載 jakarta-taglibs-standard-1.1.2.zip 包並解壓,將 jakarta-taglibs-standard-1.1.2/lib/ 下的兩個 jar 文件:standard.jar 和 jstl.jar 文件拷貝到 /WEB-INF/lib/ 下。
將 tld 下的需要引入的 tld 文件複製到 WEB-INF 目錄下。
接下來我們在 web.xml 文件中添加以下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt-rt</taglib-uri>
<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core-rt</taglib-uri>
<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/sql-rt</taglib-uri>
<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/x-rt</taglib-uri>
<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
使用步驟
-
導入jstl相關jar包
-
引入標籤庫:taglib指令:
<%@ taglib %>
-
使用標籤
常用的JSTL標籤
if–相當於java代碼的if語句
-
屬性:
- test 必須屬性,接受boolean表達式
- 如果表達式爲true,則顯示if標籤體內容,如果爲false,則不顯示標籤體內容
- 一般情況下,test屬性值會結合el表達式一起使用
- test 必須屬性,接受boolean表達式
-
注意:
- c:if標籤沒有else情況,想要else情況,則可以在定義一個c:if標籤
choose–相當於java代碼的switch語句
- 使用choose標籤聲明 相當於switch聲明
- 使用when標籤做判斷 相當於case
- 使用otherwise標籤做其他情況的聲明 相當於default
foreach–相當於java代碼的for語句
感謝
菜鳥教程
黑馬程序員
萬能的網絡
以及勤勞的自己