On 3/1/06, Chris Cheshire <[EMAIL PROTECTED]> wrote:
>
> You lost me with that page too. The use of the word widget threw me
> completely because I am used to seeing it reference to a concrete
> component :)
>
> I understand how to write the struts tag. Let's step back a bit. I'll
> see if I can explain this better.
>
> My question is how do I reference the collection for use in the when
> it is stored via an attribute in the servlet context - ie the
> ServletContext.setAttribute(attributeName, attributeValue) method
> without using a scriptlet.
>
> I have a servlet, loaded on startup, that in the init(ServletConfig)
> method, creates a List of beans containing state information - name
> and abbreviation. I have added this to the servlet context under the
> attribute name of "states".
>
> The value for the name attribute in the html:optionsCollection tag
> would be "application" - the implicit JSP variable that refers to the
> servlet context that the page is in.


Actually, the value of the name attribute should be "states".  The tag will
search request/session/application scope (in that order) for an attribute
with that name, and use the first one it finds, so be sure you do not have
something named "states" stored in any other scope.

Now the problem lies in the value for the property attribute of the
> html:optionsCollection. It is supposed to be a property of "name" such
> that is just a getBlah() method. However this is impossible on the
> servlet context, because the collection resides as part of the
> attribute map within the session context. The attribute name in the
> map is "states", but I can't use "states" as the property value
> because there is no getStates() method under ServletContext.


If the thing stored under attribute "states" in application scope is, in
fact, a List of beans, you won't need the "property" attribute at all.
Specifying a "name" but not a "property" tells the tag to use the attribute
value itself.

You would use the "property" attribute only if the thing stored under
"states" was a JavaBean that had a getStates() method or something to return
the list.

How do I retrieve this collection out of the servlet context attribute
> map for use in the html:optionsCollection tag WITHOUT using a
> scriptlet to make the collection available as some page scoped
> collection? Is this possible? I don't want scriptlets exposed in the
> JSP page itself. If I have to use one I will just write a tag to do
> it.


Putting it together, lets assume you have a StateBean class:

    public class StateBean {
        ...
        public String getAbbreviation();
        public String getName();
        ...
    }

and that what you store in application scope under key "states" is a List of
these beans.  You might then use it in a page like this:

    <html:select ...>
        <html:optionsCollection name="states" value="abbreviation"
label="name"/>
    </html:select>

We had to specify "value" and "label" attributes here because the property
names in the StateBean did not match the defaults supported by the tag.  If
the method names were getValue() and getLabel(), you would not have needed
these two attributes -- but there's no problem with using them if your
JavaBeans already have different property names.

Chris


Craig

Reply via email to