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

在本章中,我们将了解JSP中的不同标记. JavaServer Pages标准标记库(JSTL)是一组有用的JSP标记,它封装了许多JSP应用程序通用的核心功能.

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

安装JSTL库

要开始使用JSP tages,您需要首先安装JSTL库.如果您使用的是Apache Tomcat容器,请按照以下两个步骤 : 去;

步骤1 : 从 Apache Standard Taglib 下载二进制发行版并解压缩压缩文件.

第2步 : 要从 Jakarta Taglibs发行版中使用标准Taglib,只需将发行版'lib'目录中的JAR文件复制到应用程序的 webapps \ROOT \WEB-INF \ lib 目录.

要使用任何库,您必须包含< taglib>在每个使用该库的JSP顶部的指令.

JSTL标记的分类

JSTL标记可以根据其功能进行分类,在创建JSP页面时可以使用的以下JSTL标记库组 :

  • 核心标记

  • 格式化标签

  • SQL标签

  • XML标签

  • JSTL函数

核心标签

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

<%@ taglib prefix ="c"uri ="http ://java.sun.com/jsp/jstl/core"%>

下表列出了核心JSTL标签:

S.No.Tag & Description
1<c:out>

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

2<c:set >

Sets the result of an expression evaluation in a 'scope'

3<c:remove >

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

4<c:catch>

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

5<c:if>

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

6<c:choose>

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

7<c:when>

Subtag of <choose> that includes its body if its condition evalutes to 'true'.

8<c:otherwise >

Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluated to 'false'.

9<c:import>

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

10<c:forEach >

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

11<c:forTokens>

Iterates over tokens, separated by the supplied delimeters.

12<c:param>

Adds a parameter to a containing 'import' tag's URL.

13<c:redirect >

Redirects to a new URL.

14<c:url>

Creates a URL with optional query parameters

Formatting Tags

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

<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>

下表列出了格式化JSTL标记:

S.No.Tag & Description
1<fmt:formatNumber>

To render numerical value with specific precision or format.

2<fmt:parseNumber>

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

3<fmt:formatDate>

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

4<fmt:parseDate>

Parses the string representation of a date and/or time

5<fmt:bundle>

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

6<fmt:setLocale>

Stores the given locale in the locale configuration variable.

7<fmt:setBundle>

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

8<fmt:timeZone>

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

9<fmt:setTimeZone>

Stores the given time zone in the time zone configuration variable

10<fmt:message>

Displays an internationalized message.

11<fmt:requestEncoding>

Sets the request character encoding

SQL Tags

JSTL SQL标记库提供用于与关系数据库(RDBMS)(如Oracle,mySQL或Microsoft SQL Server)交互的标记。

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

<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>

下表列出了SQL JSTL标记:

S.No.Tag & Description
1<sql:setDataSource>

Creates a simple DataSource suitable only for prototyping

2<sql:query>

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

3<sql:update>

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

4<sql:param>

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

5<sql:dateParam>

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

6<sql:transaction >

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

XML tags

JSTL XML标记提供了一种以JSP为中心的方法来创建和操作XML文档。 以下是在JSP中包含JSTL XML库的语法。

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

<%@ taglib prefix = "x" 
   uri = "http://java.sun.com/jsp/jstl/xml" %>

在继续执行示例之前,您需要将以下两个与XML和XPath相关的库复制到<Tomcat安装目录\lib中:

  • XercesImpl.jar : 从https://www.apache.org/dist/xerces/j/下载

  • xalan.jar :从https://xml.apache.org/xalan-j/index.html下载

下表列出了各种JSTL函数:

S.No.Tag & Description
1<x:out>

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

2<x:parse>

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

3<x:set >

Sets a variable to the value of an XPath expression.

4<x:if >

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<x:forEach>

To loop over nodes in an XML document.

6<x:choose>

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

7<x:when >

Subtag of <choose> that includes its body if its expression evalutes to 'true'.

8<x:otherwise >

Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluates to 'false'.

9<x:transform >

Applies an XSL transformation on a XML document

10<x:param >

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

JSTL Functions

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

<%@ taglib prefix = "fn" 
   uri = "http://java.sun.com/jsp/jstl/functions" %>

下表列出了各种JSTL函数:

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

Tests if an input string contains the specified substring.

2fn:containsIgnoreCase()

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

3fn:endsWith()

Tests if an input string ends with the specified suffix.

4fn:escapeXml()

Escapes characters that can be interpreted as XML markup.

5fn:indexOf()

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

6fn:join()

Joins all elements of an array into a string.

7fn:length()

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

8fn:replace()

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

9fn:split()

Splits a string into an array of substrings.

10fn:startsWith()

Tests if an input string starts with the specified prefix.

11fn:substring()

Returns a subset of a string.

12fn:substringAfter()

Returns a subset of a string following a specific substring.

13fn:substringBefore()

Returns a subset of a string before a specific substring.

14fn:toLowerCase()

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

15fn:toUpperCase()

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

16fn:trim()

Removes white spaces from both ends of a string.