首页  >  文章  >  Java  >  Java 中的 JSTL

Java 中的 JSTL

WBOY
WBOY原创
2024-08-30 16:21:02708浏览

JSTL 缩写为 Java Standard Tag Library,它是 JSP(Java Server Pages)的进一步扩展。 JSTL 减少了开发人员的代码行数。该 JSTL 支持结构任务、条件任务和迭代等常见任务。这些标签用于更改 I18N(国际化)标签、SQL 标签、XML 文档等。这还提供了一个框架,用于附加 Java 标准标签库中已有的自定义标签。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

JSTL的优点:

  • 快速发展。
  • 代码可重用性。
  • 无需使用 scriptlet 标签。

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java Transient下一篇:Aggregation in Java