The Core Tags

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Appendix A.  JSTL Reference


The core tags perform operations that form the foundation of the JSTL tag library. Using the core tags, you can set scoped variables, iterate over collections, and handle exceptions.

The <c:catch> Tag

The <c:catch> tag is used to allow JSP pages to handle exceptions that might be raised by any of the code contained in the body of the <c:catch> tag.

<c:catch var="e">   ... Program code that may throw an exception ... </c:catch> 

The <c:catch> tag has one attribute:

Attribute

Required

Purpose

var

Y

Specifies the scoped variable that should receive the exception.

The <c:choose> Tag

The <c:choose> tag is used to form the body of a mutual exclusion. The tags <c:when> and <c:otherwise> should be embedded inside the <c:choose> tag. When the <c:choose> block is reached, only one of the <c:when> tag blocks will be executed. If none of the <c:when> tags evaluates to true, then the <c:otherwise> tag, if present, will be executed. The <c:choose> block works much like the switch, case, and default statements in the Java programming language. The <c:choose> tag has no attributes.

<c:choose>  body content (<when> and <otherwise> subtags) </c:choose> 

The <c:forEach> Tag

The <c:forEach> tag is used to loop in several different ways. The most common use for this tag is to loop over a collection. It is also possible to make the <c:forEach> tag operate similar to the Java for statement. By including begin, end, and step attributes, you can specify that the <c:forEach> tag loop a certain number of times.

// Syntax 1: Iterate over a collection of objects <c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]> body content </c:forEach> // Syntax 2: Iterate a fixed number of times <c:forEach [var="varName"] [varStatus="varStatusNamev] begin="begin" end="end" [step="step"]> body content </c:forEach> 

The <c:forEach> tag has the following attributes:

Attribute

Required

Purpose

begin

N

If items is specified, iteration begins at this item. If items is not specified, iteration begins with the index at this value.

end

N

If items is specified, iteration ends at this item. If items is not specified, iteration ends with the index at this value.

items

N

A collection of items to iterate.

step

N

The amount added to the index through each iteration.

var

N

Holds the current item in the iteration.

varStatus

N

Allows you to specify a javax.servlet.jsp.jstl.core.LoopTagStatus object to give you information about the iteration as it processes.

The <c:forTokens> Tag

The <c:forTokens> tag works much like the <c:forEach> tag, except that the <c:forTokens> tag iterates over a delineated string rather than a collection. You are allowed to specify a delineator for the string.

<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]> body content </c:forEach> 

The <c:forTokens> tag has these attributes:

Attribute

Required

Purpose

begin

N

Iteration will begin at this token. The first token has an index of 0.

delims

N

The delimiters used to parse with.

end

N

Iteration will end at this token.

items

N

A string of tokens to iterate through.

step

N

The amount added to the index through each iteration.

var

N

Holds the current item in the iteration.

varStatus

N

Allows you to specify a javax.servlet.jsp.jstl.core.LoopTagStatus object to give you information about the iteration as it processes.

The <c:if> Tag

The <c:if> tag allows you to conditionally execute a block of tags if the condition specified in the <c:if> tag is met. It is important to note that JSTL does not include an else tag. If you need to perform an if statement with an else, you must use a <c:choose> tag with a <c:otherwise> tag.

// <!  Syntax 1: Without body content  >   <c:if test="testCondition"   var="varName" [scope="{page|request|session|application}"]/> // <!  Syntax 2: With body content  >   <c:if test="testCondition"   [var="varName"] [scope="{page|request|session|application}"]>   body content   </c:if> 

The <c:if> tag has the following attributes:

Attribute

Required

Purpose

test

Y

Specifies the test that is to be performed.

scope

N

Specifies the scope; the default is page.

var

N

Specifies the scoped variable that should receive the result of the compare.

The <c:import> Tag

The <c:import> tag is used to read data from an external URL address. When the <c:import> tag is executed, the contents of the specified URL are downloaded and stored in a string specified in the <c:import> tag. This tag can be useful for retrieving XML data from Web addresses.

// Syntax 1: Resource content inlined or exported as a String object   <c:import url="url" [context="context"]   [var="varName"] [scope="{page|request|session|application}"]   [charEncoding="charEncoding"]>   optional body content for <c:param> subtags   </c:import> // Syntax 2: Resource content exported as a Reader object   <c:import url="url" [context="context"]   varReader="varReaderName"   [charEncoding="charEncoding"]>   body content where varReader is consumed by another action   </c:import> 

The <c:import> tag has the following attributes:

Attribute

Required

Purpose

charEncoding

N

Allows you to specify the character encoding (for example, UTF16).

context

N

Specifies the base URL that will be used to resolve a relative URL given by the url attribute.

scope

N

Specifies the scope for the scoped variable referenced by the var attribute.

url

Y

Specifies the URL from which the import is to take place.

var

N

Specifies a variable that will receive the output from the specified URL.

varReader

N

A Reader object to output to.

The <c:otherwise> Tag

The <c:otherwise> tag works much like the default statement in Java. The <c:otherwise> tag can be used only inside a <c:choose> tag's body. Use the <c:otherwise> tag to specify a block of code that is to be executed if none of the <c:when> tags (which are also contained inside the body of the <c:choose> tag) are executed. The <c:otherwise> tag has no attributes.

<c:otherwise>   conditional block </c:otherwise> 

The <c:out> Tag

The <c:out> tag is used to output data to the current page. Using this tag, you can display the results of variables and other expressions.

// Syntax 1: Without a body <c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"] /> // Syntax 2: With a body <c:out value="value" [escapeXml="{true|false}"]> default value </c:out> 

The <c:out> tag has the following attributes:

Attribute

Required

Purpose

default

N

Specifies the value that will be displayed if the specified value evaluates to null.

escapeXml

N

Uses a value of true to escape any special characters, or false to display text as is. The default is true.

value

Y

The value that is to be displayed.

The <c:param> Tag

The <c:param> tag is used to specify a parameter to the <c:redirect>, <c:url>, and <c:import> tags. These parameters will be appended to the URL as URL-based parameters.

// Syntax 1: Parameter value specified in attribute value <c:param name="name" value="value"/> // Syntax 2: Parameter value specified in the body content <c:param name="name"> parameter value </c:param> 

The <c:param> tag has the following attributes:

Attribute

Required

Purpose

name

Y

Specifies the name of this parameter.

value

N

Specifies the value of this parameter.

The <c:redirect> Tag

The <c:redirect> tag is used to completely move the Web user to another page. This allows you to redirect the user before the current page is displayed. This can be useful for many purposes, such as redirecting users away from a page to which they do not have access. If the <c:redirect> tag is used, no other processing will occur on the page.

// Syntax 1: Without body content <c:redirect url="value"/> // Syntax 2: With body content to specify query string parameters <c:redirect url="value"/> <c:param> subtags </c:redirect> 

The <c:redirect> tag has the following attributes:

Attribute

Required

Purpose

context

N

Specifies the base URL that will be used to resolve a relative URL given by the url attribute.

url

Y

Specifies the URL that the user is taken to.

The <c:remove> Tag

The <c:remove> tag is used to delete a scoped variable. Scoped variables are created by <c:set> and other tags. The <c:remove> tag allows you to reclaim the space occupied by the specified scoped variable.

<c:remove [scope="{page|request|session|application}"] var="var"/> 

The <c:remove> tag has the following attributes:

Attribute

Required

Purpose

scope

N

Specifies the scope of the var variable; defaults to page.

var

Y

Specifies the scoped variable that is to be removed.

The <c:set> Tag

The <c:set> tag is used to assign the specified expression to the specified scoped variable. This allows the <c:set> tag to be used to specify the values of scoped variables. Once a scoped variable is no longer needed, you can remove it by calling the <c:remove> tag.

// Syntax 1: Set using value attribute <c:set value="value" var="varName" [scope="{page|request|session|application}"]/> // Syntax 2: Set the value of a scoped variable using body content <c:set var="varName" [scope="{page|request|session|application}"]> body content </c:set> // Syntax 3: Set the value of a target object using value attribute <c:set value="value" target="target" property="propertyName"/> // Syntax 4: Set the value of a target object using body text <c:set target="target" property="propertyName"> body content </c:set> 

The <c:set> tag has the following attributes:

Attribute

Required

Purpose

property

N

If target specifies an object, then property specifies the property.

scope

N

Specifies the scope of the var variable; defaults to page.

target

N

An object that will have one of its properties set.

value

Y

Specifies the value that is to be set to the scoped variable var.

var

Y

Specifies the scoped variable that is to be set.

The <c:url> Tag

The <c:url> tag is used to construct a URL from a specified context URL and optional parameters (specified using the <c:param> tag).

// Syntax 1: Without body content   <c:url url="value"   [var="varName"] [scope="{page|request|session|application}"]/> // Syntax 2: With body content to specify query string parameters   <c:url url="value"   [var="varName"] [scope="{page|request|session|application}"]>   <c:param> subtags </c:url> 

The <c:url> tag has the following parameters:

Attribute

Required

Purpose

context

N

Specifies the base URL that will be used to resolve a relative URL given by the value attribute.

scope

N

Specifies the scope for the scoped variable referenced by the var attribute. The default is page.

value

Y

Specifies the URL from which the import is to take place.

var

N

Specifies a variable that will receive the output from the specified URL.

The <c:when> Tag

The <c:when> tag is used as part of a <c:choose> block. The <c:choose> tag will be examined until one <c:when> tag is found that contains a test that is satisfied.

<c:when test="testCondition"> body content </c:when> 

The <c:when> tag has one attribute:

Attribute

Required

Purpose

test

Y

Specifies the test that is to be performed.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/app01lev1sec1]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net