Heim  >  Artikel  >  Java  >  JSTL in Java

JSTL in Java

WBOY
WBOYOriginal
2024-08-30 16:21:02709Durchsuche

JSTL wird als Java Standard Tag Library abgekürzt und ist eine weitere Erweiterung für JSP (Java Server Pages). JSTL reduzierte die Codezeilen für den Entwickler. Diese JSTL unterstützt strukturelle Aufgaben, eine allgemeine Aufgabe wie Bedingung und Iteration. Diese Tags werden zum Ändern von I18N-Tags (Internationalisierung), SQL-Tags, XML-Dokumenten usw. verwendet. Dies bietet auch einen Rahmen zum Anhängen der bereits vorhandenen benutzerdefinierten Tags innerhalb der Java Standard Tag Library.

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Vorteile von JSTL:

  • Schnelle Entwicklung.
  • Wiederverwendbarkeit des Codes.
  • Keine Notwendigkeit, ein Scriptlet-Tag zu verwenden.

Wie funktioniert JSTL in Java?

JSTL funktioniert basierend auf dem Tag-Typ, den wir in unserer Anwendung verwendet haben. Es besteht aus etwa 5 Arten von Tags. Sie sind

  1. Kern-Tags
  2. Funktions-Tags
  3. Tags formatieren
  4. XML-Tags
  5. SQL-Tags

JSTL-Tags Beschreibung:

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
Tag-Typ
Beschreibung URI zum Einschließen Präfix
Kern-Tags Das JSTL-Kerntag bietet Flusskontrolle, Variablenunterstützung, URL-Verwaltung usw. http://java.sun.com/jsp/jstl/core c
Funktions-Tags Dieses Funktions-Tag wird zur String-Manipulation und
verwendet Stringlänge
http://java.sun.com/jsp/jstl/
Funktionen
fn
Tags formatieren Dieses Formatierungs-Tag wird für die Zahlen-, Datums- und Nachrichtenformatierung verwendet http://java.sun.com/jsp/jstl/fmt fmt
XML-Tags Dieses XML-Tag wird für Transformation und Flusskontrolle usw. verwendet. http://java.sun.com/jsp/jstl/xml x
SQL-Tags Dieses SQL-Tag wird verwendet, um Unterstützung für die SQL-Unterstützung bereitzustellen.  http://java.sun.com/jsp/jstl/sql sql

Jedes JSTL-Tag besteht wiederum aus verschiedenen Untertags. Jetzt werden wir einige Beispiele für Kern-Tags und Funktions-Tags vorstellen.

Hinweis: Nutzen Sie diese Funktionen; Wir müssen die Datei jstl.1.X.jar verwenden. X gibt die Version an.

Beispiele

Im Folgenden sind die Beispiele aufgeführt:

Projektstruktur in Eclipse:

JSTL in Java

Beispiel Nr. 1 – Kern-Tag c:out

Code: 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>

Ausgabe:

JSTL in Java

Beispiel #2 – Kern-Tag-Set und c:if-Tags

Code: 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>

Ausgabe:

JSTL in Java

Beispiel #3 – Core-Tag mit c:choose-Tag

Code: CHoose.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>

Ausgabe:

JSTL in Java

Beispiel Nr. 4 – Kern-Tag mit c:when-Tag

Code: 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>

Ausgabe:

JSTL in Java

Beispiel #5 – Core-Tag mit c:foreach-Tag

Code: 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>

Ausgabe:

JSTL in Java

Beispiel #6 – Core-Tag mit c:forTokens-Tag

Code: 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>

Ausgabe:

 JSTL in Java

Beispiel Nr. 7 – Core-Tag mit c:redirect-Tag

Code: 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>

Ausgabe:

 JSTL in Java

Beispiel #8 – Funktions-Tag mit c:contains-Tag

Code: 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>

Ausgabe:

 JSTL in Java

Beispiel Nr. 9 – Funktions-Tag mit c:endsWith-Tag

Code: 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>

Ausgabe:

JSTL in Java

Das obige ist der detaillierte Inhalt vonJSTL in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Java TransientNächster Artikel:Java Transient