Java 中的 JSTL
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
<%@ 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>
输出:
示例 #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>
输出:
示例 #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>
输出:
示例 #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>
输出:
示例 #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>
输出:
示例 #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>
输出:
示例 #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>
输出:
示例 #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>
输出:
示例 #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的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。

Spring Boot简化了可靠,可扩展和生产就绪的Java应用的创建,从而彻底改变了Java开发。 它的“惯例惯例”方法(春季生态系统固有的惯例),最小化手动设置
