259 lines
8.7 KiB
HTML
259 lines
8.7 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
|
||
|
|
||
|
<title><tsx:repeat></title>
|
||
|
</head>
|
||
|
|
||
|
<BODY>
|
||
|
<!-- Java sync-link -->
|
||
|
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
||
|
|
||
|
<h5><a name="jsprpt"></a><tsx:repeat></h5>
|
||
|
|
||
|
<p>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:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>The end value is reached.</li>
|
||
|
<li>An ArrayIndexOutofBoundsException is thrown.</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>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.</p>
|
||
|
|
||
|
<p>The <tsx:repeat> syntax is:</p>
|
||
|
|
||
|
<pre><tsx:repeat index="<em>name</em>" start="<em>starting_index</em>" end="<em>ending_index</em>">
|
||
|
</tsx:repeat></pre>
|
||
|
|
||
|
<p>This list describes the attributes and their values:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li><p><strong>index</strong>
|
||
|
<br>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.</p></li>
|
||
|
|
||
|
<li><p><strong>start</strong>
|
||
|
<br>This is an optional starting index value for this repeat block. The default is "0."</p></li>
|
||
|
|
||
|
<li><p><strong>end</strong>
|
||
|
<br>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.</p></li>
|
||
|
</ul>
|
||
|
|
||
|
<p><strong>The results set and the associated bean</strong></p>
|
||
|
|
||
|
<p>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).</p>
|
||
|
|
||
|
<p>This table is a graphic representation of the contents of a bean, named "myBean":</p>
|
||
|
|
||
|
<table border="1" cellpadding="3">
|
||
|
<tr>
|
||
|
<th> </th>
|
||
|
<th>col1</th>
|
||
|
<th>col2</th>
|
||
|
<th>col3</th>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td><strong>row0</strong></td>
|
||
|
<td>friends</td>
|
||
|
<td>Romans</td>
|
||
|
<td>countrymen</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td><strong>row1</strong></td>
|
||
|
<td>bacon</td>
|
||
|
<td>lettuce</td>
|
||
|
<td>tomato</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td><strong>row2</strong></td>
|
||
|
<td>May</td>
|
||
|
<td>June</td>
|
||
|
<td>July</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
|
||
|
<p>Some observations about the bean:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>The column names in the database table become the property names of the bean. The <a href="jspqry.htm"><tsx:dbquery></a> topic describes a technique for mapping the column names to different property names.</li>
|
||
|
|
||
|
<li>The bean properties are indexed. For example, <tt>myBean.get(Col1(row2))</tt> returns <tt>May</tt>.</li>
|
||
|
|
||
|
<li>The query results are in the rows. The <tsx:repeat> iterates over the rows (beginning at the starting row).</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>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:</p>
|
||
|
|
||
|
<table border="1" cellpadding="3" cellspacing="0">
|
||
|
<tr>
|
||
|
<th id="col1">Static bean example</th>
|
||
|
<th id="col2">Dynamic bean example (<tsx:repeat>)</th>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td headers="col1"><strong>myBean.class</strong>
|
||
|
<pre>// Code to get
|
||
|
// a connection
|
||
|
|
||
|
// Code to get the data
|
||
|
Select * from myTable;
|
||
|
|
||
|
// Code to close
|
||
|
// the connection</pre></td>
|
||
|
|
||
|
<td rowspan="2" headers="col2"><strong>JSP file</strong>
|
||
|
<pre><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></pre></td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td headers="col1"><strong>JSP file</strong>
|
||
|
<pre><tsx:repeat index=abc>
|
||
|
<tsx:getProperty name="myBean"
|
||
|
property="col1(abc)" />
|
||
|
</tsx:repeat></pre></td>
|
||
|
</tr>
|
||
|
|
||
|
<tr valign="top">
|
||
|
<td headers="col1"><strong>Notes:</strong>
|
||
|
<ul>
|
||
|
<li>The bean (myBean.class) is a static bean.</li>
|
||
|
<li>The method to access the bean properties is myBean.get(<em>property</em>(<em>index</em>)).</li>
|
||
|
<li>You can omit the property index, in which case the index of the enclosing <tsx:repeat> is used. You can also omit the index on the <tsx:repeat>.</li>
|
||
|
<li>The <tsx:repeat> iterates over the bean properties row by row, beginning with the starting row.</li>
|
||
|
</ul></td>
|
||
|
|
||
|
<td headers="col2"><strong>Notes:</strong>
|
||
|
<ul>
|
||
|
<li>The bean (dynamic) is generated by the <tsx:dbquery> and does not exist until the syntax is processed.</li>
|
||
|
<li>The method to access the bean properties is dynamic.getValue("<em>property</em>", <em>index</em>).</li>
|
||
|
<li>You can omit the property index, in which case the index of the enclosing <tsx:repeat> is used. You can also omit the index on the <tsx:repeat>.</li>
|
||
|
<li>The <tsx:repeat> syntax iterates over the bean properties row by row, beginning with the start row.</li>
|
||
|
</ul></td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
|
||
|
<p><strong>Implicit and explicit indexing</strong></p>
|
||
|
|
||
|
<p>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.</p>
|
||
|
|
||
|
<p>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.</p>
|
||
|
|
||
|
<pre><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></pre>
|
||
|
|
||
|
<p>Example 2 shows indexing, starting index, and ending index:</p>
|
||
|
|
||
|
<pre><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></pre>
|
||
|
|
||
|
<p>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 <tt>(myIndex)</tt> is not required.</p>
|
||
|
|
||
|
<pre><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></pre>
|
||
|
|
||
|
<p><strong>Nesting <tsx:repeat> blocks</strong></p>
|
||
|
|
||
|
<p>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.</p>
|
||
|
|
||
|
<pre><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>
|
||
|
</pre>
|
||
|
|
||
|
</body>
|
||
|
</html>
|
||
|
|