📜  JSP-标准标记库(JSTL)教程

📅  最后修改于: 2020-11-13 05:27:03             🧑  作者: Mango


在本章中,我们将了解JSP中的不同标记。 JavaServer Pages标准标记库(JSTL)是有用的JSP标记的集合,其中封装了许多JSP应用程序共有的核心功能。

JSTL支持常见的结构性任务,例如迭代和条件,用于处理XML文档的标签,国际化标签和SQL标签。它还提供了一个框架,用于将现有的自定义标签与JSTL标签集成在一起。

安装JSTL库

要开始使用JSP标记,您需要首先安装JSTL库。如果您使用的是Apache Tomcat容器,请遵循以下两个步骤-

步骤1-Apache Standard Taglib下载二进制发行并解压缩压缩文件。

步骤2-要使用其Jakarta Taglibs发行版中的Standard Taglib,只需将发行版“ lib”目录中的JAR文件复制到应用程序的webapps \ ROOT \ WEB-INF \ lib目录中。

要使用任何库,必须在使用该库的每个JSP的顶部包含一个指令。

JSTL标签的分类

可以根据其功能将JSTL标记分类为以下JSTL标记库组,这些组可在创建JSP页面时使用-

  • 核心标签

  • 格式化标签

  • SQL标签

  • XML标签

  • JSTL函数

核心标签

标签的核心组是最常用的JSTL标签。以下是在JSP中包含JSTL Core库的语法-

下表列出了核心JSTL标签-

S.No. Tag & Description
1

Like <%= … >, but for expressions.

2

Sets the result of an expression evaluation in a ‘scope’

3

Removes a scoped variable (from a particular scope, if specified).

4

Catches any Throwable that occurs in its body and optionally exposes it.

5

Simple conditional tag which evalutes its body if the supplied condition is true.

6

Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by and .

7

Subtag of that includes its body if its condition evalutes to ‘true’.

8

Subtag of that follows the tags and runs only if all of the prior conditions evaluated to ‘false’.

9

Retrieves an absolute or relative URL and exposes its contents to either the page, a String in ‘var’, or a Reader in ‘varReader’.

10

The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .

11

Iterates over tokens, separated by the supplied delimeters.

12

Adds a parameter to a containing ‘import’ tag’s URL.

13

Redirects to a new URL.

14

Creates a URL with optional query parameters

格式化标签

JSTL格式标记用于格式化和显示国际化网站的文本,日期,时间和数字。以下是在JSP中包含Formatting库的语法-

下表列出了格式化JSTL标签-

S.No. Tag & Description
1

To render numerical value with specific precision or format.

2

Parses the string representation of a number, currency, or percentage.

3

Formats a date and/or time using the supplied styles and pattern.

4

Parses the string representation of a date and/or time

5

Loads a resource bundle to be used by its tag body.

6

Stores the given locale in the locale configuration variable.

7

Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.

8

Specifies the time zone for any time formatting or parsing actions nested in its body.

9

Stores the given time zone in the time zone configuration variable

10

Displays an internationalized message.

11

Sets the request character encoding

SQL标签

JSTL SQL标记库提供用于与关系数据库(RDBMS)(例如Oracle,mySQLMicrosoft SQL Server )进行交互的标记。

以下是在JSP中包含JSTL SQL库的语法-

下表列出了SQL JSTL标签-

S.No. Tag & Description
1

Creates a simple DataSource suitable only for prototyping

2

Executes the SQL query defined in its body or through the sql attribute.

3

Executes the SQL update defined in its body or through the sql attribute.

4

Sets a parameter in an SQL statement to the specified value.

5

Sets a parameter in an SQL statement to the specified java.util.Date value.

6

Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

XML标签

JSTL XML标记提供了以JSP为中心的创建和处理XML文档的方式。以下是在JSP中包含JSTL XML库的语法。

JSTL XML标签库具有用于与XML数据进行交互的自定义标签。这包括解析XML,转换XML数据以及基于XPath表达式的流控制。

在继续执行示例之前,您需要将以下两个与XML和XPath相关的库复制到 \ lib-

以下是XML JSTL标签的列表-

S.No. Tag & Description
1

Like <%= … >, but for XPath expressions.

2

Used to parse the XML data specified either via an attribute or in the tag body.

3

Sets a variable to the value of an XPath expression.

4

Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.

5

To loop over nodes in an XML document.

6

Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by and tags.

7

Subtag of that includes its body if its expression evalutes to ‘true’.

8

Subtag of that follows the tags and runs only if all of the prior conditions evaluates to ‘false’.

9

Applies an XSL transformation on a XML document

10

Used along with the transform tag to set a parameter in the XSLT stylesheet

JSTL函数

JSTL包含许多标准函数,其中大多数是常见的字符串操作函数。以下是在JSP中包含JSTL Functions库的语法-

下表列出了各种JSTL函数-

S.No. Function & Description
1 fn:contains()

Tests if an input string contains the specified substring.

2 fn:containsIgnoreCase()

Tests if an input string contains the specified substring in a case insensitive way.

3 fn:endsWith()

Tests if an input string ends with the specified suffix.

4 fn:escapeXml()

Escapes characters that can be interpreted as XML markup.

5 fn:indexOf()

Returns the index withing a string of the first occurrence of a specified substring.

6 fn:join()

Joins all elements of an array into a string.

7 fn:length()

Returns the number of items in a collection, or the number of characters in a string.

8 fn:replace()

Returns a string resulting from replacing in an input string all occurrences with a given string.

9 fn:split()

Splits a string into an array of substrings.

10 fn:startsWith()

Tests if an input string starts with the specified prefix.

11 fn:substring()

Returns a subset of a string.

12 fn:substringAfter()

Returns a subset of a string following a specific substring.

13 fn:substringBefore()

Returns a subset of a string before a specific substring.

14 fn:toLowerCase()

Converts all of the characters of a string to lower case.

15 fn:toUpperCase()

Converts all of the characters of a string to upper case.

16 fn:trim()

Removes white spaces from both ends of a string.