You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nilsa Gonzalez <ng...@earthlink.net> on 2000/08/23 04:31:13 UTC
Bean name in htmlProperty and iterate
I tried using the enumerate and html property tags. I have problems with
the bean name. Can anyone please tell me what is the correct usage of
these tags. The bean I am trying to use is Objectn.java. How to the jsp
knows what is the Bean that is using.
<struts:htmlProperty name="objectn" property="parent">
</struts:htmlProperty>
How do I tell the framework to use the Objectn.java bean?
Thanks you for your help...
Nilsa
Re: Bean name in htmlProperty and iterate
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Nilsa Gonzalez wrote:
> I tried using the enumerate and html property tags. I have problems with
> the bean name. Can anyone please tell me what is the correct usage of
> these tags. The bean I am trying to use is Objectn.java. How to the jsp
> knows what is the Bean that is using.
>
> <struts:htmlProperty name="objectn" property="parent">
> </struts:htmlProperty>
> How do I tell the framework to use the Objectn.java bean?
>
The <struts:htmlProperty> tag just causes the contents of the specified
property to be output to the browser. It doesn't create anything useful for
iteration.
As an example of using the <struts:enumerate> tag, let's assume you've got a
bean of class CustomerBean, under attribute "customer". Further, let's
assume that this class has the following method:
Enumeration getOrders()
that returns an Enumeration of all the orders (objects of class OrderBean)
that are current for this customer. (The enumerate tag would also work if
the return value were an array or Vector of OrderBean objects.) You are
writing a JSP page that should report all of the outstanding orders in a
nice report format.
One approach to doing the report, using the enumerate tag, would be
something like this:
<jsp:useBean id="customer" class="com.mycompany.CustomerBean"
scope="request"/> <!-- Assume it was put here by an action class -->
<table>
<tr>
<th>Order ID</th>
<th>Total Amount</th>
</tr>
<struts:enumerate id="order" name="customer" property="orders">
<tr>
<td><struts:htmlProperty name="order" property="id"/></td>
<td><struts:htmlProperty name="order" property="total"/></td>
</tr>
</struts:enumerate>
</table>
In this scenario, the <struts:enumerate> tag is told to go access the
"orders" property of the "customer" bean. Since this returns an
Enumeration, the body of the <struts:enumerate> tag is looped -- once per
element in the enumeration (i.e. once per order). Each time through the
loop, the "order" bean contains the current OrderBean object, which is
assumed for our purposes here to have getId() and getTotal() methods that
return the order id and total order amount, respectively.
There are lots of other usage patterns supported by the enumerate tag, but
this is probably the most common one.
>
> Thanks you for your help...
> Nilsa
Craig