位置:首页 > Java技术 > JSP在线教程 > JSP标准标记库(JSTL)在线教程

JSP标准标记库(JSTL)在线教程

在JSP标准标记库(JSTL)是很有用的JSP标签的集合,它封装常见的许多JSP应用程序的核心功能。

JSTL具有如迭代和条件,标签为操纵XML文档,国际化标签,和SQL标签常见,结构任务的支持。它还提供了集成现有的自定义标签与JSTL标记框架。

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

  • 核心标签

  • 格式化标签

  • SQL标记

  • XML标签

  • JSTL功能

安装JSTL库:

如果您使用的是Apache Tomcat容器,那么按照下面两个简单的步骤:

  • Apache标准标签库下载二进制分发和解压的压缩文件。

  • 若要从Jakarta Taglibs中使用的标准标签库,只需在分布的'lib'目录复制JAR文件到您的应用程序的webappsROOTWEB-INFlib目录中。

使用任何库,必须包括<taglib>指令在每个JSP中使用该库的顶部。

核心标签:

标签的核心小组是最常用的JSTL标记。以下是语法包括JSTL core库在JSP:

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

有以下核心JSTL标签:

标签 描述
<c:out > Like <%= ... >, but for expressions.
<c:set > Sets the result of an expression evaluation in a 'scope'
<c:remove > Removes a scoped variable (from a particular scope, if specified).
<c:catch> Catches any Throwable that occurs in its body and optionally exposes it.
<c:if> Simple conditional tag which evalutes its body if the supplied condition is true.
<c:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<c:when> Subtag of <choose> that includes its body if its condition evalutes to 'true'.
<c:otherwise > Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'.
<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'.
<c:forEach > The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .
<c:forTokens> Iterates over tokens, separated by the supplied delimeters.
<c:param> Adds a parameter to a containing 'import' tag's URL.
<c:redirect > Redirects to a new URL.
<c:url> Creates a URL with optional query parameters

格式化标签:

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

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

以下是格式化的JSTL标签的列表:

标签 描述
<fmt:formatNumber> To render numerical value with specific precision or format.
<fmt:parseNumber> Parses the string representation of a number, currency, or percentage.
<fmt:formatDate> Formats a date and/or time using the supplied styles and pattern
<fmt:parseDate> Parses the string representation of a date and/or time
<fmt:bundle> Loads a resource bundle to be used by its tag body.
<fmt:setLocale> Stores the given locale in the locale configuration variable.
<fmt:setBundle> Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
<fmt:timeZone> Specifies the time zone for any time formatting or parsing actions nested in its body.
<fmt:setTimeZone> Stores the given time zone in the time zone configuration variable
<fmt:message> To display an internationalized message.
<fmt:requestEncoding> Sets the request character encoding

SQL标记:

JSTL的SQL标记库提供标签为关系型数据库(RDBMS),如Oracle,MySQL等,或Microsoft SQL Server进行交互。

以下是语法包括JSTL SQL库在JSP:

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

以下是SQL JSTL标签的列表:

标签 描述
<sql:setDataSource> Creates a simple DataSource suitable only for prototyping
<sql:query> Executes the SQL query defined in its body or through the sql attribute.
<sql:update> Executes the SQL update defined in its body or through the sql attribute.
<sql:param> Sets a parameter in an SQL statement to the specified value.
<sql:dateParam> Sets a parameter in an SQL statement to the specified java.util.Date value.
<sql:transaction > Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

XML标签:

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

JSTL的XML标记库有自定义标签用于处理XML数据进行交互。这包括解析XML,转换XML数据,流程和基于XPath表达式控制。

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

在继续之前的例子,你需要下面的两个XML和XPath相关的库文件复制到您的<Tomcat安装目录> lib中:

以下是XML的JSTL标签列表:

标签 描述
<x:out> Like <%= ... >, but for XPath expressions.
<x:parse> Use to parse XML data specified either via an attribute or in the tag body.
<x:set > Sets a variable to the value of an XPath expression.
<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.
<x:forEach> To loop over nodes in an XML document.
<x:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<x:when > Subtag of <choose> that includes its body if its expression evalutes to 'true'
<x:otherwise > Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
<x:transform > Applies an XSL transformation on a XML document
<x:param > Use along with the transform tag to set a parameter in the XSLT stylesheet

JSTL函数:

JSTL包含了一些标准函数,其中大部分是常用的字符串操作函数。以下是语法包括JSTL函数库在JSP:

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

下面是JSTL函数的列表:

Function 描述
fn:contains() Tests if an input string contains the specified substring.
fn:containsIgnoreCase() Tests if an input string contains the specified substring in a case insensitive way.
fn:endsWith() Tests if an input string ends with the specified suffix.
fn:escapeXml() Escapes characters that could be interpreted as XML markup.
fn:indexOf() Returns the index withing a string of the first occurrence of a specified substring.
fn:join() Joins all elements of an array into a string.
fn:length() Returns the number of items in a collection, or the number of characters in a string.
fn:replace() Returns a string resulting from replacing in an input string all occurrences with a given string.
fn:split() Splits a string into an array of substrings.
fn:startsWith() Tests if an input string starts with the specified prefix.
fn:substring() Returns a subset of a string.
fn:substringAfter() Returns a subset of a string following a specific substring.
fn:substringBefore() Returns a subset of a string before a specific substring.
fn:toLowerCase() Converts all of the characters of a string to lower case.
fn:toUpperCase() Converts all of the characters of a string to upper case.
fn:trim() Removes white spaces from both ends of a string.