You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Ludwig, Pat" <pl...@silverstream.com> on 2000/08/23 19:11:18 UTC
getting access to the HTTPSession from an ActionForm bean
hi,
is there a preferred way in a Struts application to get access to
the HTTPSession? i have an app which requires initial values for the
form from an object only accessible through the HTTPSession. i know that
i could code a scriptlet to set the session as a property in the
ActionForm but i wanted to stay away from including scriptlet code if
possible.
thanks,
Pat Ludwig
Silverstream Software
Re: Save the ActionForm interface! (was ...HTTPSession...)
Posted by Barrie Treloar <Ba...@camtech.com.au>.
On Thu, 24 Aug 2000, Hans Gerwitz wrote:
> Firstly, my imagination fails me when asked to provide situations where a
> form bean must be used that cannot easily have "implements ActionForm"
> added.
Besides you can always write a Facade class that implements ActionForm
and delegates all its methods to the third party bean that you can't
modify.
Barrie
--
Barrie Treloar
____________________________________________________________________
Barrie Treloar Phone: +61 8 8303 3300
Senior Analyst/Programmer Fax: +61 8 8303 4403
Electronic Commerce Division Email: barrie@camtech.com.au
Camtech (SA) Pty Ltd http://www.camtech.com.au
--- Level 8, 10 Pulteney Street, Adelaide SA 5000, Australia. ---
____________________________________________________________________
Save the ActionForm interface! (was ...HTTPSession...)
Posted by Hans Gerwitz <ja...@phobia.com>.
I want to keep the ActionForm interface.
Firstly, my imagination fails me when asked to provide situations where a
form bean must be used that cannot easily have "implements ActionForm"
added.
Secondly, although the *present* API does not specify anything beyond
standard JavaBean naming conventions, it is foreseeable that the future may
bring changes here. I, for one, support Pat Ludwig's proposal for an init()
in ActionForm for initializing form beans without necessitating actions that
do little more than populate or overriding ActionMapping just to use session
data.
The addition of init() or any other methods to ActionForm would bring the
argument for the ActionForm interface in line with the existing one for
ValidatingActionForm. While it *could* be established via reflection, this
is expensive and only proves the existance of a method, not the intent of
the developer to support the Struts API.
So, save ActionForm and give it init() :-)
--
Hans Gerwitz
hans@phobia.com
RE: getting access to the HTTPSession from an ActionForm bean
Posted by "Ludwig, Pat" <pl...@silverstream.com>.
Craig,
> One partial approach to doing this is available to you
> already -- let's
> see if it meets your needs.
>
> The controller servlet creates a new form bean instance in the first
> place (if it needs to) by calling the createFormInstance() method of
> your ActionMapping. Most people just use the default
> ActionMappingBase
> implementation class that simply creates a new bean instance, but you
> could certainly use a customized class that did fancier
> initialization.
> You can even customize which ActionMapping implementation
> class is used
> for each <action> element, by using the "className" attribute.
in my situation, this does not meet my needs since i need access to the
Request and Session to provide initialization.
>
> At present, this approach gets you part way there, because the mapping
> instance cannot see the current request or session (although
> it can get
> to the servlet context attributes). I would propose that we add a
> second method signature:
>
> public ActionForm createActionInstance(HttpServletRequest
> request);
>
> that would give you access to request and session attributes as well.
> Does that sound like a reasonable approach?
>
> Craig
this approach is certainly acceptable and may be better from a compatibility
standpoint, but
having the Action interface support an init method seems better conceptually
to me since it
acknowledges at the interface-level this kind of processing pattern. what
do others think?
>
> PS: Other posters have also suggested that Struts switch
> away from the
> ActionForm tagging interface, and just pass Object everywhere it
> currently uses ActionForm. Among other things, this lets you use, or
> re-use, bean classes that are not currently Struts-aware. What do you
> think?
>
i am neutral on this point. if the Struts control structure loses nothing
in switching to Object from ActionForm, it does sound attractive to switch
for the
reason given and the added flexibility of broadening what an ActionForm
object might be.
- Pat Ludwig
>
> Pierre Métras wrote:
>
> > I support this request, as I asked for a similar function
> last week (I
> > suggested an ActionFormInitializer interface with an init() function
> > called just before form population). Though you can do
> without it, it
> > makes things simpler in some situations. Pierre Métras
> >
> >
> > ----- Original Message -----
> > From: Ludwig, Pat
> > To: struts-user@jakarta.apache.org
> > Sent: Thursday, August 24, 2000 12:37 PM
> > Subject: RE: getting access to the HTTPSession from an
> > ActionForm bean
> > Craig, thanks for the your input. it sounds like it
> > would solve my problem. in terms of the framework, do you
> > think it to be a reasonable pattern in Struts for the Action
> > class to get control prior to the ActionForm for
> > session-related initialization. perhaps an init method which
> > is called to complement the perform?regards,Pat Ludwig
> > -----Original Message-----
> > From: Craig R. McClanahan
> > [mailto:Craig.McClanahan@eng.sun.com]
> > Sent: Wednesday, August 23, 2000 8:37 PM
> > To: struts-user@jakarta.apache.org
> > Subject: Re: getting access to the HTTPSession from an
> > ActionForm bean
> >
> >
> > "Ludwig, Pat" wrote:
> >
> > > hi, is there a preferred way in a Struts
> > > application to get access to the HTTPSession? i
> > > have an app which requires initial values for
> > > the form from an object only accessible through
> > > the HTTPSession. i know that i could code a
> > > scriptlet to set the session as a property in
> > > the ActionForm but i wanted to stay away from
> > > including scriptlet code if possible.thanks,Pat
> > > LudwigSilverstream Software
> >
> >
> > The approach I usually take to something like this
> > is to pre-populate the bean in an Action class
> > (normally stored as a request attribute or session
> > attribute) prior to forwarding control to the JSP
> > page that uses that bean (as a form bean) to
> > display the form.
> >
> > For example, let's say you want to edit the user's
> > current preferences, which are stored somewhere in
> > a session bean. The general approach would be
> > something like this:
> >
> > * The menu option (or whatever) by which the user
> > indicates
> > that he or she wants to do this is a hyperlink
> > or submit to
> > an action (a url like "/editPreferences.do" or
> > something).
> >
> > * The action class instantiates a form bean,
> > copies appropriate
> > stuff from the preferences object in the
> > session, and stores
> > the form bean as a session attribute.
> >
> > * The action class returns an ActionForward that
> > forwards to
> > the appropriate JSP page (say,
> > "/editPreferences.jsp").
> >
> > * This JSP page has a form that uses the form bean
> > set up
> > earlier, which will already be present and
> > populated with the
> > values set up by the action.
> >
> > * The form will submit to something like
> > "/savePreferences.do",
> > which will call an action that validates the
> > input, and then
> > updates the preferences information in the
> > session, and/or
> > saves it persistently somewhere.
> >
> > Using this approach, the form bean itself never
> > has to have access to the session, or beans within
> > the session, to initialize itself.
> >
> > Craig
> >
> >
Re: getting access to the HTTPSession from an ActionForm bean
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
One partial approach to doing this is available to you already -- let's
see if it meets your needs.
The controller servlet creates a new form bean instance in the first
place (if it needs to) by calling the createFormInstance() method of
your ActionMapping. Most people just use the default ActionMappingBase
implementation class that simply creates a new bean instance, but you
could certainly use a customized class that did fancier initialization.
You can even customize which ActionMapping implementation class is used
for each <action> element, by using the "className" attribute.
At present, this approach gets you part way there, because the mapping
instance cannot see the current request or session (although it can get
to the servlet context attributes). I would propose that we add a
second method signature:
public ActionForm createActionInstance(HttpServletRequest request);
that would give you access to request and session attributes as well.
Does that sound like a reasonable approach?
Craig
PS: Other posters have also suggested that Struts switch away from the
ActionForm tagging interface, and just pass Object everywhere it
currently uses ActionForm. Among other things, this lets you use, or
re-use, bean classes that are not currently Struts-aware. What do you
think?
Pierre Métras wrote:
> I support this request, as I asked for a similar function last week (I
> suggested an ActionFormInitializer interface with an init() function
> called just before form population). Though you can do without it, it
> makes things simpler in some situations. Pierre Métras
>
>
> ----- Original Message -----
> From: Ludwig, Pat
> To: struts-user@jakarta.apache.org
> Sent: Thursday, August 24, 2000 12:37 PM
> Subject: RE: getting access to the HTTPSession from an
> ActionForm bean
> Craig, thanks for the your input. it sounds like it
> would solve my problem. in terms of the framework, do you
> think it to be a reasonable pattern in Struts for the Action
> class to get control prior to the ActionForm for
> session-related initialization. perhaps an init method which
> is called to complement the perform?regards,Pat Ludwig
> -----Original Message-----
> From: Craig R. McClanahan
> [mailto:Craig.McClanahan@eng.sun.com]
> Sent: Wednesday, August 23, 2000 8:37 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: getting access to the HTTPSession from an
> ActionForm bean
>
>
> "Ludwig, Pat" wrote:
>
> > hi, is there a preferred way in a Struts
> > application to get access to the HTTPSession? i
> > have an app which requires initial values for
> > the form from an object only accessible through
> > the HTTPSession. i know that i could code a
> > scriptlet to set the session as a property in
> > the ActionForm but i wanted to stay away from
> > including scriptlet code if possible.thanks,Pat
> > LudwigSilverstream Software
>
>
> The approach I usually take to something like this
> is to pre-populate the bean in an Action class
> (normally stored as a request attribute or session
> attribute) prior to forwarding control to the JSP
> page that uses that bean (as a form bean) to
> display the form.
>
> For example, let's say you want to edit the user's
> current preferences, which are stored somewhere in
> a session bean. The general approach would be
> something like this:
>
> * The menu option (or whatever) by which the user
> indicates
> that he or she wants to do this is a hyperlink
> or submit to
> an action (a url like "/editPreferences.do" or
> something).
>
> * The action class instantiates a form bean,
> copies appropriate
> stuff from the preferences object in the
> session, and stores
> the form bean as a session attribute.
>
> * The action class returns an ActionForward that
> forwards to
> the appropriate JSP page (say,
> "/editPreferences.jsp").
>
> * This JSP page has a form that uses the form bean
> set up
> earlier, which will already be present and
> populated with the
> values set up by the action.
>
> * The form will submit to something like
> "/savePreferences.do",
> which will call an action that validates the
> input, and then
> updates the preferences information in the
> session, and/or
> saves it persistently somewhere.
>
> Using this approach, the form bean itself never
> has to have access to the session, or beans within
> the session, to initialize itself.
>
> Craig
>
>
Re: getting access to the HTTPSession from an ActionForm bean
Posted by Pierre Métras <ge...@sympatico.ca>.
I support this request, as I asked for a similar function last week (I suggested an ActionFormInitializer interface with an init() function called just before form population). Though you can do without it, it makes things simpler in some situations.
Pierre Métras
----- Original Message -----
From: Ludwig, Pat
To: struts-user@jakarta.apache.org
Sent: Thursday, August 24, 2000 12:37 PM
Subject: RE: getting access to the HTTPSession from an ActionForm bean
Craig,
thanks for the your input. it sounds like it would solve my problem. in terms of the framework, do you think it to be a reasonable pattern in Struts for the Action class to get control prior to the ActionForm for session-related initialization. perhaps an init method which is called to complement the perform?
regards,
Pat Ludwig
-----Original Message-----
From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
Sent: Wednesday, August 23, 2000 8:37 PM
To: struts-user@jakarta.apache.org
Subject: Re: getting access to the HTTPSession from an ActionForm bean
"Ludwig, Pat" wrote:
hi, is there a preferred way in a Struts application to get access to the HTTPSession? i have an app which requires initial values for the form from an object only accessible through the HTTPSession. i know that i could code a scriptlet to set the session as a property in the ActionForm but i wanted to stay away from including scriptlet code if possible.thanks,Pat LudwigSilverstream Software
The approach I usually take to something like this is to pre-populate the bean in an Action class (normally stored as a request attribute or session attribute) prior to forwarding control to the JSP page that uses that bean (as a form bean) to display the form.
For example, let's say you want to edit the user's current preferences, which are stored somewhere in a session bean. The general approach would be something like this:
* The menu option (or whatever) by which the user indicates
that he or she wants to do this is a hyperlink or submit to
an action (a url like "/editPreferences.do" or something).
* The action class instantiates a form bean, copies appropriate
stuff from the preferences object in the session, and stores
the form bean as a session attribute.
* The action class returns an ActionForward that forwards to
the appropriate JSP page (say, "/editPreferences.jsp").
* This JSP page has a form that uses the form bean set up
earlier, which will already be present and populated with the
values set up by the action.
* The form will submit to something like "/savePreferences.do",
which will call an action that validates the input, and then
updates the preferences information in the session, and/or
saves it persistently somewhere.
Using this approach, the form bean itself never has to have access to the session, or beans within the session, to initialize itself.
Craig
RE: getting access to the HTTPSession from an ActionForm bean
Posted by "Ludwig, Pat" <pl...@silverstream.com>.
Craig,
thanks for the your input. it sounds like it would solve my problem.
in terms of the framework, do you think it to be a reasonable pattern in
Struts for the Action class to get control prior to the ActionForm for
session-related initialization. perhaps an init method which is called
to complement the perform?
regards,
Pat Ludwig
-----Original Message-----
From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
Sent: Wednesday, August 23, 2000 8:37 PM
To: struts-user@jakarta.apache.org
Subject: Re: getting access to the HTTPSession from an ActionForm bean
"Ludwig, Pat" wrote:
hi, is there a preferred way in a Struts application to get access
to the HTTPSession? i have an app which requires initial values for the
form from an object only accessible through the HTTPSession. i know that
i could code a scriptlet to set the session as a property in the
ActionForm but i wanted to stay away from including scriptlet code if
possible.thanks,Pat LudwigSilverstream Software
The approach I usually take to something like this is to pre-populate
the bean in an Action class (normally stored as a request attribute or
session attribute) prior to forwarding control to the JSP page that uses
that bean (as a form bean) to display the form.
For example, let's say you want to edit the user's current preferences,
which are stored somewhere in a session bean. The general approach
would be something like this:
* The menu option (or whatever) by which the user indicates
that he or she wants to do this is a hyperlink or submit to
an action (a url like "/editPreferences.do" or something).
* The action class instantiates a form bean, copies appropriate
stuff from the preferences object in the session, and stores
the form bean as a session attribute.
* The action class returns an ActionForward that forwards to
the appropriate JSP page (say, "/editPreferences.jsp").
* This JSP page has a form that uses the form bean set up
earlier, which will already be present and populated with the
values set up by the action.
* The form will submit to something like "/savePreferences.do",
which will call an action that validates the input, and then
updates the preferences information in the session, and/or
saves it persistently somewhere.
Using this approach, the form bean itself never has to have access to
the session, or beans within the session, to initialize itself.
Craig
Re: getting access to the HTTPSession from an ActionForm bean
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Ludwig, Pat" wrote:
> hi, is there a preferred way in a Struts application to get access
> to the HTTPSession? i have an app which requires initial values for
> the form from an object only accessible through the HTTPSession. i
> know that i could code a scriptlet to set the session as a property in
> the ActionForm but i wanted to stay away from including scriptlet code
> if possible.thanks,Pat LudwigSilverstream Software
The approach I usually take to something like this is to pre-populate
the bean in an Action class (normally stored as a request attribute or
session attribute) prior to forwarding control to the JSP page that uses
that bean (as a form bean) to display the form.
For example, let's say you want to edit the user's current preferences,
which are stored somewhere in a session bean. The general approach
would be something like this:
* The menu option (or whatever) by which the user indicates
that he or she wants to do this is a hyperlink or submit to
an action (a url like "/editPreferences.do" or something).
* The action class instantiates a form bean, copies appropriate
stuff from the preferences object in the session, and stores
the form bean as a session attribute.
* The action class returns an ActionForward that forwards to
the appropriate JSP page (say, "/editPreferences.jsp").
* This JSP page has a form that uses the form bean set up
earlier, which will already be present and populated with the
values set up by the action.
* The form will submit to something like "/savePreferences.do",
which will call an action that validates the input, and then
updates the preferences information in the session, and/or
saves it persistently somewhere.
Using this approach, the form bean itself never has to have access to
the session, or beans within the session, to initialize itself.
Craig