WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP Standard Tag Library (JSTL)


The JSP Standard Tag Library (JSTL) is a collection of JSP tags that encapsulate the common core functionality of JSP applications.

JSTL supports common, structured tasks such as iteration, conditional evaluation, XML document manipulation, internationalization tags, and SQL tags. In addition to these, it also provides a framework to use custom tags that integrate with JSTL.

According to the functions provided by the JSTL tags, they can be divided into five categories.

  • Core Tags
  • Format tags
  • SQL tags
  • XML tags
  • JSTL function

JSTL library installation

The steps for Apache Tomcat to install the JSTL library are as follows:

A binary package (jakarta-taglibs-standard-current.zip) downloaded from Apache's standard tag library.

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

Copy the tld file you need to import under tld to the 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>

Use any library,You have to be at each JSP The header in the file contains <taglib>label.


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" %>
Label 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 exception conditions that generate errors and store error information
<c:if> Same as the if we used in the general program
<c:choose> Only as a matter of its own<c:when>with<c:otherwise>Parent tag
<c:when> <c:choose>Sub-tab to determine if the condition is true
<c:otherwise> <c:choose>Sub-label, followed by<c:when>After the label, when<c:when>Executed when the tag is judged to be 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 content and iterate output based on the specified separators
<c:param> Used to pass parameters to a page that contains or redirects
<c:redirect> Redirect to a new URL.
<c:url> Create a URL using optional query parameters

Formatted label

JSTL formatting tags are used to format and output text, date, time, and numbers. The syntax for referencing a formatted tag library is as follows:

<%@ taglib prefix="fmt" 
           uri="http://java.sun.com/jsp/jstl/fmt" %>
Label Description
<fmt:formatNumber> Format numbers using the specified format or precision
<fmt:parseNumber> Parse a string representing a number, currency, or percentage
<fmt:formatDate> Format the date and time using the specified style or mode
<fmt:parseDate> Parse a string representing the date or time
<fmt:bundle> Binding resources
<fmt:setLocale> Designated area
<fmt:setBundle> Binding resources
<fmt:timeZone> Specify time zone
<fmt:setTimeZone> Specify time zone
<fmt:message> Display resource profile 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, etc.). The syntax for referencing the SQL tag library is as follows:

<%@ taglib prefix="sql" 
           uri="http://java.sun.com/jsp/jstl/sql" %>
Label Description
<sql:setDataSource> Specify data source
<sql:query> Run the SQL query statement
<sql:update> Run the 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, running all statements as a single transaction

XML Label

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 relevant packages for XML and XPath to you.of<Tomcat installation manual>\libunder:

label description
<x:out> versus<%= ... >,similar,But only for XPath expressions
<x:parse> Parsing XML data
<x:set> Set the XPath expression
<x:if> Determine the XPath expression, if true, execute the content in the ontology, otherwise skip the ontology
<x:forEach> Iterating over nodes in an XML document
<x:choose> <x:when>with<x:otherwise>Parent tag
<x:when> <x:choose>Sub-tab for conditional determination
<x:otherwise> <x:choose>Child label, when<x:when>Executed when it is judged to be false
<x:transform> Apply XSL transformations to XML documents
<x:param> versus<x:transform>Used together to set up an XSL style sheet

JSTL function

JSTL contains a set of standard functions, most of which are generic string handlers. The syntax for referencing the JSTL 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() Tests if the input string contains the specified substring, which is not case sensitive
fn:endsWith() Test if the input string ends with the specified suffix
fn:escapeXml() Skip characters that can be marked as XML
fn:indexOf() Returns the position of the specified string in the input string
fn:join() Combine the elements in the array into a string and output
fn:length() Return string length
fn:replace() Replace the specified position in the input string with the specified string and return
fn:split() Separate strings with the specified delimiter and form an array of substrings and return
fn:startsWith() Test if the input string starts with the specified prefix
fn:substring() Return a subset of the string
fn:substringAfter() Returns a subset of the string after the specified substring
fn:substringBefore() Returns a subset of the string 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 the leading and trailing whitespace