Loops are one of the most basic and useful techniques in any programming system. As you would expect, a loop allows you to execute a certain behavior for a given number of times automatically. You can create loops that execute a certain set number of times, or that continue as long as a given condition is met, or that only stop when a certain condition is met. AppComposer provides several ways to create looping behaviors, and a specialized JSP tag to make easy use of them in web applications.
When a JSP file encounters a
<composer:repeat> tag, it calls
to the servlet looking for a true response to the method or activation
condition. If you have a group of behaviors that make up a loop that you want
repeated in the JSP tag, the last one in the group needs to return true.
If the JSP does not receive this indicator, it stops executing the tag, and
moves on to the next part of the JSP file.
Conditional activation is an extremely useful mechanism. It executes a certain behavior or behaviors only if a given condition is true. When you use conditional activation in conjunction with a repeat JSP tag, this mechanism easily creates looping behavior. For instance, in the project you are creating for this tutorial, you need to print a table of results for a series of numbers. Using a repeat tag that calls a conditionally activated behavior makes it possible to print as many rows as necessary for the series of numbers you are using, and then stop when you have displayed them all. A similar concept you may be familiar with from other programming languages is a "while" loop. While the condition in the activation expression is true, the associated behaviors repeatedly execute.
AppComposer makes creating conditional activation easy. You have already created many behaviors that execute from: Receive: <some event>. To create a conditional behavior, you change the Activate On field in the Details pane to Activate If. Once you do, a new field pops up labeled If: Use this field to enter a boolean (true/false) condition to evaluate when the behavior receives its activating event.
For the Math.zap project, you need to create a group of behaviors that prints a table that displays the results received from the chosen calculations. You need to set up a loop that prints one row at a time, starting with the current number in the series, and calculating the results for that number. The loop needs to repeat for as many numbers as are in the series, and stop after it reaches the end number in the series.
void, as we are going to use an alternate mechanism to return the value to the JSP.
You will add behaviors to the print table data action group that lay out and fill in the results table for the operations that the user chooses. The group also needs a behavior that sends a message of true to the JSP when it executes. This true message is what the repeat tag in the JSP uses to know that it should call the loop again. As long as the condition in the If field is met for the group, all of the behaviors will execute in a loop fashion. Once the conditional activation becomes false, the loop stops, no true message is sent, and the JSP moves on to the next part of its content.
This behavior updates the variable currentValue, which is what the capsule uses to calculate the given operations, to the value held in nextValue. This initializes currentValue so it is ready for calculations on the next number in the given series, and also for when the JSP getProperty tag requests the currentValue property of the capsule.
double startValue = NumberParser.parseDouble(capsule.getParameter("initialValue")); double endValue = NumberParser.parseDouble(capsule.getParameter("endValue")); long totalSteps = NumberParser.parseLong(capsule.getParameter("steps")); double increment = (endValue - startValue) / (totalSteps - 1); nextValue = startValue + increment * (currentStep + 1);
The script set nextValue uses a series of Java statements to:
Scripts can perform any series of Java statements. Each statement must use correct Java syntax.
The increment currentStep behavior adds one to the value of the current step, so that the correct number in the series can be properly calculated by the servlet capsule.
The continue looping behavior sets the capsule's Test Result to true. You previously created methods that returned boolean (true/false) values for the conditional tags. An alternate way of returning a value to the JSP is to set the capsule's Test Result. If your method doesn't return a boolean, the JSP checks this test result. In this case, we used conditional activation in the ActionGroup, so if the loop should not continue, no behavior is activated, and the Test Result defaults to being false for each call from the JSP. You can either create a method that returns a boolean value, or use capsule.setTestResult(), which-ever seems easier, to respond to conditional or repeat tags.
The true test result tells the JSP to process the repeat tag again. When it does, it activates the print table data group, which checks that the If condition is still met. If it is, it executes all of its child behaviors, true is sent out again, and the whole process loops. As soon as the condition is not met (in this case, as soon as the number held in the currentStep variable equals the number of steps specified by the user,) the action group does not activate, the true test result is not set, and the JSP moves on.
You have finished creating the capsule! Try it out.
Your screen should now resemble the following:
If it does not, look at the following potential trouble areas.
Since this is a complex capsule with many interlocking behaviors, you may need to dig a little deeper than this to find any errors. If so, you need to do some debugging. The next lesson shows you the tools that AppComposer provides that allow you to test and debug your servlet capsules.