Example: Using tsx:repeat JavaServer Pages tag to iterate over a results set (deprecated)
The <tsx:repeat> tag iterates over a results set. The results set is contained within a bean. The bean can be a static bean, for example, a bean created using the IBM WebSphere Studio database wizard, or a dynamically generated bean, for example, a bean generated by the <tsx:dbquery> syntax.
Deprecated feature: Support for tsx tags in the JavaServer Pages (JSP) engine are deprecated in WebSphere Application Server v6.0. Instead of using the tsx tags, we should use equivalent tags from the JavaServer Pages Standard Tag Library (JSTL). depfeat
The following table is a graphic representation of the contents of a bean called, myBean:
Column 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 <tsx:dbquery> section 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 <tsx:repeat> tag iterates over the rows, beginning at the start row.
The following table compares using the <tsx:repeat> tag to iterate over a static bean, versus a dynamically generated bean:
Static Bean Example <tsx:repeat> Bean Example myBean.class // Code to get a connection // Code to get the data Select * from myTable; // Code to close the connectionJSP file<tsx:repeat index=abc> <tsx:getProperty name="myBean" property="col1(abc)" /> </tsx:repeat>
- The bean (myBean.class) is a static bean.
- The method to access the bean properties is myBean.get(property(index)).
- We can omit the property index, in which case the index of the enclosing <tsx:repeat> tag is used. We can also omit the index on the <tsx:repeat> tag.
- The <tsx:repeat> tag iterates over the bean properties row by row, beginning with the start row.
JSP file <tsx:dbconnect id="conn" userid="alice"passwd="test" url="jdbc:db2:sample" 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>
- The bean (dynamic) is generated by the <tsx:dbquery> tag and does not exist until the syntax executes.
- The method to access the bean properties is dynamic.getValue("property", index).
- We can omit the property index, in which case the index of the enclosing <tsx:repeat> tag is used. We can also omit the index on the <tsx:repeat> tag.
- The <tsx:repeat> tag syntax 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 <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 the loop repeats.
<table> <tsx:repeat> <tr><td><tsx:getProperty name="serviceLocationsQuery" property="city" /> </tr></td> <tr><td><tsx:getProperty name="serviceLocationsQuery" property="address" /> </tr></td> <tr><td><tsx:getProperty name="serviceLocationsQuery" property="telephone" /> </tr></td> </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) /> </tr></td> <tr><td><tsx:getProperty name="serviceLocationsQuery" property=address(myIndex) /> </tr></td> <tr><td><tsx:getProperty name="serviceLocationsQuery" property=telephone(myIndex) /> </tr></td> </tsx:repeat> </table>Example 3 shows explicit indexing and ending index with implicit starting index. Although the index attribute is specified, we can still implicitly index the indexed property city because the (myIndex) tag is not required.
<table> <tsx:repeat index=myIndex end=299> <tr><td><tsx:getProperty name="serviceLocationsQuery" property="city" /t> </tr></td> <tr><td><tsx:getProperty name="serviceLocationsQuery" property="address(myIndex)" /> </tr></td> <tr><td><tsx:getProperty name="serviceLocationsQuery" property="telephone(myIndex)" /> </tr></td> </tsx:repeat> </table>
Nesting <tsx:repeat> blocks
We 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>
Related:
JavaServer Pages Use installed optional packages Manage shared libraries Web applications: Resources for learning