|
3.2 Conditional ActionsConditional statements are essential for any programming language, but before JSTL, JSP did not provide an explicit means to express conditional statements. JSTL offers the following conditional actions:
JSTL supports two types of conditions: Simple ConditionsYou can use the <c:if> action with the following syntax:[14]
The test attribute, which is required, is a boolean expression that determines whether the body of the <c:if> action is evaluated. That attribute is an EL expression for the <c:if> action and a Java expression for the <c_rt:if> action. The optional var and scope attributes specify a scoped variable that stores the boolean result of the expression specified with the test attribute. The following code fragment uses <c:if> to test the existence of a non-null request parameter named name:
If the name request parameter exists and is not null, the preceding code fragment displays its value. You can also use the var and scope attributes like this:
The preceding code fragment uses the <c_rt:if> action to see if the current HTTP requests supports GZIP encoding; if so, the message GZIP is supported is displayed. The <c_rt:if> action in the preceding code fragment also stores the result of the test condition in a request-scoped variable named zipTest. Later on, for the same request and presumably in a different JSP page, you can test to see whether the current request supports the GZIP encoding, like this:
The preceding code fragment uses <c:if> to test the value of the zipTest scoped variable; if that variable is true, the body of the <c:if> action is evaluated. Most of the time, if you store the result of a test condition in a scoped variable, as is the case for the preceding code fragment, you don't need a body for the <c:if> or <c_rt:if> action that creates the scoped variable. JSTL supports that use case with the following syntax:
In the preceding code fragment, if you don't need to display a message if the GZIP encoding is supported for a particular request but you want to store a boolean variable that you can test against later, you can do this:
The <c:if> action and its corresponding <c_rt:if> action are easy to understand and use, as the preceding code fragments illustrate. One popular use for the <c:if> action is to retain values for HTML option elements, which is discussed in the next section. Retaining Values for HTML Option ElementsIn "Retaining Values for HTML Text Elements" on page 106, we saw how to retain values for HTML text elements with the <c:out> action. In this section we explore how to retain values for HTML option elements with the <c:if> action. The JSP page shown in Figure 3-8 contains a form with two textfields and an HTML select element, which is populated with HTML option elements. The action for that form is unspecified, so when you activate the submit button, the JSP page is reloaded. As you can see from Figure 3-8, the textfields and the select element all retain their values when the JSP page is reloaded. The top picture in Figure 3-8 shows the JSP page when it's first loaded and a user has filled out the form, and the bottom picture shows the JSP page after the user has activated the submit button and the page has been reloaded. Figure 3-8. Retaining Values for HTML Option Elements with <c:if>The JSP page shown in Figure 3-8 is listed in Listing 3.12. Listing 3.12 Retaining HTML Option Values
The preceding JSP page retains values for its textfields with the <c:out> action and retains values for its option elements with the <c:if> action. Each option element is declared in three stages, like this:
First, the start tag for the option element is specified without the closing angle bracket. Second, if the cardType request parameter is the same as the name of the current option, the string selected is added to the option start tag. Third, the start tag is completed with the closing angle bracket, the display value is specified, and the option end tag is added. For the preceding code fragment, if Visa was the last card type selected, the following will be generated:
If the last card type selected was not Visa, the following will be generated:
The <c:if> action is handy for simple conditions, but it will not suffice for mutually exclusive actions, such as if/else or switch constructs. The following section shows you how to specify those constructs. Mutually Exclusive ConditionsSometimes you need to execute code if one of several conditions is true. This section shows you how to do that with the <c:choose>, <c:when>, and <c:otherwise> actions. When you specify a mutually exclusive condition with JSTL, the <c:choose> action is always the outermost action; the syntax for that action looks like this:[15]
The body content of <c:choose> actions can only contain one or more <c:when> actions and, optionally, one <c:otherwise> action. The <c:otherwise> action, if specified, must be the last action in the body of the <c:choose> action. The <c:when> action has the following syntax:[16]
The <c:when> action is similar to the <c:if> action�both actions have a test attribute that determines whether the action's body content is evaluated. Unlike the <c:if> action, <c:when> actions do not have var and scope attributes, so you cannot store the result of a <c:when> action's boolean test in a scoped variable. The <c:otherwise> action's body content is evaluated only if none of the preceding <c:when> actions nested in the same <c:choose> action evaluated to true. Here is the syntax for the <c:otherwise> action:[17]
You can use the <c:choose>, <c:when>, and <c:otherwise> actions together to emulate if/else constructs or switch statements. The former is discussed in the next section and the latter is discussed in "Implementing Switch Statement Constructs" on page 136. Implementing If/Else ConstructsAn if/else statement is implemented with <c:choose>, <c:when>, and <c:otherwise> actions like this:
In the preceding code fragment, the <c:when> action represents the if clause and the <c:otherwise> action represents the else clause. Let's see how to put that construct to use with a JSP page�shown in Figure 3-9�that simulates rolling dice. Figure 3-9. Rolling the DiceWhen you activate the Roll the dice button in the JSP page shown in Figure 3-9, a random number is generated from 0 to 6, inclusive. If that number is greater than 0, the JSP page displays a die with the corresponding number. If that number is 0, the JSP page displays a message indicating that the roll was invalid. The top picture shown in Figure 3-9 shows a valid roll and the bottom picture depicts an invalid roll. The JSP page shown in Figure 3-9 is listed in Listing 3.13. The preceding JSP page uses the <c_rt:set> action to store a random number between 0 and 6 inclusive in a page-scoped variable named roll. Subsequently, an if/else construct is implemented with a single <c:when> action and a single <c:otherwise> action nested in a <c:choose> action. The <c:when> action tests the value stored in the roll scoped variable. If that variable contains a valid number for a dice roll, the body of the <c:when> action displays an image. The name of that image is constructed with the roll scoped variable with this EL expression: dice-${roll}.jpg. If the value stored in the roll scoped variable is 1, the image dice-1.jpg will be displayed; if the value is 2, the image dice-2.jpg will be displayed, and so on. If the roll scoped variable does not contain a valid number for a dice roll, the <c:otherwise> action displays a message that indicates an invalid roll. Listing 3.13 Emulating the If/Else Construct
If you need to choose between more than two conditions, you can emulate a switch statement simply by adding more <c:when> actions in the body of a <c:choose> action, as illustrated in the next section. Implementing Switch Statement ConstructsListing 3.14 lists a variation of the JSP page shown in Figure 3-9 on page 134 that illustrates how you can emulate a switch statement with the <c:choose>, <c:when>, and <c:otherwise> actions. Listing 3.14 Implementing Switch Statement Constructs
The preceding JSP page is functionally equivalent to the JSP page listed in Listing 3.13. The preceding JSP page tests each value that constitutes a valid dice role with individual <c:when> actions. In this case, we are testing for one condition out of seven, but you can easily generalize the JSP page to select one condition out of any number of conditions that you desire. |
|
No comments:
Post a Comment