You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Gamble, Wesley (WG10)" <WG...@tmw.com> on 2008/06/10 00:34:12 UTC
Modifying session attributes in Struts 2
All,
If I have a Struts action, and I acquire the session map via:
Map session = ActionContext.getContext().getSession();
can I simply use
session.put("new_key", new_value);
to modify session attributes or do I need to do something different?
Thanks,
Wes
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Modifying session attributes in Struts 2
Posted by Lukasz Lenart <lu...@googlemail.com>.
Hi,
I think the best solution is to implement SessionAware interface and
setSession() method, then you will either add or remove values from
map. Like below:
public class IndexAction implements Action, SessionAware {
private Map<String,Object> session;
public void setSession(Map<String,Object> session) {
this.session = session;
}
public String execute() {
session.put("key", "Some Value");
return SUCCESS;
}
public String doCheck() {
return SUCCESS;
}
}
snippet from struts.xml
<package name="myPackage" extends="struts-default">
<action name="index" class="pl.org.lenart.s2demo.IndexAction">
<result>/jsp/index.jsp</result>
</action>
<action name="check" class="pl.org.lenart.s2demo.IndexAction"
method="doCheck">
<result>/jsp/index.jsp</result>
</action>
</package>
index.jsp to test session value
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Index</title>
<s:head />
</head>
<body>
<s:property value="#session.key"/>
</body>
</html>
Go to http://localhost:8080/<context>/index.action and you will see
the value from session, then go to
http://localhost:8080/<context>/check.action and once again you will
see the same value. Go to some other page in Internet and then come
back to http://localhost:8080/<context>/check.action, once again you
will see the same ;-)
Your action is free from hard coded dependency to ActionContext. I
check this and it's working, I'm using Struts 2.1.3-SNAPSHOT
Regards
--
Lukasz
http://www.lenart.org.pl/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Modifying session attributes in Struts 2
Posted by Martin <mg...@hotmail.com>.
/* you were really close*/
Map session = (Map) ActionContext.getContext().get("session");
session.put("new_key",new_value);
http://struts.apache.org/2.x/docs/accessing-application-session-request-objects.html
HTH
Martin
----- Original Message -----
From: "Gamble, Wesley (WG10)" <WG...@tmw.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Monday, June 09, 2008 7:16 PM
Subject: RE: Modifying session attributes in Struts 2
Hmm... it doesn't seem to be working. That's why I was asking. I'll
keep at it.
Thanks,
Wes
-----Original Message-----
From: Felipe Lorenz [mailto:felipe.lorenz@gmail.com]
Sent: Monday, June 09, 2008 6:15 PM
To: Struts Users Mailing List
Subject: Re: Modifying session attributes in Struts 2
you right... just do what you did say!
On Mon, Jun 9, 2008 at 7:52 PM, Gamble, Wesley (WG10) <WG...@tmw.com>
wrote:
> Huh?
>
> -----Original Message-----
> From: Felipe Lorenz [mailto:felipe.lorenz@gmail.com]
> Sent: Monday, June 09, 2008 5:37 PM
> To: Struts Users Mailing List
> Subject: Re: Modifying session attributes in Struts 2
>
> nothing diferent.. just it.
>
> On Mon, Jun 9, 2008 at 7:34 PM, Gamble, Wesley (WG10) <WG...@tmw.com>
> wrote:
>
> > All,
> >
> > If I have a Struts action, and I acquire the session map via:
> >
> > Map session = ActionContext.getContext().getSession();
> > session.put("new_key", new_value);
> >
> > to modify session attributes or do I need to do something different?
> >
> > Thanks,
> > Wes
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: Modifying session attributes in Struts 2
Posted by "Gamble, Wesley (WG10)" <WG...@tmw.com>.
Hmm... it doesn't seem to be working. That's why I was asking. I'll
keep at it.
Thanks,
Wes
-----Original Message-----
From: Felipe Lorenz [mailto:felipe.lorenz@gmail.com]
Sent: Monday, June 09, 2008 6:15 PM
To: Struts Users Mailing List
Subject: Re: Modifying session attributes in Struts 2
you right... just do what you did say!
On Mon, Jun 9, 2008 at 7:52 PM, Gamble, Wesley (WG10) <WG...@tmw.com>
wrote:
> Huh?
>
> -----Original Message-----
> From: Felipe Lorenz [mailto:felipe.lorenz@gmail.com]
> Sent: Monday, June 09, 2008 5:37 PM
> To: Struts Users Mailing List
> Subject: Re: Modifying session attributes in Struts 2
>
> nothing diferent.. just it.
>
> On Mon, Jun 9, 2008 at 7:34 PM, Gamble, Wesley (WG10) <WG...@tmw.com>
> wrote:
>
> > All,
> >
> > If I have a Struts action, and I acquire the session map via:
> >
> > Map session = ActionContext.getContext().getSession();
> >
> > can I simply use
> >
> > session.put("new_key", new_value);
> >
> > to modify session attributes or do I need to do something different?
> >
> > Thanks,
> > Wes
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Modifying session attributes in Struts 2
Posted by Felipe Lorenz <fe...@gmail.com>.
you right... just do what you did say!
On Mon, Jun 9, 2008 at 7:52 PM, Gamble, Wesley (WG10) <WG...@tmw.com> wrote:
> Huh?
>
> -----Original Message-----
> From: Felipe Lorenz [mailto:felipe.lorenz@gmail.com]
> Sent: Monday, June 09, 2008 5:37 PM
> To: Struts Users Mailing List
> Subject: Re: Modifying session attributes in Struts 2
>
> nothing diferent.. just it.
>
> On Mon, Jun 9, 2008 at 7:34 PM, Gamble, Wesley (WG10) <WG...@tmw.com>
> wrote:
>
> > All,
> >
> > If I have a Struts action, and I acquire the session map via:
> >
> > Map session = ActionContext.getContext().getSession();
> >
> > can I simply use
> >
> > session.put("new_key", new_value);
> >
> > to modify session attributes or do I need to do something different?
> >
> > Thanks,
> > Wes
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
RE: Modifying session attributes in Struts 2
Posted by "Gamble, Wesley (WG10)" <WG...@tmw.com>.
Huh?
-----Original Message-----
From: Felipe Lorenz [mailto:felipe.lorenz@gmail.com]
Sent: Monday, June 09, 2008 5:37 PM
To: Struts Users Mailing List
Subject: Re: Modifying session attributes in Struts 2
nothing diferent.. just it.
On Mon, Jun 9, 2008 at 7:34 PM, Gamble, Wesley (WG10) <WG...@tmw.com>
wrote:
> All,
>
> If I have a Struts action, and I acquire the session map via:
>
> Map session = ActionContext.getContext().getSession();
>
> can I simply use
>
> session.put("new_key", new_value);
>
> to modify session attributes or do I need to do something different?
>
> Thanks,
> Wes
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Modifying session attributes in Struts 2
Posted by Felipe Lorenz <fe...@gmail.com>.
nothing diferent.. just it.
On Mon, Jun 9, 2008 at 7:34 PM, Gamble, Wesley (WG10) <WG...@tmw.com> wrote:
> All,
>
> If I have a Struts action, and I acquire the session map via:
>
> Map session = ActionContext.getContext().getSession();
>
> can I simply use
>
> session.put("new_key", new_value);
>
> to modify session attributes or do I need to do something different?
>
> Thanks,
> Wes
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>