Java Reference
In-Depth Information
Here's the syntax without body content:
<c:if test="testCondition"
var="varName" [scope="{page|request|session|application}"]/>
Here's the syntax with body content:
<c:if test="testCondition"
[var="varName"] [scope="{page|request|session|application}"]>
. . . body content . . .
</c:if>
Using the
test
attribute, a Boolean EL expression is evaluated. If the test condition evaluates to
true
, only then the body of the action is executed. The Boolean result of the expression evaluation is
exported to a scoped variable using the
var
attribute. The default scope of the
var
is page, but using
the
scope
attribute, the scope could be set to any of the JSP scopes.
Table
3-14
illustrates the attributes of the
<c:if>
action.
Table 3-14.
<c:if> Attributes
Name
Type
Description
test
Boolean
The test condition that determines whether the body content should be processed.
var
String
The name of the exported scoped variable for the resulting value of the test
condition.
scope
The scope for
var
.
String
Listing 3-45 illustrates using <c:if> with the body content.
Listing 3-45. Using <c:if> with Body Content
<c:set var="number" value="9"/>
<c:if test="${ number < 10}" >
<c:out value ="number is less than 10"/>
</c:if>
Here's the output:
number is less than 10
The <c:choose>, <c:when>, and <c:otherwise> Actions
The
<c:choose>
action enables you to handle mutually exclusive conditions. It works like a Java
switch
statement and lets you choose between a number of alternatives, using
<c:when>
instead
of
case
statements and
<c:otherwise>
to provide default action just like the
switch
statement does
with a default clause.