Latest web development tutorials

JSP Standard Tag Library (JSTL)

JSP Standard Tag Library (JSTL) is a collection of JSP tags, which encapsulates core functionality common JSP applications.

JSTL support for generic, structured tasks such as iteration, conditional, XML document manipulation, internationalization tags, SQL tags. In addition to these, it provides a framework to use the JSTL custom tag integration.

According to the functionality provided by JSTL tags, it can be divided into five categories.

  • Core tags
  • Formatting tags
  • SQL tag
  • XML tags
  • JSTL function

JSTL library installation

Apache Tomcat installation JSTL library as follows:

Downloaded from the Apache standard tag library binary package (jakarta-taglibs-standard-current.zip).

Download jakarta-taglibs-standard-1.1.2.zip pack and unpack the two jar files jakarta-taglibs-standard-1.1.2 / lib / under: Copy standard.jar and jstl.jar files to / WEB-INF / lib / under.

Next, we 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>

Use any library, you must head in each JSP file contains the <taglib> tag.


Core tags

Core tags are the most commonly used JSTL tags. Core tag library reference syntax is as follows:

<%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %>
label description
<c: out> For displaying data in a JSP, like <% = ...>
<c: set> For holding data
<c: remove> For deleting data
<c: catch> To handle error exception condition, and an error message store
<c: if> And if we are in the same general procedure used
<c: choose> Itself only as <c: when> and <c: otherwise> parent tag
<c: when> <C: choose> sub-tab is used to determine whether the conditions established
<c: otherwise> <C: choose> sub-tab, then in <c: when> tag after, when <c: when> tag is judged as false is executed
<c: import> Retrieving an absolute or relative URL, and then exposed to the contents page
<c: forEach> Iterative label basis, accept a variety of collection types
<c: forTokens> According to the specified delimiter to separate content and iterative output
<c: param> It used to contain or redirect the page to pass parameters
<c: redirect> Redirected to a new URL.
<c: url> Use the optional query parameters to create a URL

Formatting tags

JSTL formatting tags to format and output text, date, time, number. Formatting tag library reference syntax is as follows:

<%@ taglib prefix="fmt" 
           uri="http://java.sun.com/jsp/jstl/fmt" %>
label description
<fmt: formatNumber> Using the specified format or precision digital format
<fmt: parseNumber> Resolve a representative of the number, currency, or percentage of a string
<fmt: formatDate> Using the specified style or pattern format date and time
<fmt: parseDate> Parsing a string representing the date or time
<fmt: bundle> Binding Resources
<fmt: setLocale> Region
<fmt: setBundle> Binding Resources
<fmt: timeZone> Specify the time zone
<fmt: setTimeZone> Specify the time zone
<fmt: message> Displays the resource profile information
<fmt: requestEncoding> Set the character encoding of the request

SQL tag

JSTL SQL tag library provides a relational database (Oracle, MySQL, SQL Server, etc.) interact label. SQL syntax reference tag library as follows:

<%@ taglib prefix="sql" 
           uri="http://java.sun.com/jsp/jstl/sql" %>
label description
<sql: setDataSource> Specify the data source
<sql: query> Run SQL query
<sql: update> Run SQL update statement
<sql: param> The SQL statement parameter to the specified value
<sql: dateParam> The SQL statement to the specified date parameter java.util.Date object value
<sql: transaction> Provided nested in a shared database connection database behavioral elements, all statements in the form of a transaction to run

XML tags

JSTL XML tag library provides tags to create and manipulate XML documents. XML tag library reference syntax is as follows:

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

Before using xml tag, you have to XML and XPath related packages copied to your <Tomcat installation directory> \ under lib:

label description
<x: out> And <% = ...>, similar, but only for XPath expressions
<x: parse> Parsing XML data
<x: set> Setting XPath expression
<x: if> Analyzing XPath expression, if it is true, then the body of the content or Skip body
<x: forEach> Iterative XML document nodes
<x: choose> <X: when> and <x: otherwise> parent tag
<x: when> <X: choose> child tag to be evaluated
<x: otherwise> <X: choose> child tag when <x: when> judged as false when executed
<x: transform> The XSL transformations in an XML document
<x: param> And <x: transform> are used together to set the XSL style sheet

JSTL function

JSTL includes a series of standard functions, most of which are generic string handling functions. JSTL library reference syntax 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, case-insensitive
fn: endsWith () Test whether the input string ends with the specified suffix
fn: escapeXml () Skip as XML markup characters
fn: indexOf () Returns the position of the specified string that appears in the input string
fn: join () The synthesis of elements in the array and outputs a string
fn: length () Returns the string length
fn: replace () The location specified in the input string to replace the specified string and then returns
fn: split () The strings are separated by the specified separator and then the formation of a sub-string array and returns
fn: startsWith () Test whether the input string starts with the specified prefix
fn: substring () Returns a subset of a string
fn: substringAfter () Returns a string in the subset of the specified substring after
fn: substringBefore () Returns a string subset before the specified substring
fn: toLowerCase () The string of characters to lowercase
fn: toUpperCase () The characters in a string to uppercase
fn: trim () Remove the first whitespace