| This Chapter | |
| - | Chapter 7: JSTL |
| - | Introducing JSTL |
| - | General-Purpose Actions |
| - | Conditional Actions |
| - | Iterator Actions |
| - | Formatting Actions |
| - | Functions |
| - | Summary |
Conditional actions are used to deal with situations in which the output of a page depends on the value of certain input. In Java this is solved using the if, if ... else, and switch statements.
There are four tags that perform conditional actions in JSTL: if, choose, when, and otherwise. Each will be discussed in a section below.
The if tag tests a condition and processes its body content if the condition evaluates to true. The test result is stored in a Boolean object, and a scoped variable is created to reference the Boolean object. You specify the name of the scoped variable using the var attribute and the scope of the scoped variable using the scope attribute.
The syntax of if has two forms. The first form has no body content:
<c:if test="testCondition" var="varName"
[scope="{page|request|session|application}"]/>
In this case, normally the scoped object specified by var will be tested by some other tag at a later stage in the same JSP.
The second form is used with a body content:
<c:if test="testCondition [var="varName"]
[scope="{page|request|session|application}"]>
body content
</c:if>
The body content is JSP and will be processed if the test condition evaluates to true. The list of the if tag’s attributes is given in Table 7.5.
| Attribute | Type | Description |
|---|---|---|
| test+ | Boolean | The test condition that determines whether any existing body content should be processed |
| var | String | The name of the scoped variable that references the value of the test condition; the type of var is Boolean |
| scope | String | The scope of the scoped variable specified by var. |
Table 7.5: The if tag’s attributes
For example, the following if tag displays “You logged in successfully” if there is a request parameter user whose value is ken and a request parameter password whose value is blackcomb:
<c:if test="${param.user=='ken' && param.password=='blackcomb'}">
You logged in successfully.
</c:if>
To simulate an else, use two if tags with conditions that are opposite. For instance, the following snippet displays “You logged in successfully” if the user and password parameters are ken and blackcomb, respectively. Otherwise, it displays “Login failed”.
<c:if test="${param.user=='ken' && param.password=='blackcomb'}">
You logged in successfully.
</c:if>
<c:if test="${!(param.user=='ken' && param.password=='blackcomb')}">
Login failed.
</c:if>
The following if tag tests whether the user and password parameters are ken and blackcomb, respectively, and stores the result in the page-scoped variable loggedIn. You then use an EL expression to display “You logged in successfully” if the loggedIn variable is true and “Login failed” if the loggedIn variable is false.
<c:if var="loggedIn"
test="${param.user=='ken' && param.password=='blackcomb'}"/>
...
${(loggedIn)? "You logged in successfully" : "Login failed"}
The choose and when tags act similarly to the switch and case keywords in Java, that is, they are used to provide the context for mutually exclusive conditional execution. The choose tag must have one or more when tags nested inside it, and each when tag represents a case that can be evaluated and processed. The otherwise tag is used for the default conditional block that will be processed if none of the when tags’ test conditions evaluates to true. If present, otherwise must appear after the last when.
choose and otherwise do not have an attribute. when must have the test attribute specifying the test condition that determines whether the body content should be processed.
As an example, the following code tests the value of a parameter called status. If the value of status is full, it displays “You are a full member”. If the value is student, it displays “You are a student member”. If the parameter status does not exist or if its value is neither full nor student, the code displays nothing.
<c:choose>
<c:when test="${param.status=='full'}">
You are a full member
</c:when>
<c:when test="${param.status=='student'}">
You are a student member
</c:when>
</c:choose>
The following example is similar to the preceding one, but it uses the otherwise tag to display “Please register” if the status parameter does not exist or if its value is not full or student:
<c:choose>
<c:when test="${param.status=='full'}">
You are a full member
</c:when>
<c:when test="${param.status=='student'}">
You are a student member
</c:when>
<c:otherwise>
Please register
</c:otherwise>
</c:choose>