You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nuorteva Kare <Ka...@satama.com> on 2000/09/01 14:53:09 UTC

RE: displaying content form db w/ struts

Hi,

A clarification concerning my previous posting...

I usually use this kind of jsp for displaying database content:

<jsp:useBean id="foo" class="foo.bar.Foobar"/>
<% while (foo.next()) { %>
    <jsp:getProperty name="foo" property="myProperty"/><br>
<% } %>

and the Foobar class is kind of list iterator with following API:
public String getMyProperty()
public boolean next()
class has internal list and calling next method iterates through that list
step by step and by that way the values of every properties change.

I would like to get rid of all jsp scriptlets in my jsp, so what is the
correct way to achieve this?

Cheers,
Kare 8^)

-- 
Kare Nuorteva, Software Engineer
Satama UK Ltd
mobile +44 (0)7989 852 865
http://www.satama.co.uk/ 


> -----Original Message-----
> From: Nuorteva Kare [mailto:Kare.Nuorteva@satama.com]
> Sent: Thursday, August 31, 2000 12:41 PM
> To: 'struts-user@jakarta.apache.org'
> Subject: displaying content form db w/ struts
> 
> 
> Hi,
> 
> What is the best way (if thinking about presentation and 
> logic separation)
> to show for example a list of products in e-commerce shop, 
> these products
> are of course saved in a database.
> 
> Example:
> 
> database table:
> Product
> int id
> varchar(255) name
> varchar(20) price
> 
> Should I create a bean Product with three properties (id/name/price)?
> And some kind of a ProductIterator with following methods:
> public boolean hasNext()
> public Product next()
> initialisation is done in constructor (or in init() method if wanted).
> 
> Is it possible to use <struts:iterate> tag to view the products?
> What is the correct way for doing this?
> I know that I could use a scriptlet for this, but I don't want to...
> Should I create my own custom tags for different database views?
> 
> Cheers,
> Kare 8%)
> 
> -- 
> Kare Nuorteva, Software Engineer
> Satama UK Ltd
> mobile +44 (0)7989 852 865
> http://www.satama.co.uk/ 
> 

Re: displaying content form db w/ struts

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Nuorteva Kare wrote:

> Hi,
>
> A clarification concerning my previous posting...
>
> I usually use this kind of jsp for displaying database content:
>
> <jsp:useBean id="foo" class="foo.bar.Foobar"/>
> <% while (foo.next()) { %>
>     <jsp:getProperty name="foo" property="myProperty"/><br>
> <% } %>
>
> and the Foobar class is kind of list iterator with following API:
> public String getMyProperty()
> public boolean next()
> class has internal list and calling next method iterates through that list
> step by step and by that way the values of every properties change.
>
> I would like to get rid of all jsp scriptlets in my jsp, so what is the
> correct way to achieve this?
>

FIrst, you'll need to modify your underlying class to make its enumeration or
iterator (depending on which JDK you are using) visible to callers.  We'll use
an Iterator and the corresponding <struts:iterate> tag in this example; the
changes for JDK 1.1 would be to use an Enumeration and the <struts:enumerate>
tag instead -- but the concepts are the same.

So, let's add a method to your bean:

   public class Foo {
        public Iterator getBars();
    }

that makes the Iterator explicitly available.  Now, we can banish all the
scriptlets like this:

    <jsp:useBean id="foo" class="foo.bar.Foo"/>
    <struts:iterate id="bar" name="foo" property="bars">
        The current element's name is
        <jsp:getProperty name="bar" property="name"/>
    </struts:iterate>

Inside the iterate tag, we have said that the page-scope bean named "bar"
contains the current element of the iteration, and it can be accessed like any
other bean.  The nested body of the tag will be repeated once per element of
the interation.

The <struts:iterate> tag is actually pretty smart -- the property value it
accesses can be an Iterator (as we did above), an array of objects, a Map (the
values will be iterated), or a List (like an ArrayList).

>
> Cheers,
> Kare 8^)
>

Craig McClanahan

====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat