Use the <tsx:repeat> syntax to iterate over a database query results set. The <tsx:repeat> syntax iterates from the start value to the end value until one of these conditions is met:
The output of a <tsx:repeat> block is buffered until the block completes. If an exception is thrown before a block completes, no output is written for that block.
The <tsx:repeat> syntax is:
<tsx:repeat index="name" start="starting_index" end="ending_index"> </tsx:repeat>
This list describes the attributes and their values:
index
This is an optional name used to identify the index of this repeat block. The value is case-sensitive and its scope is the JSP file.
start
This is an optional starting index value for this repeat block. The default is "0."
end
This is an optional ending index value for this repeat block. The maximum value is "2,147,483,647." If the value of the end attribute is less than the value of the start attribute, the end attribute is ignored.
The results set and the associated bean
The <tsx:repeat> iterates over a results set. The results set is contained within a bean. The bean can be a static bean or a dynamically generated bean (for example, a bean generated by the <tsx:dbquery> syntax).
This table is a graphic representation of the contents of a bean, named "myBean":
col1 | col2 | col3 | |
---|---|---|---|
row0 | friends | Romans | countrymen |
row1 | bacon | lettuce | tomato |
row2 | May | June | July |
Some observations about the bean:
The following table compares using the <tsx:repeat> tag to iterate a static bean to using the <tsx:repeat> tag with a dynamically generated bean:
Static bean example | Dynamic bean example (<tsx:repeat>) |
---|---|
myBean.class
// Code to get // a connection // Code to get the data Select * from myTable; // Code to close // the connection |
JSP file
<tsx:dbconnect id="conn" userid="alice" passwd="test" url="jdbc:db2:*local" driver="com.ibm.db2.jdbc.app.DB2Driver" </tsx:dbconnect > <tsx:dbquery id="dynamic" connection="conn" > Select * from myTable; </tsx:dbquery> <tsx:repeat index=abc> <tsx:getProperty name="dynamic" property="col1(abc)" /> </tsx:repeat> |
JSP file
<tsx:repeat index=abc> <tsx:getProperty name="myBean" property="col1(abc)" /> </tsx:repeat> |
|
Notes:
|
Notes:
|
Implicit and explicit indexing
Examples 1, 2, and 3 show how to use the <tsx:repeat> tag. The examples produce the same output if all indexed properties have 300 or fewer elements. If there are more than 300 elements, Examples 1 and 2 display all elements, while Example 3 shows only the first 300 elements.
Example 1 shows implicit indexing with the default start and default end index. The bean with the smallest number of indexed properties restricts the number of times that the loop repeats.
<table> <tsx:repeat> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property="city" /> </td> </tr> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property="address" /> </td> </tr> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property="telephone" /> </td> </tr> </tsx:repeat> </table>
Example 2 shows indexing, starting index, and ending index:
<table> <tsx:repeat index=myIndex start=0 end=2147483647> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property=city(myIndex) /> </td> </tr> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property=address(myIndex) /> </td> </tr> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property=telephone(myIndex) /> </td> </tr> </tsx:repeat> </table>
Example 3 shows explicit indexing and ending index with implicit starting index. Although the index attribute is specified, the indexed property city can still be implicitly indexed because the (myIndex) is not required.
<table> <tsx:repeat index=myIndex end=299> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property="city" /> </td> </tr> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property="address(myIndex)" /> </td> </tr> <tr> <td> <tsx:getProperty name="serviceLocationsQuery" property="telephone(myIndex)" /> </td> </tr> </tsx:repeat> </table>
Nesting <tsx:repeat> blocks
You can nest <tsx:repeat> blocks. Each block is separately indexed. This capability is useful for interleaving properties on two beans, or properties that have subproperties. In the example, two <tsx:repeat> blocks are nested to display the list of songs on each compact disc in the user's shopping cart.
<tsx:repeat index=cdindex> <h1><tsx:getProperty name="shoppingCart" property=cds.title /></h1> <table> <tsx:repeat> <tr> <td> <tsx:getProperty name="shoppingCart" property=cds(cdindex).playlist /> </td> </tr> </tsx:repeat> </table> </tsx:repeat>