ホームページ  >  記事  >  Java  >  JavaのJSTL

JavaのJSTL

WBOY
WBOYオリジナル
2024-08-30 16:21:02709ブラウズ

JSTL は Java Standard Tag Library と略され、JSP (Java Server Pages) をさらに拡張したものです。 JSTL により、開発者のコ​​ード行が削減されました。この JSTL は、条件付きタスクや反復などの一般的なタスクである構造タスクをサポートします。これらのタグは、I18N (国際化) タグ、SQL タグ、XML ドキュメントなどを変更するために使用されます。これは、Java 標準タグ ライブラリ内の既存のカスタム タグを添付するためのフレームワークも提供します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

JSTL の利点:

  • 迅速な開発。
  • コードの再利用性。
  • スクリプトレット タグを使用する必要はありません。

JSTL は Java でどのように機能しますか?

JSTL は、アプリケーションで使用したタグのタイプに基づいて機能します。 5種類程度のタグで構成されています。彼らは

です
  1. コアタグ
  2. 機能タグ
  3. タグの書式設定
  4. XML タグ
  5. 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
タグの種類 説明 含める URI プレフィックス コアタグ JSTL コア タグは、フロー制御、変数サポート、URL 管理などを提供します。 http://java.sun.com/jsp/jstl/core c 関数タグ この関数タグは文字列操作と
に使用されます。 文字列の長さ http://java.sun.com/jsp/jstl/
関数 fn タグの書式設定 この書式設定タグは、数値、日付、メッセージの書式設定に使用されます http://java.sun.com/jsp/jstl/fmt fmt XML タグ この XML タグは、変換やフロー制御などに使用されます。 http://java.sun.com/jsp/jstl/xml x SQL タグ この SQL タグは、SQL サポートのサポートを提供するために使用されます。 http://java.sun.com/jsp/jstl/sql SQL テーブル>

各 JSTL タグは、やはり異なるサブタグで構成されています。今度は、コアタグとファンクションタグの例をいくつか紹介します。

注: これらの機能を活用してください。 jstl.1.X.jar ファイルを使用する必要があります。 X はバージョンを示します。

以下に例を示します:

Eclipse のプロジェクト構造:

JavaのJSTL

例 #1 – コアタグ c:out

コード: NewFile.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<!--THis c:out tag is used for displaying output-->
<c:out value="${'Hello Amardeep, Wel come to EDUCBA online courses.'}" />
</body>
</html>

出力:

JavaのJSTL

例 #2 – コアタグセットと c:if タグ

コード: SetCIf.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="money" scope="session" value="${5000*5}"/>
<c:if test="${money > 8000}">
<p>My Salary is: <c:out value="${money}"/><p>
</c:if>
</body>
</html>

出力:

JavaのJSTL

例 #3 – c:choose タグを使用したコアタグ

コード: CChoose.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="money" scope="session" value="${5000*5}"/>
<p>Your income is : <c:out value="${money}"/></p>
<c:choose>
<c:when test="${money <= 1000}">
You are paid with good salary.
</c:when>
<c:when test="${money > 10000}">
You are paid with really good salary.
</c:when>
<c:otherwise>
You are paid with low salary .
</c:otherwise>
</c:choose>
</body>
</html>

出力:

JavaのJSTL

例 #4 – c:when タグを使用したコアタグ

コード: CWhen.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set value="214" var="digit"></c:set>
<c:choose>
<c:when test="${digit%2==0}">
<c:out value="${digit} is EVEN digit"></c:out>
</c:when>
<c:otherwise>
<c:out value="${digit} is ODD digit"></c:out>
</c:otherwise>
</c:choose>
</body>
</html>

出力:

JavaのJSTL

例 #5 – c:foreach タグを使用したコアタグ

コード: CForeach.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:forEach var="iterator" begin="100" end="110">
Count: <c:out value="${iterator}" />
<p>
</c:forEach>
</body>
</html>

出力:

JavaのJSTL

例 #6 – c:forTokens タグを含むコアタグ

コード: CForTokens.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:forTokens items="My-Name-is-Nathi-Paramesh" delims="-" var="del">
Word: <c:out value="${del}" />
<p>
</c:forTokens>
</body>
</html>

出力:

JavaのJSTL

例 # 7 – c:redirect タグを含むコアタグ

コード: CRedirect.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="urlName" value="2" scope="request"/>
<c:if test="${urlName<1}">
<c:redirect url="http:/educba.com"/>
</c:if>
<!-- Page directly redirect to gmail because value is greater than 1 -->
<c:if test="${urlName>1}">
<c:redirect url="http://gmail.com"/>
</c:if>
</body>
</html>

出力:

JavaのJSTL

例 #8 – c:contains タグを含む関数タグ

コード: FunctionContains.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="StringType"
value="We are learning online course from EDUCBA platform" />
<c: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>
</c:if>
<c:if test="${fn:contains(StringType, 'courses')}">
<p>No Given String is not found in the value
<p>
</c:if>
</body>
</html>

出力:

JavaのJSTL

例 # 9 – c:endsWith タグを使用した関数タグ

コード: CEndsWith.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="StringType"
value="We are learning online course from EDUCBA platform" />
<c:if test="${fn:endsWith(StringType, 'platform')}">
<h1 style="color: red">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>
</c:if>
<c:if test="${fn:endsWith(String, 'are')}">
<p>String ends with are.
<p>
</c:if>
</body>
</html>

出力:

JavaのJSTL

以上がJavaのJSTLの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:Java 一時的次の記事:Java 一時的