InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.2: Developing JSP files >
4.2.2.3: Overview of JSP file content >
4.2.2.3.8: IBM extensions to JSP .91 syntax >
4.2.2.3.8.1: JSP .91 syntax: Tags for variable data >
4.2.2.3.8.1.2a: JSP .91 syntax: <REPEAT> tag results set and the associated bean

4.2.2.3.8.1.2a: JSP .91 syntax: <REPEAT> tag results set and the associated bean

The <REPEAT> tag iterates over a results set. The results set is contained within a JavaBean. The bean can be a static bean (for example, a bean created by using the IBM WebSphere Studio database wizard) or a dynamically generated bean (for example, a bean generated by the <DBQUERY> tag). The following table is a graphic representation of the contents of a bean, myBean:

  col1 col2 col3
row0 friends Romans countrymen
row1 bacon lettuce tomato
row2 May June July

Some observations about the bean:

  • The column names in the database table become the property names of the bean. The section <DBQUERY> tag describes a technique for mapping the column names to different property names.
  • The bean properties are indexed. For example, myBean.get(Col1(row2)) returns May.
  • The query results are in the rows. The <REPEAT> tag iterates over the rows (beginning at the start row).

The following table compares using the <REPEAT> tag to iterate over static bean versus a dynamically generated bean:

Static Bean Example <DBQUERY> Bean Example
myBean.class
// Code to get a connection

// Code to get the data
Select * from myTable;

// Code to close the connection
JSP file
<repeat index=abc>
<insert bean="myBean"
property="col1(abc)">
</insert>
</repeat>
  • The bean (myBean.class) is a static bean.
  • The method to access the bean properties is myBean.get(property(index)).
  • You can omit the property index, in which case the index of the enclosing <REPEAT> tag is used. You can also omit the index on the <REPEAT> tag.
  • The <REPEAT> tag iterates over the bean properties row by row, beginning with the start row.
JSP file
<dbconnect id="conn"
userid="alice"passwd="test"
url="jdbc:db2:sample"
driver="COM.ibm.db2.jdbc.app.DB2Driver"
</dbconnect>

<dbquery id="dynamic" connection="conn" >
Select * from myTable;
</dbquery>

<repeat index=abc>
<insert bean="dynamic"
property="col1(abc)">
</insert>
</repeat>
  • The bean (dynamic) is generated by the <DBQUERY> tag and does not exist until the tag is executed.
  • The method to access the bean properties is dynamic.getValue("property", index).
  • You can omit the property index, in which case the index of the enclosing <REPEAT> tag is used. You can also omit the index on the <REPEAT> tag.
  • The <REPEAT> tag iterates over the bean properties row by row, beginning with the start row.

Implicit and explicit indexing

Examples 1, 2, and 3 show how to use the <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 will display all elements, while Example 3 will show 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 the loop will repeat.

<table>
<repeat>
<tr><td><insert bean=serviceLocationsQuery property=city></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=address></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=telephone></insert></tr></td>
</repeat>
</table>

Example 2 shows indexing, starting index, and ending index:

<table>
<repeat index=myIndex start=0 end=2147483647>
<tr><td><insert bean=serviceLocationsQuery property=city(myIndex)></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=address(myIndex)></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=telephone(myIndex)></insert></tr></td>
</repeat>
</table>

The JSP compiler for the Application Server Version 3 is designed to prevent the ArrayIndexOutofBoundsException with explicit indexing. Consequently, you do not need to place JSP variable data syntax before the <INSERT> tag to check the validity of the index.

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>
<repeat index=myIndex end=299>
<tr><td><insert bean=serviceLocationsQuery property=city></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=address(myIndex)></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=telephone(myIndex)></insert></tr></td>
</repeat>
</table>

Nesting <REPEAT> tags

You can nest <REPEAT> blocks. Each block is separately indexed. This capability is useful for interleaving properties on two beans, or properties that have sub-properties. In the example, two <REPEAT> blocks are nested to display the list of songs on each compact disc in the user's shopping cart.

<repeat index=cdindex>
<h1><insert bean=shoppingCart property=cds.title></insert></h1>
<table>
<repeat>
<tr><td><insert bean=shoppingCart property=cds(cdindex).playlist></insert>
</td></tr>
</table>
</repeat>
</repeat>

Go to previous article: JSP .91 <REPEAT> tag syntax Go to next article: JSP .91 syntax: JSP tags for database access

 

 
Go to previous article: JSP .91 <REPEAT> tag syntax Go to next article: JSP .91 syntax: JSP tags for database access