JSP Standard Tag Library (JSTL) is a collection of JSP tags that encapsulates the common core functions of JSP applications.
JSTL supports common, structured tasks, such as iteration, conditional judgment, XML document operations, internationalization tags, and SQL tags. In addition to these, it also provides a framework to use custom tags integrated with JSTL.
According to the functions provided by JSTL tags, they can be divided into 5 categories.
core tags
formatting tags
SQL tag
XML tag
JSTL functions
The steps to install the JSTL library on Apache Tomcat are as follows:
The binary package (jakarta-taglibs-standard-current.zip) downloaded from Apache's standard tag library. Download address: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
Download the jakarta-taglibs-standard-1.1.1.zip package and unzip it, and copy the two jar files under jakarta-taglibs-standard-1.1.1/lib/: standard.jar and jstl.jar files to /WEB-INF /lib/under.
Next we add the following configuration in the web.xml file:
…
<
jsp
-
config
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/fmt</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
fmt
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/fmt-rt</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
fmt
-
rt
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/core</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
c
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/core-rt</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
c
-
rt
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/sql</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
sql
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/sql-rt</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
sql
-
rt
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/x</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
x
.
tld
</
taglib
-
location
>
</
taglib
>
<taglib>
<
taglib
-
uri
>
http
:
//java.sun.com/jstl/x-rt</taglib-uri>
<
taglib
-
location
>
/WEB-INF/
x
-
rt
.
tld
</
taglib
-
location
>
</
taglib
>
</
jsp
-
config
>
…
To use any library, you must include the <taglib> tag in the header of every JSP file.
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" %>
Label | describe |
---|---|
<c:out> | Used to display data in JSP, like <%= ... > |
<c:set> | used to save data |
<c:remove> | for deleting data |
<c:catch> | Used to handle exceptions that cause errors and store error information |
<c:if> | It’s the same as the if we use in general programs |
<c:choose> | It is only used as the parent tag of <c:when> and <c:otherwise> |
<c:when> | The sub-tag of <c:choose> is used to determine whether the condition is true. |
<c:otherwise> | The sub-tag of <c:choose> is connected after the <c:when> tag and is executed when the <c:when> tag is judged to be false. |
<c:import> | Retrieve an absolute or relative URL and expose its contents to the page |
<c:forEach> | Basic iteration tag, accepting multiple collection types |
<c:forTokens> | Separate content based on specified delimiter and iterate output |
<c:param> | Used to pass parameters to included or redirected pages |
<c:redirect> | Redirect to a new URL. |
<c:url> | Use optional query parameters to create a URL |
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"
%>
Label | describe |
---|---|
<fmt:formatNumber> | Format a number using the specified format or precision |
<fmt:parseNumber> | Parse a string representing a number, currency, or percentage |
<fmt:formatDate> | Format date and time using specified style or pattern |
<fmt:parseDate> | Parse a string representing a date or time |
<fmt:bundle> | Bind resources |
<fmt:setLocale> | Designated area |
<fmt:setBundle> | Bind resources |
<fmt:timeZone> | Specify time zone |
<fmt:setTimeZone> | Specify time zone |
<fmt:message> | Display resource profile information |
<fmt:requestEncoding> | Set the character encoding of 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" %>
Label | describe |
---|---|
<sql:setDataSource> | Specify data source |
<sql:query> | Run SQL query statement |
<sql:update> | Run SQL update statement |
<sql:param> | Set the parameters 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 a shared database connection to run all statements as a transaction |
The JSTL XML tag library provides tags for creating and manipulating XML documents. The syntax for referencing an XML tag library is as follows:
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
Before using the xml tag, 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
Label | describe |
---|---|
<x:out> | Similar to <%= ... >, but only used in XPath expressions |
<x:parse> | Parse XML data |
<x:set> | Set XPath expression |
<x:if> | Determine the XPath expression. If it is true, execute the content in the ontology, otherwise skip the ontology. |
<x:forEach> | Iterate over nodes in XML document |
<x:choose> | Parent tags of <x:when> and <x:otherwise> |
<x:when> | The sub-tag of <x:choose> is used for conditional judgment. |
<x:otherwise> | The sub-tag of <x:choose> is executed when <x:when> is judged to be false. |
<x:transform> | Apply XSL transformations to XML documents |
<x:param> | Used together with <x:transform> to set XSL style sheets |
JSTL contains a series of standard functions, most of which are general-purpose 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 | describe |
---|---|
fn:contains() | Tests whether the input string contains the specified substring |
fn:containsIgnoreCase() | Tests whether the input string contains the specified substring, case-insensitive |
fn:endsWith() | Tests whether the input string ends with the specified suffix |
fn:escapeXml() | Skip characters that can be used as XML tags |
fn:indexOf() | Returns the position where the specified string appears in the input string |
fn:join() | Combine the elements in the array into a string and output it |
fn:length() | Returns the string length |
fn:replace() | Replaces the specified position in the input string with the specified string and returns |
fn:split() | Separate the string with the specified delimiter and then form an array of substrings and return |
fn:startsWith() | Tests whether the input string starts with the specified prefix |
fn:substring() | Returns a subset of a string |
fn:substringAfter() | Returns the subset of the string after the specified substring |
fn:substringBefore() | Returns the subset of the string before the specified substring |
fn:toLowerCase() | Convert characters in string to lowercase |
fn:toUpperCase() | Convert characters in a string to uppercase |
fn:trim() | Remove leading whitespace characters |