JSP standard tag Library (JSTL), jspjstl
JSP standard tag Library (JSTL)
The JSP standard tag Library (JSTL) is a collection of JSP tags, which encapsulates common core functions of JSP applications.
JSTL supports common and structured tasks, such as iteration, condition judgment, XML document operations, international tags, and SQL tags. In addition, it also provides a framework to use custom labels integrated with JSTL.
Based on the functions provided by the JSTL tag, it can be divided into five categories.
- Core tag
- Format tags
- SQL tag
- XML tag
- JSTL Functions
JSTL library Installation
Follow these steps to install the JSTL library in Apache Tomcat:
Download the Binary Package (jakarta-taglibs-standard-current.zip) from the apachestandard tag library ).
- Http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
- This site: jakarta-taglibs-standard-1.1.2.zip
Download the jakarta-taglibs-standard-1.1.2.zip package and decompress it. Copy the two jar files: standard. jar and jstl. jar files under jakarta-taglibs-standard-1.1.2/lib/to/WEB-INF/lib.
Next, add the following configuration in 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/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></web-app>
To use any library, you must include the <taglib> label in the header of each JSP file.
Core tag
The core tag is the most commonly used JSTL tag. 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, just like <% =...> |
<C: set> |
Used to save data |
<C: remove> |
Used to delete data |
<C: catch> |
Used to handle exceptions that generate errors and store error information |
<C: if> |
Same as if in general programs |
<C: choose> |
It is used only as the parent label of <c: when> and <c: otherwise>. |
<C: when> |
<C: choose> sub-tag used to determine whether a condition is true |
<C: otherwise> |
The sub-tag of <c: choose> is executed when the <c: when> label is set to false. |
<C: import> |
Retrieve an absolute or relative URL and expose its content to the page |
<C: forEach> |
Basic iteration tag, accepting multiple Collection types |
<C: forTokens> |
Separate the content based on the specified separator and iterate the output |
<C: param> |
Used to pass parameters to pages that contain or redirect |
<C: redirect> |
Redirects to a new URL. |
<C: url> |
Use optional query parameters to create a URL |
Format tags
The JSTL format label is used to format and output text, date, time, and number. Syntax for referencing and formatting the tag library is as follows:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Tag |
Description |
<Fmt: formatNumber> |
Format a number with the specified format or precision |
<Fmt: parseNumber> |
Parses a string that represents a number, currency, or percentage. |
<Fmt: formatDate> |
Format the date and time with the specified style or Mode |
<Fmt: parseDate> |
Parses a string representing the date or time |
<Fmt: bundle> |
Bind Resources |
<Fmt: setLocale> |
Specified Region |
<Fmt: setBundle> |
Bind Resources |
<Fmt: timeZone> |
Specified Time Zone |
<Fmt: setTimeZone> |
Specified Time Zone |
<Fmt: message> |
Display resource configuration file information |
<Fmt: requestEncoding> |
Set the character encoding of the request |
SQL tag
The jstl SQL tag Library provides tags for interacting with relational databases (Oracle, MySQL, SQL Server, and so on. 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> |
Data Source |
<SQL: query> |
Run SQL query statements |
<SQL: update> |
Run SQL update statements |
<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 value of the specified java. util. Date object. |
<SQL: transaction> |
Provides nested database behavior elements in a shared database connection to run all statements in the form of one transaction. |
XML tag
The jstl xml tag Library provides tags for creating and operating 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 the xml tag, you must copy the XML and XPath packages to your <Tomcat installation directory> \ lib:
Tag |
Description |
<X: out> |
Similar to <% =...>, but only used for XPath expressions |
<X: parse> |
Parse XML data |
<X: set> |
Set an XPath expression |
<X: if> |
Judge the XPath expression. If it is true, the content in the body is executed. Otherwise, the body is skipped. |
<X: forEach> |
Iterate nodes in XML documents |
<X: choose> |
<X: when> and <x: otherwise> parent labels |
<X: when> |
The sub-tag of <x: choose> for condition judgment. |
<X: otherwise> |
The sub-tag of <x: choose> is executed when <x: when> is set to false. |
<X: transform> |
Apply XSL conversion in XML documents |
<X: param> |
Used Together with <x: transform> to set the XSL style sheet |
JSTL Functions
JSTL contains a series of standard functions, most of which are common 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 whether the input string contains the specified substring. |
Fn: containsIgnoreCase () |
Test whether the input string contains the specified substring. It is case insensitive. |
Fn: endsWith () |
Test whether the input string ends with the specified suffix. |
Fn: escapeXml () |
Skip the characters that can be marked as XML characters |
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 then output |
Fn: length () |
Returns the string length. |
Fn: replace () |
Replace the specified position in the input string with the specified string and then return |
Fn: split () |
Separate the string with the specified separator and form a substring array and return |
Fn: startsWith () |
Test whether the input string starts with the specified prefix. |
Fn: substring () |
Returns the subset of a string. |
Fn: substringAfter () |
Returns the subset of a string after the specified substring. |
Fn: substringBefore () |
Returns the subset of a string before the specified substring. |
Fn: toLowerCase () |
Converts characters in a string to lowercase letters. |
Fn: toUpperCase () |
Converts the characters in the string to uppercase. |
Fn: trim () |
Remove the leading blank character |