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>
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". |
The <choose> tag has no attributes.
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. |
This tag has no attributes.
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>