You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pat Young <pa...@yahoo.com> on 2004/02/04 05:11:36 UTC

Need to pass data to JSP in ActionForm

I have the follow scenerio that I am trying to code
for.

1.  I have a JSP with a form.  This page ios called
"sales.jsp".
2.  The form has a select box in it that displays a
list of products.  The page grabs the products list
Bean from the Request.  There are also additional
fields in the form.
3.  The list of products is retrieved with a call to
our service layer in the Action for the JSP page. 
This Action is called "sales.do".
4.  The submit button for the form submits the data on
the form to "salesAdjust.do".
5.  The salesAdjust.do Action has an ActionForm
associated to it called "salesForm".  This form has a
validate method for validating the input.  If the
validate fails, controls is forwarded back to the
sales.jsp page.  Therefore, sales.jsp is the "input"
property for the salesAdjust.do Action.
6.  The problem I am having is that if the ActionForm
failes validation, then the sales.jsp page gets an
error becaue it can not find the bean in the Request
for the products list.  Should I build this bean in my
validate and place it in the request at the beginning
of validate?  If validate fails, there is no other
opportunity to build the products list and get it into
the Request.
7.  I typically uses Actions to retrieves and build
beans and then add them to the request for the jsp to
access.  Is this an acceptable practice also?

Any help would be greatly appreciated.

Pat Young

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

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


Re: Need to pass data to JSP in ActionForm

Posted by Pat Young <pa...@yahoo.com>.
I am working in a multi-server, multi-application
environment, so placing objects in the session isn't a
simple option.  I have to worry about the user getting
the same session and also that I don't use too much
memory.

When I said "build", I did mean that I am calling a
business object to get the list.

I guess my issue with doing this in the validate
method, is the validate method is meant for validation
logic and not for processing.  I guess it depends on
how you look at it.

Could you breifly mention some of the other
alternatives?

Thanks,
Pat Young


--- Rick Reumann <st...@reumann.net> wrote:
> Pat Young wrote:
> 
> > 6.  The problem I am having is that if the
> ActionForm
> > failes validation, then the sales.jsp page gets an
> > error becaue it can not find the bean in the
> Request
> > for the products list.  Should I build this bean
> in my
> > validate and place it in the request at the
> beginning
> > of validate?  If validate fails, there is no other
> > opportunity to build the products list and get it
> into
> > the Request.
> 
> Personally I often opt for the easy solution and put
> the List in Session 
> scope, but there are other alternatives that have
> been discussed on this 
> list before.
> 
> > 7.  I typically uses Actions to retrieves and
> build
> > beans and then add them to the request for the jsp
> to
> > access.  Is this an acceptable practice also?
> 
> Depends what you mean by "build beans." Nothing
> wrong wtih setting stuff 
> into Request scope from your Actions, but make sure
> you are calling some 
> business process that does the actual "building."
> For example...
> 
> //in your Action:
> List someList = someBusinessClass.getMyList();
> request.setAttribute("listWhatever", someList);
> 
> -- 
> Rick
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

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


RE: Need to pass data to JSP in ActionForm

Posted by Pat Young <pa...@yahoo.com>.
I like the adapter idea.  Does the adapter contain all
data that is required by the jsp page?  so fomr a
modelling perspective, do you have adapter that match
the pages?

Thanks,
Pat Young

--- Andrew Hill <an...@gridnode.com>
wrote:
> If the list doesnt change during the course of a
> users session then the best
> thing to do is to create it on first demand and
> store it in the session.
> This saves you from looking it up from the db
> repeatedly when unnecessary
> and is thus more efficient. If the list is the same
> for all users and doesnt
> change at all then you could store it in the servlet
> (application) context.
> 
> If it does change (for example when new records are
> added) then you will
> still need to look it up each time. In this case
> your best bet might be to
> extend Rick's suggestion about having a seperate
> helper class that assembles
> the list (always a good idea). This
> helper/manager/thing is part of your
> business process layer. Above that you could have an
> aditional (very simple)
> adaptor class that is passed a request, calls this
> helper to obtain the
> list, and stores said list under the appropriate
> request attribute.
> 
> Your validate method could thus call this adapter to
> setup the list when it
> is validating the entered data. Your setup action,
> likewise would call the
> adaptor class rather than directly calling the
> business layer and storing
> the result.
> 
> One thing to beware of when you do such non-trivial
> things in the
> actionform, is that if memory serves me correctly
> (someone please correct me
> if Im wrong on this!), exceptions thrown here wont
> be handled by the
> exception handler you configured in struts config as
> the try/catch that
> invokes that handler is merely around the invocation
> of the actual action
> execute and not the form reset / validate stuff.
> 
> These are some of the reasons I personally prefer to
> do my validation in the
> action itself and dont make use of the forms
> validate method. (Of course if
> your using struts validator that becomes problematic
> too)
> 
> hth
> Andrew
> 
> 
> -----Original Message-----
> From: Rick Reumann [mailto:struttin@reumann.net]
> Sent: Wednesday, 4 February 2004 12:26
> To: Struts Users Mailing List
> Subject: Re: Need to pass data to JSP in ActionForm
> 
> 
> Pat Young wrote:
> 
> > 6.  The problem I am having is that if the
> ActionForm
> > failes validation, then the sales.jsp page gets an
> > error becaue it can not find the bean in the
> Request
> > for the products list.  Should I build this bean
> in my
> > validate and place it in the request at the
> beginning
> > of validate?  If validate fails, there is no other
> > opportunity to build the products list and get it
> into
> > the Request.
> 
> Personally I often opt for the easy solution and put
> the List in Session
> scope, but there are other alternatives that have
> been discussed on this
> list before.
> 
> > 7.  I typically uses Actions to retrieves and
> build
> > beans and then add them to the request for the jsp
> to
> > access.  Is this an acceptable practice also?
> 
> Depends what you mean by "build beans." Nothing
> wrong wtih setting stuff
> into Request scope from your Actions, but make sure
> you are calling some
> business process that does the actual "building."
> For example...
> 
> //in your Action:
> List someList = someBusinessClass.getMyList();
> request.setAttribute("listWhatever", someList);
> 
> --
> Rick
> 
>
---------------------------------------------------------------------
> 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
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

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


RE: Need to pass data to JSP in ActionForm

Posted by Andrew Hill <an...@gridnode.com>.
If the list doesnt change during the course of a users session then the best
thing to do is to create it on first demand and store it in the session.
This saves you from looking it up from the db repeatedly when unnecessary
and is thus more efficient. If the list is the same for all users and doesnt
change at all then you could store it in the servlet (application) context.

If it does change (for example when new records are added) then you will
still need to look it up each time. In this case your best bet might be to
extend Rick's suggestion about having a seperate helper class that assembles
the list (always a good idea). This helper/manager/thing is part of your
business process layer. Above that you could have an aditional (very simple)
adaptor class that is passed a request, calls this helper to obtain the
list, and stores said list under the appropriate request attribute.

Your validate method could thus call this adapter to setup the list when it
is validating the entered data. Your setup action, likewise would call the
adaptor class rather than directly calling the business layer and storing
the result.

One thing to beware of when you do such non-trivial things in the
actionform, is that if memory serves me correctly (someone please correct me
if Im wrong on this!), exceptions thrown here wont be handled by the
exception handler you configured in struts config as the try/catch that
invokes that handler is merely around the invocation of the actual action
execute and not the form reset / validate stuff.

These are some of the reasons I personally prefer to do my validation in the
action itself and dont make use of the forms validate method. (Of course if
your using struts validator that becomes problematic too)

hth
Andrew


-----Original Message-----
From: Rick Reumann [mailto:struttin@reumann.net]
Sent: Wednesday, 4 February 2004 12:26
To: Struts Users Mailing List
Subject: Re: Need to pass data to JSP in ActionForm


Pat Young wrote:

> 6.  The problem I am having is that if the ActionForm
> failes validation, then the sales.jsp page gets an
> error becaue it can not find the bean in the Request
> for the products list.  Should I build this bean in my
> validate and place it in the request at the beginning
> of validate?  If validate fails, there is no other
> opportunity to build the products list and get it into
> the Request.

Personally I often opt for the easy solution and put the List in Session
scope, but there are other alternatives that have been discussed on this
list before.

> 7.  I typically uses Actions to retrieves and build
> beans and then add them to the request for the jsp to
> access.  Is this an acceptable practice also?

Depends what you mean by "build beans." Nothing wrong wtih setting stuff
into Request scope from your Actions, but make sure you are calling some
business process that does the actual "building." For example...

//in your Action:
List someList = someBusinessClass.getMyList();
request.setAttribute("listWhatever", someList);

--
Rick

---------------------------------------------------------------------
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: Need to pass data to JSP in ActionForm

Posted by Rick Reumann <st...@reumann.net>.
Pat Young wrote:

> 6.  The problem I am having is that if the ActionForm
> failes validation, then the sales.jsp page gets an
> error becaue it can not find the bean in the Request
> for the products list.  Should I build this bean in my
> validate and place it in the request at the beginning
> of validate?  If validate fails, there is no other
> opportunity to build the products list and get it into
> the Request.

Personally I often opt for the easy solution and put the List in Session 
scope, but there are other alternatives that have been discussed on this 
list before.

> 7.  I typically uses Actions to retrieves and build
> beans and then add them to the request for the jsp to
> access.  Is this an acceptable practice also?

Depends what you mean by "build beans." Nothing wrong wtih setting stuff 
into Request scope from your Actions, but make sure you are calling some 
business process that does the actual "building." For example...

//in your Action:
List someList = someBusinessClass.getMyList();
request.setAttribute("listWhatever", someList);

-- 
Rick

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