Learn Java for Web Development

(Tina Meador) #1

136 CHAPTER 3: Best Practices in Java EE Web Development


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...



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 action.


Table 3-14. 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 String The scope for var.

Listing 3-45 illustrates using with the body content.


Listing 3-45. Using with Body Content






Here’s the output:


number is less than 10


The , , and Actions


The 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 instead
of case statements and to provide default action just like the switch statement does
with a default clause.

Free download pdf