Bruce Bantos wrote:

> I am having trouble understanding what a NESTED, as opposed to an INDEXED
> property is in Java Beans. Here is a snip from the JSP 0.92 spec document,
> detailing the properties of a bean named Ticker. My questions are: In the
> example discussed below, if the property 'companyName[]' is just an array of
> strings, how can the property 'latest' (double) be nested within
> companyName[]? Are all these properties located in the same bean? Where
> would the "getLatest()" accessor method reside? Thanks for helping me noodle
> through this.
>
> Bruce
>

To use nested beans (in the 0.92 syntax), I would organize things a little
differently, by having two beans:

Bean "Market" has the following property accessors (among others):
    String getName();    // i.e. scalar property
    Company[] getCompany();    // i.e. indexed property

Bean "Company" has the following property accessors (among others):
    String getAbbreviation();
    Double getHigh();
    Double getLow();
    Double getLatest();

Note that the value returned by the getCompany() accessor of the Market bean is
in fact an array of another bean type.  That's where the nesting comes in.  (In
fact "Company.java" can actually be an interface, not a class -- I've used
beans that return an RMI interface to a remote object, and JSP still works
quite happily because it can introspect interfaces too).

Now, let's say I wanted to print out the stock market page (like the stock
quotes in a newspaper) for this market (this is still 0.92 syntax -- you have
to go back to scriptlets in 1.0, but the concepts are not that much different):

<usebean name="market" type="my.package.Market">
<!-- whatever other stuff you need to set it up -->
</usebean>

<table>

    <tr>
        <th>Stock</th>
        <th>High</th>
        <th>Low</th>
        <th>Latest</th>
    </tr>

<LOOP PROPERTY="market:company" PROPERTYELEMENT="x">
    <tr>
        <td><DISPLAY PROPERTY="x:abbreviation"></td>
        <td><DISPLAY PROPERTY="x:high"></td>
        <td><DISPLAY PROPERTY="x.low"></td>
        <td><DISPLAY PROPERTY="x:latest"></td>
   </tr>
</LOOP>

</table>

The variable "x" acts like an iterator through the multiple Company objects
returned by the getCompany() method, and one row of the table gets created for
each company.  Because JSP knows that an "x" object really is a Company, it can
introspect on the properties of Company, even though you didn't mention it
explicitly with a USEBEAN tag.  You can also nest this more than one level, if
a Company had a property that was itself a bean.

Nesting is not restricted to cases where the "outer" property is indexed --
that is where the syntax with multiple colons comes in.  Assume in the above
example that a market had only one company (i.e. it was Company getCompany()
instead of Company[] getCompany()).  Then, you could refer to the "latest"
value of that company with something like:

    <DISPLAY PROPERTY="market:company:latest">

But this won't work if company is an indexed property, because you are not
telling it which subscript to access.

Craig McClanahan


>
> -----------------SNIP-----------------------------------------
> Suppose you had a Bean named Ticker that provided a stock market ticker. The
> Bean tracks companies on different stock markets and displays a list of data
> about each stock. For example, the Ticker bean might track three companies
> on the New York Stock Exchange and three companies on the NASDAQ and display
> the stock's latest price during trading (updated at intervals), its high and
> low prices for the day, and its previous closing price.
>
> The Bean developer has already given Ticker a few properties:
>
> 'market', which describes the name of a stock market
>
> 'companyName[]', which is nested in market and is an array containing the
> names of the companies Ticker tracks on that market
>
> 'latest', which is nested in companyName and returns the stock's latest
> price, updated at regular intervals during trading
>
> 'high', which is nested in companyName and represents the stock's high price
> for the day
>
> 'low', which is nested in companyName and represents the stock's low price
> for the day
>
> JavaServer Pages notation, we would list the properties this way:
>
> market (a String)
> market:companyName[] (an array of String values)
> market:companyName:latest (a Double)
>
> etc etc.
>
> ----------------------------------------------------------------------------
> -----------------
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JSP-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to