This lesson shows you how to use the HTMLDoMultiple actor to generate a variable number of output items. It will work with the SqlSelect component to generate a list of the results from the database.
You want to display the results in a bulleted list. The HTML tag that creates bulleted lists is called an unordered list tag, written as <UL>. To generate such a list from the capsule, you need to add an HTMLList actor to the HTML document.
While you build the capsule, you do not know how many items will be on the
shelf, so you do not know how long of a list to make. You need to add an HTMLDoMultiple
element as a child of ListOfBooks
. This actor generates a variable
number of HTML elements that it determines at runtime. As a child of HTMLDoMultiple
,
you will add an HTMLListItem
actor, since this is the element that
you want HTMLDoMultiple
to generate. Finally, each item on the
list will contain text from the database, so you need to add an HTMLText
actor to hold the text from the database.
At this point, your capsule should look something like this:
Your query is now in place, as well as the page to display the results. You still need a means to execute the query and write the results to the list.
First, create the action to execute the query. The query needs to execute in order to display the list, so put the action under HTMLDoMultiple.
Now that you have a way to activate and execute the SqlSelect statement, you need to define a way to retrieve the results to the capsule. To do this, add an action group behavior to SqlSelect that activates when a row is retrieved from the result set of the query. By default, an SqlSelect component automatically moves to the next row of the result set when the current row finishes processing, so you do not need to send a "next" message to SqlSelect.
When the statement is executed, it begins at the first row immediately, and automatically moves to each subsequent row when the processing is complete on the current row. By adding an action group behavior that activates on each rowRetrieved event, every action that is included in the group executes each time a row is retrieved.
Next you need to display the results. You can add two actions to row retrieved to accomplish this. First add an action to print the current row data, then add an action to tell HTMLDoMultiple to go on to its next iteration.
Save your work and run the servlet!
From the File
menu, choose Run. You should see the following
screen:
You have just successfully built a servlet that uses an SQLSelect bean and displays a varying number of results in a list. Congratulations!