English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The JSP Standard Tag Library (JSTL) is a collection of JSP tags that encapsulate the common core functions of JSP applications.
JSTL supports general, structured tasks such as iteration, conditional judgment, XML document operations, internationalization tags, and SQL tags. In addition to these, it also provides a framework for using custom tags integrated with JSTL.
According to the functions provided by the JSTL tags, they can be divided into5categories.
Core tags
Formatting Tag
SQL tags
XML Tags
JSTL functions
The steps to install the JSTL library for Apache Tomcat are as follows:
The binary package downloaded from the Apache standard tag library (jakarta-taglibs-standard-current.zip).
Official download address:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/1.1.2.zip
download jakarta-taglibs-standard-1.1.2.zip package and unzip, then copy jakarta-taglibs-standard-1.1.2/lib/ under the two jar files:standard.jar and jstl.jar file to /WEB-INF/lib/ below.
Copy the tld files that need to be imported from the tld under WEB-INF directory.
Next, we add the following configuration to the web.xml file:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri> <taglib-location>/WEB-INF/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/x-rt.tld</taglib-location> </taglib> </jsp-config> </web-app>
For any library, you must include the <taglib> tag in the header of each JSP file.
The core tags are the most commonly used JSTL tags. The syntax for referencing the core tag library is as follows:
<%@ taglib prefix="c" uri="http:"}//java.sun.com/jsp/jstl/core" %>
Tag | Description |
---|---|
<c:out> | Used to display data in JSP, similar to <%= ... %> |
<c:set> | Used to save data |
<c:remove> | Used to delete data |
<c:catch> | Used to handle exceptions that occur and store error information |
<c:if> | Similar to the if we use in general programs |
<c:choose> | Used only as the parent tag of <c:when> and <c:otherwise> |
<c:when> | A subtag of <c:choose>, used to determine if a condition is true |
<c:otherwise> | A subtag of <c:choose>, executed after the <c:when> tag when the <c:when> tag is evaluated as false |
<c:import> | Retrieve an absolute or relative URL and then expose its content to the page |
<c:forEach> | Basic iteration tag, accepts multiple collection types |
<c:forTokens> | Split content by a specified delimiter and iterate to output |
<c:param> | Used to pass parameters to pages that include or redirect |
<c:redirect> | Redirect to a new URL. |
<c:url> | Create a URL using optional query parameters |
JSTL formatting tags are used to format and output text, dates, times, and numbers. The syntax for referencing the formatting tag library is as follows:
<%@ taglib prefix="fmt" %> uri="http://java.sun.com/jsp/jstl/fmt" %>
Tag | Description |
---|---|
<fmt:formatNumber> | Format a number using a specified format or precision |
<fmt:parseNumber> | Parse a string representing a number, currency, or percentage |
<fmt:formatDate> | Format a date and time using a specified style or pattern |
<fmt:parseDate> | Parse a string representing a date or time |
<fmt:bundle> | Bind a resource |
<fmt:setLocale> | Specify the region |
<fmt:setBundle> | Bind a resource |
<fmt:timeZone> | Specify the time zone |
<fmt:setTimeZone> | Specify the time zone |
<fmt:message> | Display information about the resource configuration file |
<fmt:requestEncoding> | Set the character encoding of the request |
The JSTL SQL tag library provides tags for interacting with relational databases (Oracle, MySQL, SQL Server, etc.). The syntax for referencing the SQL tag library is as follows:
<%@ taglib prefix="sql"} uri="http://java.sun.com/jsp/jstl/sql" %>
Tag | Description |
---|---|
<sql:setDataSource> | Specify the data source |
<sql:query> | Run SQL query statements |
<sql:update> | Run SQL update statements |
<sql:param> | Set the parameter in the SQL statement to the specified value |
<sql:dateParam> | Set the date parameter in the SQL statement to the specified java.util.Date object value |
<sql:transaction> | Provide nested database behavior elements in shared database connections, running all statements as a single transaction |
The JSTL XML tag library provides tags for creating and manipulating XML documents. The syntax for referencing the XML tag library is as follows:
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
Before using xml tags, you must copy the XML and XPath related packages to your <Tomcat Installation Directory>\lib:
XercesImpl.jar
Download address: http://www.apache.org/dist/xerces/j/
xalan.jar
Download address: http://xml.apache.org/xalan-j/index.html
Tag | Description |
---|---|
<x:out> | Similar to <%= ... >, but only used for XPath expressions |
<x:parse> | Parse XML data |
<x:set> | Set the XPath expression |
<x:if> | Evaluate the XPath expression, if true, execute the content within, otherwise skip the content |
<x:forEach> | Iterate over nodes in the XML document |
<x:choose> | The parent tag of <x:when> and <x:otherwise> |
<x:when> | A child tag of <x:choose>, used for conditional judgment |
<x:otherwise> | A child tag of <x:choose>, it is executed when <x:when> evaluates to false |
<x:transform> | Apply the XSL transformation to the XML document |
<x:param> | Used together with <x:transform>, it is used to set the XSL style sheet |
JSTL includes a set of standard functions, most of which are general string processing functions. The syntax for referencing the JSTL function library is as follows:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Function | Description |
---|---|
fn:contains() | Test if the input string contains the specified substring |
fn:containsIgnoreCase() | Test if the input string contains the specified substring, case-insensitive |
fn:endsWith() | Test if the input string ends with the specified suffix |
fn:escapeXml() | Skip characters that can be XML tags |
fn:indexOf() | Return the position of the specified string in the input string |
fn:join() | Concatenate the elements of the array into a string and output |
fn:length() | Return the length of the string |
fn:replace() | Replace the specified position in the input string with the specified string and return |
fn:split() | Split the input string into an array of substrings using the specified delimiter and return |
fn:startsWith() | Test if the input string starts with the specified prefix |
fn:substring() | Return a substring of the string |
fn:substringAfter() | Return the substring after the specified substring |
fn:substringBefore() | Return the substring before the specified substring |
fn:toLowerCase() | Convert characters in a string to lowercase |
fn:toUpperCase() | Convert characters in a string to uppercase |
fn:trim() | Remove leading and trailing whitespace |