例: tsx:repeat JavaServer Pages タグの使用による結果セットでの繰り返し (非推奨)
<tsx:repeat> タグは結果セットで繰り返されます。 結果セットは Bean の中に含まれています。 Bean は静的 Bean (例えば、IBM® WebSphere® Studio データベース・ウィザードを使用して作成された Bean) でも、動的に生成された Bean (例えば、<tsx:dbquery> 構文によって生成された Bean) でも構いません。

次の表は、myBean と呼ばれる Bean のコンテンツを表で表現したものです。
列 | col1 | col2 | col3 |
---|---|---|---|
row0 | friends | Romans | countrymen |
row1 | bacon | lettuce | tomato |
row2 | May | June | July |
Bean に関するいくつかの注意点
- データベース表の中のカラム名は Bean のプロパティー名になります。 <tsx:dbquery> セクションでは、 カラム名をさまざまなプロパティー名にマップするための手法を説明しています。
- Bean プロパティーは索引が付いています。 例えば、myBean.get(Col1(row2)) は May を戻します。
- 照会結果は複数の行になっています。 <tsx:repeat> タグは、複数の行で繰り返されます (開始行から始まります)。
次の表は、静的 Bean で繰り返される <tsx:repeat> タグを使用する場合と、 動的に生成された Bean の場合とを比較したものです。
静的 Bean の例 | <tsx:repeat> Bean の例 |
---|---|
myBean.class// Code to get a connection // Code to get the data Select * from myTable; // Code to close the connectionJSP ファイル <tsx:repeat index=abc> <tsx:getProperty name="myBean" property="col1(abc)" /> </tsx:repeat> 注:
|
JSP ファイル<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> 注:
|
暗黙的および明示的な索引付け
例 1、2、および 3 は、 <tsx:repeat> タグの使用方法を示しています。 これらの例は、索引付きのプロパティーがすべて 300 以下のエレメントを持つ場合には、 同じ出力を作成します。 エレメントが 300 よりも多い場合には、例 1 と 2 はすべてのエレメントを表示しますが、 例 3 は最初の 300 のエレメントだけを表示します。
例 1 は、デフォルトの開始および終了索引を使用した、暗黙の索引付け を示しています。 最も小さい番号の索引付きプロパティーを持つ Bean により、ループの反復回数が制限されます。
<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>
例 2 は、索引付け、開始索引、および終了索引を示しています。
<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>
例 3 は、明示的な索引付け および終了索引と、暗黙の開始索引を示しています。 index 属性が指定されていますが、(myIndex) タグが必要ないため、 索引付きプロパティー city は暗黙的に索引付けすることができます。
<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>
<tsx:repeat> ブロックのネスティング
<tsx:repeat> ブロックをネストできます。 各ブロックは個別に索引付けされます。 この機能は、2 つの Bean でプロパティーをインターリーブする場合、 あるいはサブプロパティーを持つプロパティーをインターリーブする場合に便利です。 例では、2 つの <tsx:repeat> ブロックがネストされて ユーザーのショッピング・カートの中にあるそれぞれの CD に入っている歌のリストを表示します。
<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>