struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: displaying content form db w/ struts
Date Fri, 01 Sep 2000 16:38:45 GMT
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



Mime
View raw message