11.3 Conditional ActionsJSTL provides four actions that let you handle simple conditions or mutually exclusive conditions. Simple conditions execute some code based on whether a single condition is true, whereas mutually exclusive conditions execute some code based on whether one of many conditions is true. The JSTL conditional actions are listed in Table 11.11.
One classConditionalTagSupportis exposed for conditional custom actions. JSTL Conditional Actions
Syntax:[5]
Syntax #1: Without a body, stores the test result in a scoped variable <c:if test var [scope]/> Syntax #2: With a body that is evaluated if the test condition is true <c:if test [var] [scope]> Description: You can use <c:if> to do two things: conditionally execute some code contained in the body of the action and store the boolean result of the test condition in a scoped variable. You can do both of those things simultaneously with syntax 2.
Constraints and Error Handling:
In a Nutshell: The <c:if> action evaluates a boolean expression specified with the test attribute; if that expression is true and the <c:if> action has a body, the body is evaluated; otherwise it is ignored. If you specify the var attribute, <c:if> will store the result of the boolean expression in a scoped variable. You can also use <c:if> without a body, as illustrated by syntax 1, to store the result of a boolean expression in scoped variable; presumably, that scoped variable is referenced elsewhere to determine whether some functionality is performed.
<c:choose> Description: The body of a <c:choose> action can contain one or more <c:when> actions and an optional <c:otherwise> action. The body content of the first <c:when> action whose condition evaluates to true is evaluated; otherwise, the body of the <c:otherwise> action, if present, is evaluated. Attributes: none Constraints and Error Handling:
In a Nutshell: The <c:choose> action is used in conjunction with <c:when> and <c:otherwise> to emulate if/else and switch statement constructs.[6]
<c:when test> Description: A <c:when> action can only exist in the body of a <c:choose> action. The body content of the first <c:when> action whose test conditionspecified with the test attributeevaluates to true is evaluated. Attributes:
Constraints and Error Handling:
In a Nutshell: The <c:when> action is similar to the <c:if> action; both actions have a test conditions specified with a test attribute. The difference is that <c:when> actions must appear within a <c:choose> action and represent one of several (two or more) alternatives.
<c:otherwise> Description: The <c:choose> action represents the last alternative in a <c:choose> action. The body content of a <c:otherwise> action is similar to the default in a Java switch statement. Attributes: none Constraints and Error Handling: Exposed ClassThe JSTL conditional actions expose one classConditionalTagSupportthat you can use to implement conditional custom actions. That class is discussed below.
Definition: class ConditionalTagSupport { public ConditionalTagSupport() public abstract boolean condition() throws JspTagException public void setVar(String var) public void setScope(String scope) } Description: The Conditional Custom Actions" on page 145 for more information about how you can extend the ConditionalTagSupport class to implement custom conditional actions. |