You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jeff Smith <je...@centralscheduling.net> on 2003/03/07 01:21:02 UTC

Multiple beans in request scope?

I'm building my first real struts app. (Okay, its also my first real Java app, my first real servlet app and a bunch of other firsts, but who's counting? :-)

I'd like to present a form that contains a few selection lists, with values taken from a few tables in my database.

Let's say the lists are:
1) A list of documents the user can have sent to him
2) A list of transport methods he can choose (ftp, email, snail mail, etc...)
3) A list of credit cards that we currently accept.

Obviously, these are logically disparate lists, so I would rather not create a form-specific bean to encapsulate all this information.

What I'd *REALLY* like to do is have a DocsBean, a DeliveryOptionsBean and a CreditCardBean that each load themselves from the database. And in a perfect world, I'd like to be able to specify (in the action tag of my struts-config) that form UserOrder wants all three of those beans loaded into the request scope.

Is that possible? It appears to me that the action tag only takes a single formbean name, so I am assuming I have to find a different way to do what I want. Any suggestions?

Failing that, I suppose I could chain together a couple of actions: a) a pre-action that would load my beans for me and then b) the actual forward to the userform.jsp. But that seems a bit tedious. (Why write by hand what could be abstracted out and built into the next version of Struts?)

I've got lots more questions, but I'll try to keep them in separate threads.

Jefficus

Re: Multiple beans in request scope?

Posted by Jeff Smith <je...@centralscheduling.net>.
There's actually a couple of different issues here.

The first is this: I want to have a quasi-dynamic form that relies on
information in the database to control some of the options presented in the
form itself. I'm using a fictitious example to keep things simple. Suppose I
have a storefront application and the list of credit cards we support is
maintained in the database. (This way, when we add support for more cards,
we just update the database and the payment form will automatically present
them.)

So I need a bean that contains the list of supported cards. And I need that
bean populated before my form is displayed - since the form relies on the
bean contents to display itself.

How do I get that bean to load before the page loads? I can think of a
couple of ways:

1) Declare the bean in the name field of the action item for the form. (As I
understand it, this will cause it to be instantiated and placed whatever
scope I put in the scope field.) As long as my bean constructor has db
loading logic, this would give me my bean, loaded and ready for use when I
start rendering my JSP.

2) I could chain two actions together. The first would be a preparation
action that manually instantiated the bean, populated it and placed it in
the scope. The success forward for that first action would be the actual
display action.

Either of those solutions will work. (There may be a couple of others I
haven't considered.) But let's complicate the problem a bit more. On this
same page, I want to include a few other elements that are also drawn from
the database. In my previous message, I suggested that they might be other
lists (like the list of documents available for purchase and the list of
delivery methods offered). But it could be any db-driven information.

So now I have two more beans (keeping with my original example). One that
lists delivery methods and another that lists documents. Since they are also
db-driven and since they are also part of the form presentation, they must
also be loaded and scoped before the form is displayed.

Now method 2 from above still works, since my preparation action has no
limits on how many beans it can load, but method 1 no longer works, because
struts only supports one bean being automatically loaded for an action.

Richard also suggested that I could create a "parent" bean that in turn
loaded each of the list-beans. This approach is similar to my
preparation-action solution.

Both of these methods will work, but they require additional code every time
I want to simply make some basic information available to my form. If one of
those lists is used on a dozen different forms, I have to write a lot of
code to instantiate them each time.

Wouldn't it be powerful if the <action> tag supported multiple <formbean>
tags within it? In that case, I could do something like this:

<action
    path="/ShowForm"
    type="org.apache.struts.actions.ForwardAction"
    parameter="MyForm.jsp" >
    <formbean name="supportedCreditCardsBean" scope="request"/>
    <formbean name="supportedDeliveryOptionsBean" scope="request"//>
    <formbean name="documentsAvailableBean" scope="request"//>
</action>

This construct would allow me to make all kinds of information available to
build my form without having to write special prep-Action handlers or
aggregation beans. In fact, it could be even more flexible by adding a
parameter attribute to the formbean tag that allows me to specify an
initialization method to invoke on the bean when it gets loaded.
    <formbean name="supportedCreditCardsBean" scope="request"
parameter="loadMe"/>

I was hoping somebody would say "Yeah, you can do that."  But I guess not.

Jefficus


----- Original Message -----
From: "alexj" <al...@freesurf.ch>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, March 06, 2003 6:50 PM
Subject: Re: Multiple beans in request scope?


> Sure you can use mutliple beans in your scope.
>
> But I didn't see the main case, it will be better for us (for me lol) to
> understand the whole context of your application.
>
> Cheers.
>
> --
> Alexandre Jaquet
>
> ----- Original Message -----
> From: "Jeff Smith" <je...@centralscheduling.net>
> To: <st...@jakarta.apache.org>
> Sent: Friday, March 07, 2003 1:21 AM
> Subject: Multiple beans in request scope?
>
>
> I'm building my first real struts app. (Okay, its also my first real Java
> app, my first real servlet app and a bunch of other firsts, but who's
> counting? :-)
>
> I'd like to present a form that contains a few selection lists, with
values
> taken from a few tables in my database.
>
> Let's say the lists are:
> 1) A list of documents the user can have sent to him
> 2) A list of transport methods he can choose (ftp, email, snail mail,
> etc...)
> 3) A list of credit cards that we currently accept.
>
> Obviously, these are logically disparate lists, so I would rather not
create
> a form-specific bean to encapsulate all this information.
>
> What I'd *REALLY* like to do is have a DocsBean, a DeliveryOptionsBean and
a
> CreditCardBean that each load themselves from the database. And in a
perfect
> world, I'd like to be able to specify (in the action tag of my
> struts-config) that form UserOrder wants all three of those beans loaded
> into the request scope.
>
> Is that possible? It appears to me that the action tag only takes a single
> formbean name, so I am assuming I have to find a different way to do what
I
> want. Any suggestions?
>
> Failing that, I suppose I could chain together a couple of actions: a) a
> pre-action that would load my beans for me and then b) the actual forward
to
> the userform.jsp. But that seems a bit tedious. (Why write by hand what
> could be abstracted out and built into the next version of Struts?)
>
> I've got lots more questions, but I'll try to keep them in separate
threads.
>
> Jefficus
>
>
>
> ---------------------------------------------------------------------
> 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: Multiple beans in request scope?

Posted by alexj <al...@freesurf.ch>.
Sure you can use mutliple beans in your scope.

But I didn't see the main case, it will be better for us (for me lol) to
understand the whole context of your application.

Cheers.

--
Alexandre Jaquet

----- Original Message -----
From: "Jeff Smith" <je...@centralscheduling.net>
To: <st...@jakarta.apache.org>
Sent: Friday, March 07, 2003 1:21 AM
Subject: Multiple beans in request scope?


I'm building my first real struts app. (Okay, its also my first real Java
app, my first real servlet app and a bunch of other firsts, but who's
counting? :-)

I'd like to present a form that contains a few selection lists, with values
taken from a few tables in my database.

Let's say the lists are:
1) A list of documents the user can have sent to him
2) A list of transport methods he can choose (ftp, email, snail mail,
etc...)
3) A list of credit cards that we currently accept.

Obviously, these are logically disparate lists, so I would rather not create
a form-specific bean to encapsulate all this information.

What I'd *REALLY* like to do is have a DocsBean, a DeliveryOptionsBean and a
CreditCardBean that each load themselves from the database. And in a perfect
world, I'd like to be able to specify (in the action tag of my
struts-config) that form UserOrder wants all three of those beans loaded
into the request scope.

Is that possible? It appears to me that the action tag only takes a single
formbean name, so I am assuming I have to find a different way to do what I
want. Any suggestions?

Failing that, I suppose I could chain together a couple of actions: a) a
pre-action that would load my beans for me and then b) the actual forward to
the userform.jsp. But that seems a bit tedious. (Why write by hand what
could be abstracted out and built into the next version of Struts?)

I've got lots more questions, but I'll try to keep them in separate threads.

Jefficus



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


Re: Multiple beans in request scope?

Posted by Richard Raquepo <rr...@primeorion.com>.
Try encapsulating all the three beans in one bean.

----- Original Message -----
From: "Jeff Smith" <je...@centralscheduling.net>
To: <st...@jakarta.apache.org>
Sent: Friday, March 07, 2003 8:21 AM
Subject: Multiple beans in request scope?


I'm building my first real struts app. (Okay, its also my first real Java
app, my first real servlet app and a bunch of other firsts, but who's
counting? :-)

I'd like to present a form that contains a few selection lists, with values
taken from a few tables in my database.

Let's say the lists are:
1) A list of documents the user can have sent to him
2) A list of transport methods he can choose (ftp, email, snail mail,
etc...)
3) A list of credit cards that we currently accept.

Obviously, these are logically disparate lists, so I would rather not create
a form-specific bean to encapsulate all this information.

What I'd *REALLY* like to do is have a DocsBean, a DeliveryOptionsBean and a
CreditCardBean that each load themselves from the database. And in a perfect
world, I'd like to be able to specify (in the action tag of my
struts-config) that form UserOrder wants all three of those beans loaded
into the request scope.

Is that possible? It appears to me that the action tag only takes a single
formbean name, so I am assuming I have to find a different way to do what I
want. Any suggestions?

Failing that, I suppose I could chain together a couple of actions: a) a
pre-action that would load my beans for me and then b) the actual forward to
the userform.jsp. But that seems a bit tedious. (Why write by hand what
could be abstracted out and built into the next version of Struts?)

I've got lots more questions, but I'll try to keep them in separate threads.

Jefficus



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