Conditional Tags — Functional Description

<if>
<choose> <when> <otherwise> 


1. Introduction

The set of JSTL conditional tags are designed to support the two most common usage patterns associated with conditional processing: simple conditional execution and mutually exclusive conditional execution. The JSTL conditional tags are similar to the XSLT instructions for conditional processing (<xsl:if> and <xsl:choose>).

A simple conditional execution tag evaluates its body content only if the test condition associated with it is true. In the following example, a special greeting is displayed only if this is a customer's first visit to the site.

  <c:if test="$user.visitCount == 1">
     This is your first visit. Welcome to the site!
  </c:if>

With mutually exclusive conditional execution, only one among a number of possible alternative tags gets its body content evaluated. In the example below, the information displayed for a specific product depends on the verbosity level selected.

  <choose>
    <when test="$verbosityLevel == 'short'">
       <c:expr value="$product.shortDescription"/>
    </when>
    <when test="$verbosityLevel == 'medium'">
       <c:expr value="$product.mediumDescription"/>
    </when>
    <otherwise>
       <c:expr value="$product.longDescription"/>
    </otherwise>
  </choose>

2. <if>

The <if> tag evaluates its body only if the test condition returns true.
 
Attributes
test required expr The test condition to be evaluated. The value returned by the attribute must either be a boolean primitive or a Boolean object (EL library). Must be a Boolean object in the RT library.
var optional string Name of the page scope attribute used to store the current result of the test condition. Visibility is "at_end".

3. <choose>

The <choose> tag performs conditional block execution embedded by the <when> sub tags. It renders the body of the first <when> tag whose test condition evaluates to true. If none of the test conditions of nested <when> tags evaluate to true, then the body of an <otherwise> tag is evaluated, if present.

The <choose> tag has no attributes.

4. <when>

The <when> tag is used within a <choose> tag. The <choose> tag renders the body of the first <when> tag whose test condition evaluates to true. The immediate parent of a <when> tag must be a <choose> tag, otherwise an exception will be thrown.
 
Attributes
test required expr The test condition to be evaluated. The value returned by the attribute must either be a boolean primitive or a Boolean object (EL library). Must be a Boolean object in the RT library.

5. <otherwise>

The <otherwise> tag is used as the last subtag within a <choose> tag. The <choose> tag renders the body of the <otherwise> tag if none of the preceding <when> tags test conditions evaluated to true. The immediate parent of an <otherwise> tag must be a <choose> tag otherwise an exception will be thrown.

This tag has no attributes.

6. Custom logic tags

It is important to note that <if> and <when> tags have different semantics. These semantic differences are enforced by the fact that only <when> tags can be used within the context of a mutually exclusive conditional execution (<choose> tag).

This clean separation of behavior also impacts the way custom logic tags (i.e. tags who render their bodies depending on the result of a test condition) should be developed. Ideally, the result associated with the evaluation of custom logic tag should be usable both in the context of a simple conditional execution, as well as in a mutually exclusive conditional execution.

The proper way to support this is by simply having a custom logic tag export a boolean scripting variable. This boolean scripting variable can then be used as the test condition of a <when> subtag. For example:

  <paramExists name="hotel" var="needsHotel"/>
  <choose>
    <when test="$needsHotel">
      ...
    </when>
    <otherwise>
      ...
    </otherwise>
  </choose>