You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Dean A. Hoover" <dh...@rochester.rr.com> on 2004/03/13 15:55:53 UTC

how to use bean:define for this

I am still on a steep learning curve for struts/tiles and
to a degree, trying to remember certain things about
JSP. Anyway, struts has this construct I want to use
for building an HTML select tagset from a java collection.

Here's the jsp snippet:
<html:select property="contactInfoWidgetsSubFormCountryCode">
<html:options collection="countries"
             property="value" labelProperty="label"/>
</html:select>

The reference to "countries" is a java.util.Collection that is
put into the session. I have a class I wrote "CountryOptions"
that contains one static method:

public static Collection getOptions()

that creates what it needs to. Now what I need to figure out is
the best way to instantiate the "countries" variable in the session.
I think it might be by using the bean:define tag, but I am not
sure. Has anyone done this before?

Thanks.
Dean


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


Re: how to use bean:define for this

Posted by Axel Gross <ka...@iaeste.at>.
or maybe use <jsp:useBean ..>
will create a default bean, if it can't find an instance
so, if you make a bean which uses the the static method for initialisation
of a collections property (in default constructor) you can go without the
scriptlet

regards,
axel

On 2004-03-13 at 14:08:13 -0500, Geeta Ramani wrote:
> Dean:
> 
> Yes, I do see your problem and since (as i inderstand it) Struts tags work via
> reflection, it needs an instance of the class before you can use <bean:define kind
> of things..  So my guess is you will have to bite the bullet and just use plain
> Java (remember the good ole days??..;)):
> 
> <%java.util.Collection allCountries = myClass.myStaticMethod.getCountries; %>
> 
> and then go ahead and use "allCountries" as usual later in your jsp.. I believe
> you can use it within struts tags with no problem..
> 
> Regards,
> Geeta
> 
> "Dean A. Hoover" wrote:
> 
> >
> > >
> > that's what I am trying to avoid... I am already doing it in an Action.
> > I want the
> > collection to be instantiating in JSP where it is used. The reason I am
> > putting it in
> > session is that the data comes out of a database but is quite static.
> > Are you suggesting
> > application scope or page? As I said in my previous message, I already
> > have a class
> > with a static method that will return a populated collection.
> >
> > Dean
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 

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


Re: how to use bean:define for this

Posted by Geeta Ramani <ge...@cmpco.com>.
Dean:

Yes, I do see your problem and since (as i inderstand it) Struts tags work via
reflection, it needs an instance of the class before you can use <bean:define kind
of things..  So my guess is you will have to bite the bullet and just use plain
Java (remember the good ole days??..;)):

<%java.util.Collection allCountries = myClass.myStaticMethod.getCountries; %>

and then go ahead and use "allCountries" as usual later in your jsp.. I believe
you can use it within struts tags with no problem..

Regards,
Geeta

"Dean A. Hoover" wrote:

>
> >
> that's what I am trying to avoid... I am already doing it in an Action.
> I want the
> collection to be instantiating in JSP where it is used. The reason I am
> putting it in
> session is that the data comes out of a database but is quite static.
> Are you suggesting
> application scope or page? As I said in my previous message, I already
> have a class
> with a static method that will return a populated collection.
>
> Dean


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


Re: how to use bean:define for this

Posted by "Dean A. Hoover" <dh...@rochester.rr.com>.
Geeta Ramani wrote:

>Dean:
>
>Oh, Ok, I think i see what your issue is now (shouldn't have hit that earlier
>"send" so fast..!)  Ok, in the action before you forward to the jsp, populate
>your countries variable.  Then set your session variable (in your Action
>class), say, session.setAttribute("countries", countries); (though i still
>wonder about making this a session var.. anyway..).
>
that's what I am trying to avoid... I am already doing it in an Action. 
I want the
collection to be instantiating in JSP where it is used. The reason I am 
putting it in
session is that the data comes out of a database but is quite static. 
Are you suggesting
application scope or page? As I said in my previous message, I already 
have a class
with a static method that will return a populated collection.

Dean

>
>In your jsp then you can simply say:
><bean:define id="allCountries" name="countries" scope="session"
>type="java.util.Collection" />
>
>You should then be able to access the "allCountries" Collection further down
>in your jsp..
>
>Hope this helps,
>Geeta
>
>  
>
>>>that creates what it needs to. Now what I need to figure out is
>>>the best way to instantiate the "countries" variable in the session.
>>>I think it might be by using the bean:define tag, but I am not
>>>sure. Has anyone done this before?
>>>
>>>Thanks.
>>>Dean
>>>      
>>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>  
>



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


Re: how to use bean:define for this

Posted by Geeta Ramani <ge...@cmpco.com>.
Dean:

Oh, Ok, I think i see what your issue is now (shouldn't have hit that earlier
"send" so fast..!)  Ok, in the action before you forward to the jsp, populate
your countries variable.  Then set your session variable (in your Action
class), say, session.setAttribute("countries", countries); (though i still
wonder about making this a session var.. anyway..).

In your jsp then you can simply say:
<bean:define id="allCountries" name="countries" scope="session"
type="java.util.Collection" />

You should then be able to access the "allCountries" Collection further down
in your jsp..

Hope this helps,
Geeta

> > that creates what it needs to. Now what I need to figure out is
> > the best way to instantiate the "countries" variable in the session.
> > I think it might be by using the bean:define tag, but I am not
> > sure. Has anyone done this before?
> >
> > Thanks.
> > Dean
>


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


Re: how to use bean:define for this

Posted by Geeta Ramani <ge...@cmpco.com>.
Dean:

First, you need this "countries" Collection available in kind of an
application-wide context, right?  I mean there is no reason to "attach"
this as a session variable since I imagine it would not change from user
to user...

Also such variables are best initialised/populated in a class (the model)
rather than in a jsp, since  jsps should just be used mainly for the
view..  So i guess I don't see why you are thinking of <bean:define tags
for instantiating this Collection...? Am i misunderstanding your
problem..?

Regards,
Geeta

"Dean A. Hoover" wrote:

> I am still on a steep learning curve for struts/tiles and
> to a degree, trying to remember certain things about
> JSP. Anyway, struts has this construct I want to use
> for building an HTML select tagset from a java collection.
>
> Here's the jsp snippet:
> <html:select property="contactInfoWidgetsSubFormCountryCode">
> <html:options collection="countries"
>              property="value" labelProperty="label"/>
> </html:select>
>
> The reference to "countries" is a java.util.Collection that is
> put into the session. I have a class I wrote "CountryOptions"
> that contains one static method:
>
> public static Collection getOptions()
>
> that creates what it needs to. Now what I need to figure out is
> the best way to instantiate the "countries" variable in the session.
> I think it might be by using the bean:define tag, but I am not
> sure. Has anyone done this before?
>
> Thanks.
> Dean
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org


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