ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/jsprpt.htm

259 lines
8.7 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!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>&lt;tsx:repeat&gt;</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jsprpt"></a>&lt;tsx:repeat&gt;</h5>
<p>Use the &lt;tsx:repeat&gt; syntax to iterate over a database query results set. The &lt;tsx:repeat&gt; 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 &lt;tsx:repeat&gt; 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 &lt;tsx:repeat&gt; syntax is:</p>
<pre>&lt;tsx:repeat index=&quot;<em>name</em>&quot; start=&quot;<em>starting_index</em>&quot; end=&quot;<em>ending_index</em>&quot;&gt;
&lt;/tsx:repeat&gt;</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 &quot;0.&quot;</p></li>
<li><p><strong>end</strong>
<br>This is an optional ending index value for this repeat block. The maximum value is &quot;2,147,483,647.&quot; 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 &lt;tsx:repeat&gt; 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
&lt;tsx:dbquery&gt; syntax).</p>
<p>This table is a graphic representation of the contents of a bean, named &quot;myBean&quot;:</p>
<table border="1" cellpadding="3">
<tr>
<th>&nbsp;</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">&lt;tsx:dbquery&gt;</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 &lt;tsx:repeat&gt; iterates over the rows (beginning at the starting row).</li>
</ul>
<p>The following table compares using the &lt;tsx:repeat&gt; tag to iterate a static bean to using the &lt;tsx:repeat&gt; 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 (&lt;tsx:repeat&gt;)</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>&lt;tsx:dbconnect id=&quot;conn&quot;
userid=&quot;alice&quot; passwd=&quot;test&quot;
url=&quot;jdbc:db2:*local&quot;
driver=&quot;com.ibm.db2.jdbc.app.DB2Driver&quot;
&lt;/tsx:dbconnect &gt;
&lt;tsx:dbquery id=&quot;dynamic&quot; connection=&quot;conn&quot; &gt;
Select * from myTable;
&lt;/tsx:dbquery&gt;
&lt;tsx:repeat index=abc&gt;
&lt;tsx:getProperty name=&quot;dynamic&quot;
property=&quot;col1(abc)&quot; /&gt;
&lt;/tsx:repeat&gt;</pre></td>
</tr>
<tr>
<td headers="col1"><strong>JSP file</strong>
<pre>&lt;tsx:repeat index=abc&gt;
&lt;tsx:getProperty name=&quot;myBean&quot;
property=&quot;col1(abc)&quot; /&gt;
&lt;/tsx:repeat&gt;</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 &lt;tsx:repeat&gt; is used. You can also omit the index on the &lt;tsx:repeat&gt;.</li>
<li>The &lt;tsx:repeat&gt; 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 &lt;tsx:dbquery&gt; and does not exist until the syntax is processed.</li>
<li>The method to access the bean properties is dynamic.getValue(&quot;<em>property</em>&quot;, <em>index</em>).</li>
<li>You can omit the property index, in which case the index of the enclosing &lt;tsx:repeat&gt; is used. You can also omit the index on the &lt;tsx:repeat&gt;.</li>
<li>The &lt;tsx:repeat&gt; 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 &lt;tsx:repeat&gt; 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>&lt;table&gt;
&lt;tsx:repeat&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=&quot;city&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=&quot;address&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=&quot;telephone&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tsx:repeat&gt;
&lt;/table&gt;</pre>
<p>Example 2 shows indexing, starting index, and ending index:</p>
<pre>&lt;table&gt;
&lt;tsx:repeat index=myIndex start=0 end=2147483647&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=city(myIndex) /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=address(myIndex) /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=telephone(myIndex) /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tsx:repeat&gt;
&lt;/table&gt;</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>&lt;table&gt;
&lt;tsx:repeat index=myIndex end=299&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=&quot;city&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=&quot;address(myIndex)&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;serviceLocationsQuery&quot; property=&quot;telephone(myIndex)&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tsx:repeat&gt;
&lt;/table&gt;</pre>
<p><strong>Nesting &lt;tsx:repeat&gt; blocks</strong></p>
<p>You can nest &lt;tsx:repeat&gt; 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 &lt;tsx:repeat&gt; blocks are nested to display the list of songs on each compact disc in the user's shopping cart.</p>
<pre>&lt;tsx:repeat index=cdindex&gt;
&lt;h1&gt;&lt;tsx:getProperty name=&quot;shoppingCart&quot; property=cds.title /&gt;&lt;/h1&gt;
&lt;table&gt;
&lt;tsx:repeat&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;shoppingCart&quot; property=cds(cdindex).playlist /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tsx:repeat&gt;
&lt;/table&gt;
&lt;/tsx:repeat&gt;
</pre>
</body>
</html>