You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "C.F. Scheidecker Antunes" <na...@antunes.eti.br> on 2005/08/10 01:02:27 UTC

Setting and accessing Context and Session data

Hello all,

More newbie questions. I just want to make sure I understand the idea on 
how to write a Struts application
properly.

How do you save something to the Session scope and then access it within 
Struts?
I pretty much understand how to do it within a Servlet.

How do you do the same for the Context? I also know how to do it in a 
Servlet.

Can a Model Class access or set anything in the Session or in the 
Context space?
Say that you want to access a database based on a parameter set on the 
Session?
Shall I make a getter for this model class and pass the parameter 
through an Action call or
can I access the Session data from a Model Class? Then I guess my Model 
Class should extend some
Struts class that would provide access to both the Session and the Context.

Are Model Classes instances unique for every Session? Or one instance is 
common to everyone?
I guess I am concerned with data state here and I just want to make sure 
how it works.

Thanks again!

C.F.

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


Re: Setting and accessing Context and Session data

Posted by Dave Newton <ne...@pingsite.com>.
C.F. Scheidecker Antunes wrote:

> How do you save something to the Session scope and then access it 
> within Struts?
> I pretty much understand how to do it within a Servlet.
>
> How do you do the same for the Context? I also know how to do it in a 
> Servlet.

Same way; inside your Actions you can get the session through the 
request and the context through getServlet().getContext() (minus 
whatever end-of-day errors I've made)

Dave

> Shall I make a getter for this model class and pass the parameter 
> through an Action call or
> can I access the Session data from a Model Class? Then I guess my 
> Model Class should extend some
> Struts class that would provide access to both the Session and the 
> Context.

Yuck. In general you'd want to decouple your model from both Struts and 
web applications. This provides lots of benefits like testability and 
reuse (say in a standalone desktop app).

Dave



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


Re: Setting and accessing Context and Session data

Posted by Michael Jouravlev <jm...@gmail.com>.
On 8/9/05, C.F. Scheidecker Antunes <na...@antunes.eti.br> wrote:
> How do you save something to the Session scope and then access it within
> Struts?
> I pretty much understand how to do it within a Servlet.

You can do this directly, or you can setup your ActionForm class to
have session scope. ActionForm is simply a bean which lifecycle is
controlled by Struts using scope setting that you set in <action>
element of config file. If you select "request" scope, Struts will
stick ActionForm in request object. If you select "session" scope,
Struts will stick ActionForm into the session.

The <form-beans> section of config file defines keys, under which
Struts stores ActionForm objects in implicit scope objects. In a
sense, ActionForm is just a [convenient?] container for the stuff
related to an action.

I personally find it easier to have everything related to an action in
a corresponding ActionForm. If I need to store data during several
requests, I use session scope for an ActionForm. Another approach is
to use request scope for ActionForms, and to save session-scoped stuff
directly into session object.

Michael.

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