You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chris Cheshire <ch...@gmail.com> on 2006/03/01 23:08:45 UTC

collection for html:optionsCollection in servlet context

I am building a select list to select a state for an address and since
it will be used in a few forms throughout the application I want to
make this as centralised as possible.

I have built a bean with getValue(), getLabel() attributes and created
a collection of them and placed as an attribute in the servlet context
via a servlet that is loaded on startup.

How do I retrieve this for use in the html:optionsCollection tag? I
know the servlet context is accessible via the implicit 'application'
variable in the jsp, but how do I access values from
application.getAttribute(attrName) without using a scriptlet?

Thanks

Chris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: collection for html:optionsCollection in servlet context

Posted by Chris Cheshire <ch...@gmail.com>.
On 3/1/06, Craig McClanahan <cr...@apache.org> wrote:
>
> 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.
>

Ahhh this is what I was missing! The reference I looked at said that
property was actually required (Jakarta Struts Pocket Reference -
Cavaness & Keaton).


>  You might then use it in a page like this:
>
>     <html:select ...>
>         <html:optionsCollection name="states" value="abbreviation"
> label="name"/>
>     </html:select>
>

Thanks Craig, this works :)

Chris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: collection for html:optionsCollection in servlet context

Posted by Craig McClanahan <cr...@apache.org>.
On 3/1/06, Chris Cheshire <ch...@gmail.com> 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

Re: collection for html:optionsCollection in servlet context

Posted by Michael Jouravlev <jm...@gmail.com>.
I see. What if you wrap your list into another bean, say StateHolder.
Then you stick StateHolder in the application context under name
"stateHolder". Then you use something like this:

<html:optionsCollection name="stateHolder" property="stateList" ... />

On 3/1/06, Chris Cheshire <ch...@gmail.com> 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.
>
> 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.
>
> 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.
>
> Chris
>
> On 3/1/06, Michael Jouravlev <jm...@gmail.com> wrote:
> > On 3/1/06, Chris Cheshire <ch...@gmail.com> wrote:
> > > Michael,
> > >
> > > Thanks for the reference, but it doesn't explain very much.
> > >
> > > Yes the widget looks like what I want, but is it something I download?
> >
> > You lost me.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: collection for html:optionsCollection in servlet context

Posted by Chris Cheshire <ch...@gmail.com>.
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.

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.

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.

Chris

On 3/1/06, Michael Jouravlev <jm...@gmail.com> wrote:
> On 3/1/06, Chris Cheshire <ch...@gmail.com> wrote:
> > Michael,
> >
> > Thanks for the reference, but it doesn't explain very much.
> >
> > Yes the widget looks like what I want, but is it something I download?
>
> You lost me.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: collection for html:optionsCollection in servlet context

Posted by Michael Jouravlev <jm...@gmail.com>.
On 3/1/06, Chris Cheshire <ch...@gmail.com> wrote:
> Michael,
>
> Thanks for the reference, but it doesn't explain very much.
>
> Yes the widget looks like what I want, but is it something I download?

You lost me.

> Is it something that exists in the struts tag libraries? Does the
> states collection exist there?

What "this"? The collection is created by you in your program.

> What happens if I wanted to do states
> for Australia for instance, where would I obtain those?

I don't know. Australian postal service I guess.

> I understand how to write the struts tags to build the list, I don't
> quite know how to obtain a reference to a collection I have built and
> stored in the servlet context.

Have you tried to put collection name in the "name" attribute of
<html:optionscollection>? Does it not work? Do you have another object
with this name in another scope?

Have you looked in the OptionsCollectionTag source code? It uses
pageContext.findAttribute(name) to locate a bean by name.

Michael.

> On 3/1/06, Michael Jouravlev <jm...@gmail.com> wrote:
> > On 3/1/06, Chris Cheshire <ch...@gmail.com> wrote:
> > > I am building a select list to select a state for an address and since
> > > it will be used in a few forms throughout the application I want to
> > > make this as centralised as possible.
> > >
> > > I have built a bean with getValue(), getLabel() attributes and created
> > > a collection of them and placed as an attribute in the servlet context
> > > via a servlet that is loaded on startup.
> > >
> > > How do I retrieve this for use in the html:optionsCollection tag?
> >
> > http://wiki.apache.org/struts/StrutsWidgets

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: collection for html:optionsCollection in servlet context

Posted by Chris Cheshire <ch...@gmail.com>.
Michael,

Thanks for the reference, but it doesn't explain very much.

Yes the widget looks like what I want, but is it something I download?
Is it something that exists in the struts tag libraries? Does the
states collection exist there? What happens if I wanted to do states
for Australia for instance, where would I obtain those?

I understand how to write the struts tags to build the list, I don't
quite know how to obtain a reference to a collection I have built and
stored in the servlet context.

Chris

On 3/1/06, Michael Jouravlev <jm...@gmail.com> wrote:
> On 3/1/06, Chris Cheshire <ch...@gmail.com> wrote:
> > I am building a select list to select a state for an address and since
> > it will be used in a few forms throughout the application I want to
> > make this as centralised as possible.
> >
> > I have built a bean with getValue(), getLabel() attributes and created
> > a collection of them and placed as an attribute in the servlet context
> > via a servlet that is loaded on startup.
> >
> > How do I retrieve this for use in the html:optionsCollection tag?
>
> http://wiki.apache.org/struts/StrutsWidgets
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: collection for html:optionsCollection in servlet context

Posted by Michael Jouravlev <jm...@gmail.com>.
On 3/1/06, Chris Cheshire <ch...@gmail.com> wrote:
> I am building a select list to select a state for an address and since
> it will be used in a few forms throughout the application I want to
> make this as centralised as possible.
>
> I have built a bean with getValue(), getLabel() attributes and created
> a collection of them and placed as an attribute in the servlet context
> via a servlet that is loaded on startup.
>
> How do I retrieve this for use in the html:optionsCollection tag?

http://wiki.apache.org/struts/StrutsWidgets

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org