JSTL 縮寫為 Java Standard Tag Library,它是 JSP(Java Server Pages)的進一步擴展。 JSTL 減少了開發人員的程式碼行數。此 JSTL 支援結構任務、條件任務和迭代等常見任務。這些標籤用於更改 I18N(國際化)標籤、SQL 標籤、XML 文件等。這也提供了一個框架,用於附加 Java 標準標籤庫中已有的自訂標籤。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
JSTL的優點:
- 快速發展。
- 程式碼可重用性。
- 無需使用 scriptlet 標籤。
JSTL 在 Java 中如何運作?
JSTL 的工作原理是基於我們在應用程式中使用的標籤類型。它由大約 5 種類型的標籤組成。他們是
- 核心標籤
- 函數標籤
- 格式化標籤
- XML 標籤
- SQL 標籤
JSTL 標籤說明:
Tag Type | Description | URI to include | prefix |
Core tags | The JSTL core tag provides flow control, variable support, URL management etc. | http://java.sun.com/jsp/jstl/core | c |
Function tags | This function tag is used for String manipulation and String length |
http://java.sun.com/jsp/jstl/ functions |
fn |
Formatting tags | This formatting tag is used for number, date and message formatting | http://java.sun.com/jsp/jstl/fmt | fmt |
XML tags | This XML tag is used for transformation and flow control etc. | http://java.sun.com/jsp/jstl/xml | x |
SQL tags | This SQL tag is used to provide support for SQL support. | http://java.sun.com/jsp/jstl/sql | sql |
字串長度
功能
每個 JSTL 標籤又由不同的子標籤組成。現在我們將介紹一些核心標籤和功能標籤的範例。
注意:使用這些功能;我們必須使用jstl.1.X.jar 檔案。 X 表示版本。範例
以下是範例:
eclipse 中的專案結構:
範例 #1 – 核心標籤 c:out
代碼:NewFile.jsp
<title>JSTL Tags</title> <!--THis c:out tag is used for displaying output--> <out value="${'Hello Amardeep, Wel come to EDUCBA online courses.'}"></out>
輸出:
範例 #2 – 核心標籤集和 c:if 標籤
代碼:SetCIf.jsp
<title>JSTL Tags</title> <set var="money" scope="session" value="${5000*5}"></set> <if test="${money > 8000}"> <p>My Salary is: <out value="${money}"></out></p> <p> </p></if>
輸出:
範例 #3 – 帶有 c:choose 標籤的核心標籤
代碼:CChoose.jsp
<title>JSTL Tags</title> <set var="money" scope="session" value="${5000*5}"></set> <p>Your income is : <out value="${money}"></out></p> <choose> <when test="${money <= 1000}"> You are paid with good salary. </when> <when test="${money > 10000}"> You are paid with really good salary. </when> <otherwise> You are paid with low salary . </otherwise> </choose>
輸出:
範例 #4 – 帶有 c:when 標籤的核心標籤
代碼:CWhen.jsp
<title>JSTL Tags</title> <set value="214" var="digit"></set> <choose> <when test="${digit%2==0}"> <out value="${digit} is EVEN digit"></out> </when> <otherwise> <out value="${digit} is ODD digit"></out> </otherwise> </choose>
輸出:
範例 #5 – 帶有 c:foreach 標籤的核心標籤
代碼:CForeach.jsp
<title>JSTL Tags</title> <foreach var="iterator" begin="100" end="110"> Count: <out value="${iterator}"></out> <p> </p></foreach>
輸出:
範例 #6 – 帶有 c:forTokens 標籤的核心標籤
代碼:CForTokens.jsp
<title>JSTL Tags</title> <fortokens items="My-Name-is-Nathi-Paramesh" delims="-" var="del"> Word: <out value="${del}"></out> <p> </p></fortokens>
輸出:
範例 #7 – 帶有 c:redirect 標籤的核心標籤
代碼:CRedirect.jsp
<title>JSTL Tags</title> <set var="urlName" value="2" scope="request"></set> <if test="${urlName<1}"> <redirect url="http:/educba.com"></redirect> </if> <!-- Page directly redirect to gmail because value is greater than 1 --> <if test="${urlName>1}"> <redirect url="http://gmail.com"></redirect> </if>
輸出:
範例 #8 – 帶有 c:contains 標籤的函數標籤
代碼:FunctionContains.jsp
<title>JSTL Tags</title> <set var="StringType" value="We are learning online course from EDUCBA platform"></set> <if test="${fn:contains(StringType, 'EDUCBA')}"> <h1 style="color: green">Yes Given String found in the value </h1> <p style="color:blue;border: 1px solid red;font-size:20px">JSTL abbreviated as Java Standard Tag Library. Which is further extension for JSP (Java Server Pages). JSTL reduced the lines of code for developer. This JSTL supports for structural tasks, common task like conditional and iteration. This tags used for changing I18N (Internationalization) tags, SQL tags, XML documents etc. This JSTL also provides a framework for attaching the already existing custom tags within the Java Standard Tag Library.</p> </if> <if test="${fn:contains(StringType, 'courses')}"> <p>No Given String is not found in the value </p> <p> </p></if>
輸出:
範例 #9 – 帶有 c:endsWith 標籤的函數標籤
代碼:CEndsWith.jsp
<title>JSTL Tags</title> <set var="StringType" value="We are learning online course from EDUCBA platform"></set> <if test="${fn:endsWith(StringType, 'platform')}"> <h1 id="Yes-String-ends-with-platfrom">Yes String ends with platfrom.</h1> <p style="color: fuchsia; border: 1px solid red; font-size: 20px">JSTL abbreviated as Java Standard Tag Library. Which is further extension for JSP (Java Server Pages). JSTL reduced the lines of code for developer. This JSTL supports for structural tasks, common task like conditional and iteration. This tags used for changing I18N (Internationalization) tags, SQL tags, XML documents etc. This JSTL also provides a framework for attaching the already existing custom tags within the Java Standard Tag Library.</p> </if> <if test="${fn:endsWith(String, 'are')}"> <p>String ends with are. </p> <p> </p></if>
輸出:
以上是Java 中的 JSTL的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器