JSP Standard Tag Library (JSTL)

Source: Internet
Author: User
Tags xpath xsl tld apache tomcat

The JSP standard Tag library (JSTL) is a collection of JSP tags that encapsulates the Common Core functionality of JSP applications.

JSTL supports common, structured tasks such as iterations, conditional judgments, XML document manipulation, internationalization tags, and SQL tags. In addition to these, it also provides a framework for using custom labels with integrated Jstl.

Depending on the functionality provided by the JSTL tag, it can be divided into 5 categories.

    • Core tags
    • Formatting labels
    • SQL tags
    • XML tags
    • JSTL function
JSTL Library Installation

Apache Tomcat installs the JSTL library steps as follows:

Download the binary package (Jakarta-taglibs-standard-current.zip) from the standard tag Library of Apache.

    • Official: 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 unzip it to jakarta-taglibs-standard-1.1.2/lib/ Two jar files under: Standard.jar and Jstl.jar files are copied to/web-inf/lib/.

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< span class= "tag" ></taglib-uri>    < Taglib-location>/web-inf/x-rt.tld</taglib-location>     </taglib>     </jsp-config></WEB-APP>   

With any library, you must include the <taglib> tag in the header of each JSP file.

Core tags

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 JSPs, like <%= ... >
<c:set> For saving data
<c:remove> Used to delete data
<c:catch> To handle the abnormal condition that caused the error, and to store the error message.
<c:if> Same as we do in the general procedure
<c:choose> Only as a parent tag for <c:when> and <c:otherwise>
<c:when> <c:choose> Sub-tags to determine if conditions are true
<c:otherwise> <c:choose> Sub-label, followed by the <c:when> tag, when the <c:when> tag is judged to be false when it is executed
<c:import> Retrieves an absolute or relative URL, and then exposes its contents to the page
<c:forEach> Base iteration tags, accepting multiple collection types
<c:forTokens> Separates content and iterates output according to the specified delimiter
<c:param> Used to pass parameters to a page that contains or redirects
<c:redirect> Redirect to a new URL.
<c:url> Use optional query parameters to create a URL
Formatting labels

The JSTL format label is used to format and output text, date, time, and number. The syntax for referencing the formatting tag library is as follows:

<%@ taglib prefix = "FMT"  Uri=%>         
label Description
<fmt:formatNumber> Formats a number using the specified format or precision
<fmt:parseNumber> Parse a string representing a number, currency, or percentage
<fmt:formatDate> Formats the date and time with the specified style or pattern
<fmt:parseDate> Parse a string that represents a 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 tags

The JSTL SQL Tag Library provides a label that interacts with a relational database (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" %>  
label Description
<sql:setDataSource> Specify the data source
<sql:query> Run the SQL query statement
<sql:update> To run the SQL UPDATE statement
<sql:param> To set a parameter in an SQL statement to a specified value
<sql:dateParam> Sets 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, and run all statements as a single transaction
XML tags

The JSTL XML tag library provides a label 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

      : http://www.apache.org/dist/xerces/j/

    • Xalan.jar

      : http://xml.apache.org/xalan-j/index.html

label Description
<x:out> Similar to <%=, but only for XPath expressions
<x:parse> Parsing XML Data
<x:set> To set an XPath expression
<x:if> Evaluates the XPath expression, if true, executes the contents of the ontology, otherwise skips the ontology
<x:forEach> Iterate over a node in an XML document
<x:choose> Parent tags for <x:when> and <x:otherwise>
<x:when> <x:choose> Sub-tags for conditional judgment
<x:otherwise> <x:choose> Sub-label, executed when <x:when> evaluates to False
<x:transform> To apply an XSL transformation to an XML document
<x:param> Work with <x:transform> to set up XSL style sheets
Jstl function

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 Description
Fn:contains () Tests whether the input string contains the specified substring
Fn:containsignorecase () Tests whether the input string contains the specified substring, is case insensitive
Fn:endswith () Tests whether the input string ends with the specified suffix
Fn:escapexml () Skipping characters that can be tagged as xml
Fn:indexof () Returns the position of the specified string where it appears in the input string
Fn:join () The elements in the array are composited into a string and then output
Fn:length () return string length
Fn:replace () Replaces the specified position in the input string with the specified string and returns
Fn:split () Separates the string with the specified delimiter and then composes a substring array and returns
Fn:startswith () Tests whether the input string starts with the specified prefix
Fn:substring () Returns a subset of a string
Fn:substringafter () Returns a subset of a string after a 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 () To capitalize characters in a string
Fn:trim () Remove the first white space character

JSP Standard Tag Library (JSTL)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.