You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Vijay Balakrishnan <vb...@northpacific.com> on 2003/07/15 20:34:07 UTC

[OT]RE: Iterating the resultset contents in the view (jsp)

Hi,

Is there a way to return this resultset from a SQL Query as an xml file
using a beanutils class or Digester ?

Thanks,
Vijay

-----Original Message-----
From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
Sent: Monday, July 14, 2003 7:25 PM
To: Struts Users Mailing List
Subject: Re: Iterating the resultset contents in the view (jsp)




On Mon, 14 Jul 2003, Richard Hill wrote:

> Date: Mon, 14 Jul 2003 16:36:17 -0700
> From: Richard Hill <RH...@classmates.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: "'struts-user@jakarta.apache.org'" 
> <st...@jakarta.apache.org>
> Subject: Iterating the resultset contents in the view (jsp)
>
> Hi,
> I'm working on an action that gets a resultset from a database table 
> containing 4 columns. I need to pass that information back to the view 
> (jsp) which will iterate over results. My question is what is the best 
> way to do this. Do I create an array for each row in the resultset and 
> insert each array in a collection, passing that back to the view?
>

That is certainly one approach.  Indeed, commons-beanutils has a useful
little class (org.apache.commons.beanutils.RowSetDynaClass) that is ideally
suited to this use case.  It creates a List of DynaBeans representing the
data content returned by the SELECT.  Because it makes a copy, you can close
the result set (and return the connection back to the connection pool)
before forwarding to the page.

> If so, how would you iterate over each array in the collection with 
> the logic:iterate taglib? All of the examples only show iterations 
> over single column lists.
>

Let's assume you have done this in your Action:

  ResultSet rs = ...;
  RowSetDynaClass rsdc = new RowSetDynaClass(rs);
  rs.close();
  request.setAttribute("customers", rsdc.getList());

so you now have a request attribute containing the list.  Now, in your page,
you can say things like:

  <logic:iterate id="customer" name="customers">
    Name is <bean:write name="customer" property="name"/>
    Status is <bean:write name="customer" property="status"/>
  </logic:iterate>

and so on.  Details of RowSetDynaClass are in the javadocs for BeanUtils:

  http://jakarta.apache.org/commons/beanutils/

> Any help would be appreciated.
>
> Thanks,
> Richard
>

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: [OT]RE: Iterating the resultset contents in the view (jsp)

Posted by Kris Schneider <kr...@dotech.com>.
I'm not sure about other DB vendors, but Oracle provides an XML Developer's Kit
that includes:

XML SQL Utility: supporting Java, generates XML documents, DTDs and Schemas from
SQL queries.

Haven't used it before but it might be worth a look:

http://otn.oracle.com/tech/xml/xdkhome.html

The roll-your-own approach generally involves using ResultSetMetaData to
dynamically build an XML doc that "looks" something like:

<result-set>
  <row id="1">
    <!-- each row subelement is a column -->
    <firstname>Joe</firstname>
    <lastname>Foo</lastname>
    ...
  </row>
  ...
<result-set>

I don't have a specific reference handy, but you should be able to find some
example code floating around the net...

Quoting David Graham <gr...@yahoo.com>:

> --- Vijay Balakrishnan <vb...@northpacific.com> wrote:
> > Hi,
> > 
> > Is there a way to return this resultset from a SQL Query as an xml file
> > using a beanutils class or Digester ?
> 
> I don't know of any class that does that.  Every application will require
> its own XML format anyways.
> 
> David
> 
> > 
> > Thanks,
> > Vijay
> > 
> > -----Original Message-----
> > From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
> > Sent: Monday, July 14, 2003 7:25 PM
> > To: Struts Users Mailing List
> > Subject: Re: Iterating the resultset contents in the view (jsp)
> > 
> > 
> > 
> > 
> > On Mon, 14 Jul 2003, Richard Hill wrote:
> > 
> > > Date: Mon, 14 Jul 2003 16:36:17 -0700
> > > From: Richard Hill <RH...@classmates.com>
> > > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > > To: "'struts-user@jakarta.apache.org'" 
> > > <st...@jakarta.apache.org>
> > > Subject: Iterating the resultset contents in the view (jsp)
> > >
> > > Hi,
> > > I'm working on an action that gets a resultset from a database table 
> > > containing 4 columns. I need to pass that information back to the view
> > 
> > > (jsp) which will iterate over results. My question is what is the best
> > 
> > > way to do this. Do I create an array for each row in the resultset and
> > 
> > > insert each array in a collection, passing that back to the view?
> > >
> > 
> > That is certainly one approach.  Indeed, commons-beanutils has a useful
> > little class (org.apache.commons.beanutils.RowSetDynaClass) that is
> > ideally
> > suited to this use case.  It creates a List of DynaBeans representing
> > the
> > data content returned by the SELECT.  Because it makes a copy, you can
> > close
> > the result set (and return the connection back to the connection pool)
> > before forwarding to the page.
> > 
> > > If so, how would you iterate over each array in the collection with 
> > > the logic:iterate taglib? All of the examples only show iterations 
> > > over single column lists.
> > >
> > 
> > Let's assume you have done this in your Action:
> > 
> >   ResultSet rs = ...;
> >   RowSetDynaClass rsdc = new RowSetDynaClass(rs);
> >   rs.close();
> >   request.setAttribute("customers", rsdc.getList());
> > 
> > so you now have a request attribute containing the list.  Now, in your
> > page,
> > you can say things like:
> > 
> >   <logic:iterate id="customer" name="customers">
> >     Name is <bean:write name="customer" property="name"/>
> >     Status is <bean:write name="customer" property="status"/>
> >   </logic:iterate>
> > 
> > and so on.  Details of RowSetDynaClass are in the javadocs for
> > BeanUtils:
> > 
> >   http://jakarta.apache.org/commons/beanutils/
> > 
> > > Any help would be appreciated.
> > >
> > > Thanks,
> > > Richard
> > >
> > 
> > Craig

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: [OT]RE: Iterating the resultset contents in the view (jsp)

Posted by David Graham <gr...@yahoo.com>.
--- Vijay Balakrishnan <vb...@northpacific.com> wrote:
> Hi,
> 
> Is there a way to return this resultset from a SQL Query as an xml file
> using a beanutils class or Digester ?

I don't know of any class that does that.  Every application will require
its own XML format anyways.

David

> 
> Thanks,
> Vijay
> 
> -----Original Message-----
> From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
> Sent: Monday, July 14, 2003 7:25 PM
> To: Struts Users Mailing List
> Subject: Re: Iterating the resultset contents in the view (jsp)
> 
> 
> 
> 
> On Mon, 14 Jul 2003, Richard Hill wrote:
> 
> > Date: Mon, 14 Jul 2003 16:36:17 -0700
> > From: Richard Hill <RH...@classmates.com>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: "'struts-user@jakarta.apache.org'" 
> > <st...@jakarta.apache.org>
> > Subject: Iterating the resultset contents in the view (jsp)
> >
> > Hi,
> > I'm working on an action that gets a resultset from a database table 
> > containing 4 columns. I need to pass that information back to the view
> 
> > (jsp) which will iterate over results. My question is what is the best
> 
> > way to do this. Do I create an array for each row in the resultset and
> 
> > insert each array in a collection, passing that back to the view?
> >
> 
> That is certainly one approach.  Indeed, commons-beanutils has a useful
> little class (org.apache.commons.beanutils.RowSetDynaClass) that is
> ideally
> suited to this use case.  It creates a List of DynaBeans representing
> the
> data content returned by the SELECT.  Because it makes a copy, you can
> close
> the result set (and return the connection back to the connection pool)
> before forwarding to the page.
> 
> > If so, how would you iterate over each array in the collection with 
> > the logic:iterate taglib? All of the examples only show iterations 
> > over single column lists.
> >
> 
> Let's assume you have done this in your Action:
> 
>   ResultSet rs = ...;
>   RowSetDynaClass rsdc = new RowSetDynaClass(rs);
>   rs.close();
>   request.setAttribute("customers", rsdc.getList());
> 
> so you now have a request attribute containing the list.  Now, in your
> page,
> you can say things like:
> 
>   <logic:iterate id="customer" name="customers">
>     Name is <bean:write name="customer" property="name"/>
>     Status is <bean:write name="customer" property="status"/>
>   </logic:iterate>
> 
> and so on.  Details of RowSetDynaClass are in the javadocs for
> BeanUtils:
> 
>   http://jakarta.apache.org/commons/beanutils/
> 
> > Any help would be appreciated.
> >
> > Thanks,
> > Richard
> >
> 
> Craig
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org