1. 程式人生 > 實用技巧 > JSP--JSTL(JSP標準標籤庫)

JSP--JSTL(JSP標準標籤庫)

JSP--JSTL(JSP標準標籤庫)

部落格說明

文章所涉及的資料來自網際網路整理和個人總結,意在於個人學習和經驗彙總,如有什麼地方侵權,請聯絡本人刪除,謝謝!

概念

JavaServer Pages Tag Library JSP標準標籤庫

是由Apache組織提供的開源的免費的jsp標籤

作用

用於簡化和替換jsp頁面上的java程式碼

安裝

菜鳥教程文件地址 https://www.runoob.com/jsp/jsp-jstl.html

下載地址

下載 jakarta-taglibs-standard-1.1.2.zip 包並解壓,將 jakarta-taglibs-standard-1.1.2/lib/ 下的兩個 jar 檔案:standard.jarjstl.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>

使用步驟

  1. 匯入jstl相關jar包

  2. 引入標籤庫:taglib指令:

    <%@ taglib %>
    
  3. 使用標籤

常用的JSTL標籤

if--相當於java程式碼的if語句
  1. 屬性:

    • test 必須屬性,接受boolean表示式
      • 如果表示式為true,則顯示if標籤體內容,如果為false,則不顯示標籤體內容
      • 一般情況下,test屬性值會結合el表示式一起使用
  2. 注意:

    • c:if標籤沒有else情況,想要else情況,則可以在定義一個c:if標籤
choose--相當於java程式碼的switch語句
  1. 使用choose標籤宣告 相當於switch宣告
  2. 使用when標籤做判斷 相當於case
  3. 使用otherwise標籤做其他情況的宣告 相當於default
foreach--相當於java程式碼的for語句

感謝

菜鳥教程

黑馬程式設計師

萬能的網路

以及勤勞的自己
關注公眾號: 歸子莫,獲取更多的資料,還有更長的學習計劃