11.3 Conditional Actions

   

JSTL provides four actions that let you handle simple conditions or mutually exclusive conditions. Simple conditions execute some code based on whether a single condition is true, whereas mutually exclusive conditions execute some code based on whether one of many conditions is true. The JSTL conditional actions are listed in Table 11.11.

Table 11.11. Conditional Actions

Action

Description

<c:if>

Evaluates a boolean expression; if the expression is true , <c:if> evaluates its body content, if any. You can also store the result of the boolean expression in a scoped variable.

<c:choose>

The outermost action for mutually exclusive conditions. This action can only contain <c:when> actions and an optional <c: otherwise > action, in that order.

<c:when>

One or more <c:when> actions can be nested in a <c:choose> action. The body content of the first <c:when> action whose test attribute value evaluates to true is evaluated.

<c:otherwise>

One (and only one) <c:otherwise> action can reside ”as the last action ”in a <c:choose> action. The <c:otherwise> action represents a default in a switch statement.

One class ” ConditionalTagSupport ”is exposed for conditional custom actions.

JSTL Conditional Actions

<c:if>

Performs a simple conditional test

Syntax: [5]

[5] Items in brackets are optional.

Syntax #1: Without a body, stores the test result in a scoped variable

 <c:if test var [scope]/> 

Syntax #2: With a body that is evaluated if the test condition is true

 <c:if test [var] [scope]>  body content  </c:if> 

Description:

You can use <c:if> to do two things: conditionally execute some code contained in the body of the action and store the boolean result of the test condition in a scoped variable. You can do both of those things simultaneously with syntax 2.

Attributes:

Attribute [a]

Type

Description

test

boolean

A test condition.

var

String

The name of a scoped variable that references the boolean result of the value of the test attribute.

scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

[a] static dynamic

Constraints and Error Handling:

  • If you specify the scope attribute, you must also specify the var attribute.

In a Nutshell:

The <c:if> action evaluates a boolean expression specified with the test attribute; if that expression is true and the <c:if> action has a body, the body is evaluated; otherwise it is ignored. If you specify the var attribute, <c:if> will store the result of the boolean expression in a scoped variable. You can also use <c:if> without a body, as illustrated by syntax 1, to store the result of a boolean expression in scoped variable; presumably, that scoped variable is referenced elsewhere to determine whether some functionality is performed.

<c:choose>

Encapsulates a mutually exclusive condition

Syntax:

 <c:choose>  nested <c:when> actions and an optional <c:otherwise> action  </c:choose> 

Description:

The body of a <c:choose> action can contain one or more <c:when> actions and an optional <c:otherwise> action. The body content of the first <c:when> action whose condition evaluates to true is evaluated; otherwise, the body of the <c:otherwise> action, if present, is evaluated.

Attributes: none

Constraints and Error Handling:

  • The body of a <c:choose> action can only contain whitespace, one or more <c:when> actions, and an optional <c:otherwise> action. If present, the <c:otherwise> action must be the last action nested in the <c:choose> action.

In a Nutshell:

The <c:choose> action is used in conjunction with <c:when> and <c:otherwise> to emulate if/else and switch statement constructs. [6]

[6] See "Conditional Actions" on page 127 for more information about implementing if/then statements and switch constructs.

<c:when>

An alternative in a <c:choose> action

Syntax:

 <c:when test>  body content  </c:when> 

Description:

A <c:when> action can only exist in the body of a <c:choose> action. The body content of the first <c:when> action whose test condition ”specified with the test attribute ”evaluates to true is evaluated.

Attributes:

Attribute [a]

Type

Description

test

boolean

A test condition.

[a] static dynamic

Constraints and Error Handling:

  • <c:when> actions can only exist in the body of a <c:choose> action.

  • <c:when> actions must come before the <c:otherwise> action, if present, in the same <c:choose> action.

In a Nutshell:

The <c:when> action is similar to the <c:if> action; both actions have a test conditions specified with a test attribute. The difference is that <c:when> actions must appear within a <c:choose> action and represent one of several (two or more) alternatives.

<c:otherwise>

The default alternative in a <c:choose> action

Syntax:

 <c:otherwise>  body content  </c:otherwise> 

Description:

The <c:choose> action represents the last alternative in a <c:choose> action. The body content of a <c:otherwise> action is similar to the default in a Java switch statement.

Attributes: none

Constraints and Error Handling:

  • <c:otherwise> actions must be the last action contained in a <c:choose> action.

Exposed Class

The JSTL conditional actions expose one class ” ConditionalTagSupport ”that you can use to implement conditional custom actions. That class is discussed below.

ConditionalTagSupport

A class implemented by the <c:forEach> and <c:forTokens> tag handlers

Definition:

 class ConditionalTagSupport {       public ConditionalTagSupport()      public abstract boolean condition() throws JspTagException      public void setVar(String var)      public void setScope(String scope) } 

Description:

The abstract condition method returns a boolean value that determines whether the action's body is evaluated. If that method returns true , the body is evaluated; otherwise, it is ignored.

The ConditionalTagSupport class also provides setter methods for var and scope attributes. Those attributes are used in exactly the same manner as they are for the <c:if> action. See "Conditional Custom Actions" on page 145 for more information about how you can extend the ConditionalTagSupport class to implement custom conditional actions.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

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