You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alexis Pigeon <pi...@gmail.com> on 2007/04/10 09:00:17 UTC

[S2] How to keep a reference to a bean without using a session scope?

Hi all,

My application uses a bean to store a collection of values to display
in a select input. Since the generation of this collection is
time-consuming, I set this bean to a session scope, and using the
following code :

in jsp:
<%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld" %>
<s:set name="myCollection" value="myCollection" scope="session"/>

in the Action (implementing Preparable and ServletRequestAware):
private Vector myCollection;
public Vector getMyCollection() {
    return myCollection;
}
public void setMyCollection (Vector v) {
    this.myCollection = v;
}
public void prepare() {
    ...
    setMyCollection( (Vector)
request.getSession().getAttribute("myCollection"));
    ....
}

This works perfectly as expected, as long as there is only one
instance of the application launched at a time in the same browser
(using Firefox or IE7 tabs for example). In that case, the bean is
shared between the different instances, which is problematic since its
content depends on previous data input.

I tried to change the scope of the bean, using "page" or "request".
Passing the bean from the Action to the jsp is ok, but I can't get the
jsp to pass it back to the Action.

Is there another interface I should implement in the Action, or
another tag I should use in the jsp? Or maybe there is a way to force
the creation of a new session when opening another instance of the
application?

Thank you all for the help,
alexis

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


Re: [S2] How to keep a reference to a bean without using a session scope?

Posted by Mark Menard <ma...@mjm.net>.
Hi Alexis,

On 4/23/07 5:16 AM, "Alexis Pigeon" <pi...@gmail.com> wrote:
> Just as a follow-up, you may have missed my message asking for the
> updated version of your conversation scope interceptor.

I'm sorry, quite busy the last few weeks. I have attached my latest version
to the WW-1514 ticket.

> Moreover, any news about the issue you entered in JIRA concerning this
> implementation? http://issues.apache.org/struts/browse/WW-1514

There really isn't any news. I've personally been very busy with project
work, and the S2 guys have been busy just getting a stable release out. I
think I might have time in the 2.1 cycle to work on this some more.

Mark

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


Re: [S2] How to keep a reference to a bean without using a session scope?

Posted by Alexis Pigeon <pi...@gmail.com>.
Hi again Mark,

Just as a follow-up, you may have missed my message asking for the
updated version of your conversation scope interceptor.
Moreover, any news about the issue you entered in JIRA concerning this
implementation?
< http://issues.apache.org/struts/browse/WW-1514 >

Thanks,
alexis

On 11/04/07, Alexis Pigeon <pi...@gmail.com> wrote:

> On 10/04/07, Mark Menard <ma...@mjm.net> wrote:

[...]

> > You could also take a
> > look at a simple interceptor I created for this purpose.
> > (http://www.vitarara.org/cms/node/84) I have since updated that method to
> > simply be an extension of ModelDriven, if you're interested in the most
> > recent version let me know.
>
> The concept of conversation scope is exactly what I was looking for.
> The most common situation where I would need it is when having a
> select input which content depends on the item chosen from another one
> (for example, a set of two selects representing countries and cities,
> the content of the latter representing the cities of the chosen
> country only).
> In my case, the generation of the "subselect" can be quite
> time-consuming, thus the need to keep it in some higher scope than the
> request.
> As for the Interceptor you wrote, I would be most interested in the
> most recent version.
>
> Thanks for the help,
> alexis
>

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


Re: [S2] How to keep a reference to a bean without using a session scope?

Posted by Alexis Pigeon <pi...@gmail.com>.
Hi Mark,

On 10/04/07, Mark Menard <ma...@mjm.net> wrote:
> There's a cleaner Struts 2 way of doing this. Take a look at the
> org.apache.struts2.interceptor.SessionAware interface. This will make your
> action independent of the servlet spec and more easily testable.

Thanks for the tip!

> > This works perfectly as expected, as long as there is only one
> > instance of the application launched at a time in the same browser
> > (using Firefox or IE7 tabs for example). In that case, the bean is
> > shared between the different instances, which is problematic since its
> > content depends on previous data input.
>
> You need a conversation scope. There is no direct support for this in the
> Servlet spec, nor in Struts 2 directly. You could take a look at using the
> Spring Webflow integration (I don't know the link). You could also take a
> look at a simple interceptor I created for this purpose.
> (http://www.vitarara.org/cms/node/84) I have since updated that method to
> simply be an extension of ModelDriven, if you're interested in the most
> recent version let me know.

The concept of conversation scope is exactly what I was looking for.
The most common situation where I would need it is when having a
select input which content depends on the item chosen from another one
(for example, a set of two selects representing countries and cities,
the content of the latter representing the cities of the chosen
country only).
In my case, the generation of the "subselect" can be quite
time-consuming, thus the need to keep it in some higher scope than the
request.
As for the Interceptor you wrote, I would be most interested in the
most recent version.

Thanks for the help,
alexis

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


Re: [S2] How to keep a reference to a bean without using a session scope?

Posted by Mark Menard <ma...@mjm.net>.
On 4/10/07 3:00 AM, "Alexis Pigeon" <pi...@gmail.com> wrote:

> Hi all,
> 
> My application uses a bean to store a collection of values to display
> in a select input. Since the generation of this collection is
> time-consuming, I set this bean to a session scope, and using the
> following code :
> 
> in jsp:
> <%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld" %>
> <s:set name="myCollection" value="myCollection" scope="session"/>

I wouldn't recommend doing this in the JSP. (see below)
 
> in the Action (implementing Preparable and ServletRequestAware):
> private Vector myCollection;
> public Vector getMyCollection() {
>   return myCollection;
> }
> public void setMyCollection (Vector v) {
>   this.myCollection = v;
> }
> public void prepare() {
>   ...
>   setMyCollection( (Vector)
> request.getSession().getAttribute("myCollection"));
>   ....
> }

There's a cleaner Struts 2 way of doing this. Take a look at the
org.apache.struts2.interceptor.SessionAware interface. This will make your
action independent of the servlet spec and more easily testable.

Also you ought to put the collection into the session in your action.
Setting it into the session scope in the JSP is in general a bad practice.
The view really should not change the "model" of the running app.
 
> This works perfectly as expected, as long as there is only one
> instance of the application launched at a time in the same browser
> (using Firefox or IE7 tabs for example). In that case, the bean is
> shared between the different instances, which is problematic since its
> content depends on previous data input.

You need a conversation scope. There is no direct support for this in the
Servlet spec, nor in Struts 2 directly. You could take a look at using the
Spring Webflow integration (I don't know the link). You could also take a
look at a simple interceptor I created for this purpose.
(http://www.vitarara.org/cms/node/84) I have since updated that method to
simply be an extension of ModelDriven, if you're interested in the most
recent version let me know.
 
Mark

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