Anda di halaman 1dari 4

JSP - STANDARD TAG LIBRARY (JSTL) TUTORIAL

http://www.tuto rialspo int.co m/jsp/jsp_standard_tag _library.htm


Co pyrig ht tuto rials po int.co m

T he JavaServer Pag es Standard T ag Library (JST L) is a collection of useful JSP tag s which encapsulates core functionality common to many JSP applications. JST L has support for common, structural tasks such as iteration and conditionals, tag s for manipulating XML documents, internationalization tag s, and SQL tag s. It also provides a framework for integ rating existing custom tag s with JST L tag s. T he JST L tag s can be classified, according to their functions, into following JST L tag library g roups that can be used when creating a JSP pag e: Core T ag s Formatting tag s SQ L tag s XML tag s J ST L Func tions

Install JSTL Library:


If you are using Apache T omcat container then follow the following two simple steps: Download the binary distribution from Apache Standard T ag lib and unpack the compressed file. T o use the Standard T ag lib from its Jakarta T ag libs distribution, simply copy the JAR files in the distribution's 'lib' directory to your application's webapps\ROOT \WEB-INF\lib directory. T o use any of the libraries, you must include a <tag lib> directive at the top of each JSP that uses the library.

Core Tag s:
T he core g roup of tag s are the most frequently used JST L tag s. Following is the syntax to include JST L Core library in your JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

T here are following Core JST L T ag s:

T ag <c:out > <c:set > <c:remove > <c:catch> <c:if> <c:choose> <c:when>

Desc ription Like <%= ... >, but for expressions. Sets the result of an expression evaluation in a 'scope' Removes a scoped variable (from a particular scope, if specified). Catches any T hrowable that occurs in its body and optionally exposes it. Simple conditional tag which evalutes its body if the supplied condition is true. Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> Subtag of <choose> that includes its body if its condition evalutes to 'true'.

<c:otherwise > <c:import> <c:forEach > <c:forT okens> <c:param> <c:redirect > <c:url>

Subtag of <choose> that follows <when> tag s and runs only if all of the prior conditions evaluated to 'false'. Retrieves an absolute or relative URL and exposes its contents to either the pag e, a String in 'var', or a Reader in 'varReader'. T he basic iteration tag , accepting many different collection types and supporting subsetting and other functionality . Iterates over tokens, separated by the supplied delimeters. Adds a parameter to a containing 'import' tag 's URL. Redirects to a new URL. Creates a URL with optional query parameters

Formatting tag s:
T he JST L formatting tag s are used to format and display text, the date, the time, and numbers for internationalized Web sites. Following is the syntax to include Formatting library in your JSP:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Following is the list of Formatting JST L T ag s:

T ag <fmt:formatNumber> <fmt:parseNumber> <fmt:formatDate> <fmt:parseDate> <fmt:bundle> <fmt:setLocale> <fmt:setBundle> <fmt:timeZ one> <fmt:setT imeZ one> <fmt:messag e> <fmt:requestEncoding >

Desc ription T o render numerical value with specific precision or format. Parses the string representation of a number, currency, or percentag e. Formats a date and/or time using the supplied styles and pattern Parses the string representation of a date and/or time Loads a resource bundle to be used by its tag body. Stores the g iven locale in the locale config uration variable. Loads a resource bundle and stores it in the named scoped variable or the bundle config uration variable. Specifies the time zone for any time formatting or parsing actions nested in its body. Stores the g iven time zone in the time zone config uration variable T o display an internationalized messag e. Sets the request character encoding

SQL tag s:
T he JST L SQL tag library provides tag s for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server. Following is the syntax to include JST L SQL library in your JSP:

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

Following is the list of SQL JST L T ag s:

T ag <sql:setDataSource> <sql:query> <sql:update> <sql:param> <sql:dateParam> <sql:transaction >

Desc ription Creates a simple DataSource suitable only for prototyping Executes the SQL query defined in its body or throug h the sql attribute. Executes the SQL update defined in its body or throug h the sql attribute. Sets a parameter in an SQL statement to the specified value. Sets a parameter in an SQL statement to the specified java.util.Date value. Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

XML tag s:
T he JST L XML tag s provide a JSP-centric way of creating and manipulating XML documents. Following is the syntax to include JST L XML library in your JSP. T he JST L XML tag library has custom tag s for interacting with XML data. T his includes parsing XML, transforming XML data, and flow control based on XPath expressions.
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

Before you proceed with the examples, you would need to copy following two XML and XPath related libraries into your <T omcat Installation Directory>\lib: Xerc esImpl.jar: Download it from http://www.apache.org /dist/xerces/j/ xalan.jar: Download it from http://xml.apache.org /xalan-j/index.html Following is the list of XML JST L T ag s:

T ag <x:out> <x:parse> <x:set > <x:if > <x:forEach> <x:choose> <x:when > <x:otherwise >

Desc ription Like <%= ... >, but for XPath expressions. Use to parse XML data specified either via an attribute or in the tag body. Sets a variable to the value of an XPath expression. Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ig nored. T o loop over nodes in an XML document. Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> Subtag of <choose> that includes its body if its expression evalutes to 'true' Subtag of <choose> that follows <when> tag s and runs only if all of the prior conditions evaluated to 'false'

<x:transform > <x:param >

Applies an XSL transformation on a XML document Use along with the transform tag to set a parameter in the XSLT stylesheet

JSTL Functions:
JST L includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JST L Functions library in your JSP:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Following is the list of JST L Functions:

Func tion fn:contains() fn:containsIg noreCase() fn:endsWith() fn:escapeXml() fn:indexOf() fn:join() fn:leng th() fn:replace() fn:split() fn:startsWith() fn:substring () fn:substring After() fn:substring Before() fn:toLowerCase() fn:toUpperCase() fn:trim()

Desc ription T ests if an input string contains the specified substring . T ests if an input string contains the specified substring in a case insensitive way. T ests if an input string ends with the specified suffix. Escapes characters that could be interpreted as XML markup. Returns the index withing a string of the first occurrence of a specified substring . Joins all elements of an array into a string . Returns the number of items in a collection, or the number of characters in a string . Returns a string resulting from replacing in an input string all occurrences with a g iven string . Splits a string into an array of substring s. T ests if an input string starts with the specified prefix. Returns a subset of a string . Returns a subset of a string following a specific substring . Returns a subset of a string before a specific substring . Converts all of the characters of a string to lower case. Converts all of the characters of a string to upper case. Removes white spaces from both ends of a string .

Anda mungkin juga menyukai