In your sample code you use the RowSetDataSource class.  In what package
is this class located?

-----Original Message-----
From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]] 
Sent: Sunday, January 05, 2003 5:49 PM
To: Struts Users Mailing List; [EMAIL PROTECTED]
Subject: Re: Iterate tag with ResultSet Example




On Sat, 4 Jan 2003, Mervin Williams wrote:

> Date: Sat, 4 Jan 2003 19:27:48 -0600
> From: Mervin Williams <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>,
>      [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> Subject: Iterate tag with ResultSet Example
>
> Can someone please provide sample code that demonstrates the use of 
> passing a database SQL ResultSet to the logic:iterate tag?
>

In general, this is going to be a problem.

It would be technically feasible to pass in a java.sql.ResultSet object,
if the tags were enhanced to know how to pull the data out of them.
However, this requires that the ResultSet (and its associated Statement)
remain open until the JSP page was done with it.  This would violate the
separation of business and presentation logic that is one of the key
features of Struts.

As it happens, though, I just checked in a change in commons-beanutils
that can be used for this purpose, if you're using Struts 1.1.
Basically, there's now a really simple way to copy the contents of a
ResultSet into a list of DynaBeans.  This really is a *copy* operation,
so you have to pay some performance and memory cost, but it lets you
pass the data without having to create custom beans, and without leaving
the ResultSet open.

You'll need to grab a very recent nightly build of commons-beanutils
from:

 
http://jakarta.apache.org/builds/jakarta-commons/nightly/commons-beanuti
ls

and replace your commons-beanutils.jar file if you want to try this.

> Please include the .java and .jsp code.
>

In your Java code, you'd do something like this:

    Connection conn = ...;             // Get connection from the pool
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select custid, name from
customers");
    RowSetDataSource rsdc = new RowSetDataSource(rs);
    rs.close();
    stmt.close();
    ... return connection to the pool ...
    request.setAttribute("custoemrs", rsdc.getRows());

In your JSP page, treat this List of DynaBeans like you would any other
collection of beans, because all the Struts tags know how to deal with
DynaBeans.

    <table>
      <tr>
        <th>Customer ID</th>
        <th>Customer Name</th>
      </tr>
      <logic:iterate name="customers" id="customer">
        <tr>
          <td><bean:write name="customer" property="custid"/></td>
          <td><bean:write name="customer" property="name"/></td>
        </tr>
      </logic:iterate>
    </table>

See the updated Javadocs for commons-beanutils (included in the binary
distribution) for class org.apache.commons.beanutils.RowSetDataSource
for more information about this class.

Craig


--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to