The JSTL tags can be classified, according to their functions, into following JSTL tag library groups that can be used when creating a JSP page:3. Core Tags.[r]
Trang 1CHUYÊN ĐỀ WEB
JSP - Standard Tag
Library (JSTL)
ThS.Nguyễn Hữu Thể
Trang 2JavaServer Pages Standard Tag Library (JSTL)
Collection of useful JSP tags which encapsulates core
functionality common to many JSP applications
The JSTL tags can be classified, according to their functions,
into following JSTL tag library groups that can be used when
creating a JSP page:
Core Tags
Formatting tags
SQL tags
XML tags
JSTL Functions
Trang 3Install JSTL Library
If you are using Apache Tomcat:
Download the binary distribution from Apache Standard
Taglib (http://tomcat.apache.org/taglibs/index.html) and
unpack the compressed file
Copy the JAR files in the distribution's 'lib' directory to your application's webapps\ROOT\WEB-INF\lib directory
To use any of the libraries, you must include a <taglib>
directive at the top of each JSP
Trang 4Core Tags
The core group of tags are the most frequently used JSTL tags Following is the syntax to include JSTL Core library in your JSP:
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
Trang 5Core Tags
<c:out > Like <%= >, but for expressions
<c:set > Sets the result of an expression evaluation in a 'scope'
<c:remove > Removes a scoped variable (from a particular scope, if
specified)
<c:catch> Catches any Throwable that occurs in its body and
optionally exposes it
<c:if> Simple conditional tag which evalutes its body if the
supplied condition is true
<c:choose>
Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by
<when> and <otherwise>
<c:when> Subtag of <choose> that includes its body if its
condition evalutes to 'true'
Trang 6Core Tags
<c:otherwise >
Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
<c:import>
Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a
Reader in 'varReader'
<c:forEach >
The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality
<c:forTokens> Iterates over tokens, separated by the supplied
delimeters
<c:param> Adds a parameter to a containing 'import' tag's URL
<c:redirect > Redirects to a new URL
<c:url> Creates a URL with optional query parameters
Trang 7JSTL Core <c:out> Tag
The <c:out> tag displays the result of an expression, similar to the way <%= %>
<%@ taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<html>
<body>
<c:out value="${'<tag> , &'}"/>
</body>
</html>
<tag> , &
Trang 8JSTL Core <c:set> Tag
value Information to save No body
target Name of the variable whose
property should be modified No None property Property to modify No None
var Name of the variable to store
scope Scope of variable to store
Trang 9JSTL Core <c:set> Tag
<%@ taglib uri ="http://java.sun.com/jsp/jstl/core"
prefix= "c" %>
< html >
< body >
<c:set var="salary" scope="session"
value="${2000*2}"/>
<c:out value="${salary}"/>
</ body >
</ html >
4000
Trang 10JSTL Core <c:if> Tag
Attribute Description Requir
ed Default test Condition to evaluate Yes None
var Name of the variable to store the
scope Scope of the variable to store the
condition's result No page
Trang 11JSTL Core <c:if> Tag
<%@ taglib uri ="http://java.sun.com/jsp/jstl/core"
prefix= "c" %>
< html >
< body >
< c:set var="salary" scope="session" value="${2000*2}"/>
< c:if test="${salary > 2000}">
< p >My salary is: < c:out value="${salary}"/></p>
</ c:if >
</ body >
</ html >
My salary is: 4000
Trang 12JSTL Core <c:forEach> Tag
items Information to loop over No None
begin Element to start with (0 = first
item, 1 = second item, ) No 0
end Element to end with (0 = first
item, 1 = second item, ) No
Last element step Process every step items No 1
var Name of the variable to
expose the current item No None varStatus Name of the variable to
expose the loop status No None
Trang 13JSTL Core <c:forEach> Tag
<%@ taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
< html >
< body >
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
</ body >
</ html >
Item 1 Item 2 Item 3 Item 4 Item 5
Trang 14JSTL Core <c:forToken> Tag
<%@ taglib uri ="http://java.sun.com/jsp/jstl/core"
prefix= "c" %>
< html >
< body >
< c:forTokens items="Zara,nuha,roshy" delims=","
var="name" >
< c:out value="${name}"/><p>
</ c:forTokens >
</ body >
</ html >
Attribute Description Required Default delims Characters to use as delimiters Yes None
Zara nuha roshy