You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kushan Jayathilake <ku...@gmail.com> on 2010/06/18 07:01:34 UTC

How to set a variable in session in the JSP

Hi Guys,

Can anyone suggest me a way to set a variable in the session in the JSP and
access that variable in the action class.. (variable must be set from the
JSP file to the session)

iam using struts 2.. is there anyway to do this..?

-- 
Regards
Kushan Jayathilake

Re: How to set a variable in session in the JSP

Posted by Kushan Jayathilake <ku...@gmail.com>.
Hi,

Yeah Wes, you are correct, i was so stuck with this and couldnt see that i
can straightly put that to the session from the Action class...

Thank you so much for reminding me this....

-- 
Regards
Kushan Jayathilake


On Fri, Jun 18, 2010 at 11:03 AM, Wes Wannemacher <we...@wantii.com> wrote:

> What you want is this -
>
> <s:set name="sessionValue" value="%{accountList}" scope="session" />
>
> However, what i'm wondering right now is this -
>
> you have to expose "accountList" most likely by having a getter/setter
> combination on the action that is rendering the JSP that contains this
> tag. Assuming you are placing the variable in the session so that you
> can retrieve it from another action, why have the object pushed into
> the session from a view? I mean, if you have it in an action, why not
> push it into the session in that action?
>
> To me, setting session variables from a view smells a bit like
> business logic creeping into your view layer. I probably don't know
> enough about the task to say for sure, and the tag above should get
> the job done, but consider what I'm saying... One of the advantages of
> adopting an MVC framework is to abstract business logic away from the
> views in your application. If the view performs more than just
> displaying the appropriate data, it is potentially less reusable by
> different actions (although it doesn't happen a lot that a view is
> reused by different actions, it does happen more often if you're
> properly disciplined in separating the business logic).
>
> -Wes
>
> On Fri, Jun 18, 2010 at 1:24 AM, Kushan Jayathilake <ku...@gmail.com>
> wrote:
> > Hi,
> >
> > Thanks for replying...
> >
> >
> > In my JSP i put the <s:set> as you said, i want to get the value from the
> > Action class and set that value in the session in the JSP, so i wrote,
> >
> > <s:set name="sessionValue" value="<s:property value="accountList"/>"
> > scope="session"/>
> >
> > note that "accountList" is a java.util.List in the Action class, i want
> to
> > get that list to the JSP and assign its value to the session,
> >
> > but it gives "equal symbol expected" error...
> >
> > Can you please suggest me a way to do this..?
> >
> > --
> > Regards
> > Kushan Jayathilake
> >
> >
> > On Fri, Jun 18, 2010 at 10:41 AM, Wes Wannemacher <we...@wantii.com>
> wrote:
> >
> >> http://struts.apache.org/2.x/docs/set.html
> >>
> >> Notice particularly the "scope" attribute.
> >>
> >> -Wes
> >>
> >> On Fri, Jun 18, 2010 at 1:01 AM, Kushan Jayathilake <kushanxp@gmail.com
> >
> >> wrote:
> >> > Hi Guys,
> >> >
> >> > Can anyone suggest me a way to set a variable in the session in the
> JSP
> >> and
> >> > access that variable in the action class.. (variable must be set from
> the
> >> > JSP file to the session)
> >> >
> >> > iam using struts 2.. is there anyway to do this..?
> >> >
> >> > --
> >> > Regards
> >> > Kushan Jayathilake
> >> >
> >>
> >>
> >>
> >> --
> >> Wes Wannemacher
> >>
> >> Head Engineer, WanTii, Inc.
> >> Need Training? Struts, Spring, Maven, Tomcat...
> >> Ask me for a quote!
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
>
>
>
> --
> Wes Wannemacher
>
> Head Engineer, WanTii, Inc.
> Need Training? Struts, Spring, Maven, Tomcat...
> Ask me for a quote!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: How to set a variable in session in the JSP

Posted by Wes Wannemacher <we...@wantii.com>.
What you want is this -

<s:set name="sessionValue" value="%{accountList}" scope="session" />

However, what i'm wondering right now is this -

you have to expose "accountList" most likely by having a getter/setter
combination on the action that is rendering the JSP that contains this
tag. Assuming you are placing the variable in the session so that you
can retrieve it from another action, why have the object pushed into
the session from a view? I mean, if you have it in an action, why not
push it into the session in that action?

To me, setting session variables from a view smells a bit like
business logic creeping into your view layer. I probably don't know
enough about the task to say for sure, and the tag above should get
the job done, but consider what I'm saying... One of the advantages of
adopting an MVC framework is to abstract business logic away from the
views in your application. If the view performs more than just
displaying the appropriate data, it is potentially less reusable by
different actions (although it doesn't happen a lot that a view is
reused by different actions, it does happen more often if you're
properly disciplined in separating the business logic).

-Wes

On Fri, Jun 18, 2010 at 1:24 AM, Kushan Jayathilake <ku...@gmail.com> wrote:
> Hi,
>
> Thanks for replying...
>
>
> In my JSP i put the <s:set> as you said, i want to get the value from the
> Action class and set that value in the session in the JSP, so i wrote,
>
> <s:set name="sessionValue" value="<s:property value="accountList"/>"
> scope="session"/>
>
> note that "accountList" is a java.util.List in the Action class, i want to
> get that list to the JSP and assign its value to the session,
>
> but it gives "equal symbol expected" error...
>
> Can you please suggest me a way to do this..?
>
> --
> Regards
> Kushan Jayathilake
>
>
> On Fri, Jun 18, 2010 at 10:41 AM, Wes Wannemacher <we...@wantii.com> wrote:
>
>> http://struts.apache.org/2.x/docs/set.html
>>
>> Notice particularly the "scope" attribute.
>>
>> -Wes
>>
>> On Fri, Jun 18, 2010 at 1:01 AM, Kushan Jayathilake <ku...@gmail.com>
>> wrote:
>> > Hi Guys,
>> >
>> > Can anyone suggest me a way to set a variable in the session in the JSP
>> and
>> > access that variable in the action class.. (variable must be set from the
>> > JSP file to the session)
>> >
>> > iam using struts 2.. is there anyway to do this..?
>> >
>> > --
>> > Regards
>> > Kushan Jayathilake
>> >
>>
>>
>>
>> --
>> Wes Wannemacher
>>
>> Head Engineer, WanTii, Inc.
>> Need Training? Struts, Spring, Maven, Tomcat...
>> Ask me for a quote!
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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


Re: How to set a variable in session in the JSP

Posted by Kushan Jayathilake <ku...@gmail.com>.
Hi,

Thanks for replying...


In my JSP i put the <s:set> as you said, i want to get the value from the
Action class and set that value in the session in the JSP, so i wrote,

<s:set name="sessionValue" value="<s:property value="accountList"/>"
scope="session"/>

note that "accountList" is a java.util.List in the Action class, i want to
get that list to the JSP and assign its value to the session,

but it gives "equal symbol expected" error...

Can you please suggest me a way to do this..?

-- 
Regards
Kushan Jayathilake


On Fri, Jun 18, 2010 at 10:41 AM, Wes Wannemacher <we...@wantii.com> wrote:

> http://struts.apache.org/2.x/docs/set.html
>
> Notice particularly the "scope" attribute.
>
> -Wes
>
> On Fri, Jun 18, 2010 at 1:01 AM, Kushan Jayathilake <ku...@gmail.com>
> wrote:
> > Hi Guys,
> >
> > Can anyone suggest me a way to set a variable in the session in the JSP
> and
> > access that variable in the action class.. (variable must be set from the
> > JSP file to the session)
> >
> > iam using struts 2.. is there anyway to do this..?
> >
> > --
> > Regards
> > Kushan Jayathilake
> >
>
>
>
> --
> Wes Wannemacher
>
> Head Engineer, WanTii, Inc.
> Need Training? Struts, Spring, Maven, Tomcat...
> Ask me for a quote!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: How to set a variable in session in the JSP

Posted by Wes Wannemacher <we...@wantii.com>.
http://struts.apache.org/2.x/docs/set.html

Notice particularly the "scope" attribute.

-Wes

On Fri, Jun 18, 2010 at 1:01 AM, Kushan Jayathilake <ku...@gmail.com> wrote:
> Hi Guys,
>
> Can anyone suggest me a way to set a variable in the session in the JSP and
> access that variable in the action class.. (variable must be set from the
> JSP file to the session)
>
> iam using struts 2.. is there anyway to do this..?
>
> --
> Regards
> Kushan Jayathilake
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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


RE: How to set a variable in session in the JSP

Posted by Mano Dasanayaka <d....@directfn.com>.
Hi Kushan,

You can access the session object using a jsp scriplet and set your required
variables there.
<%
request.getSession().setAttribute("your_variable",obj)

%>


>From the Action class , you can get access to session either by implementing
the ServletRequestAware 
Interface within your action class or by implementing the SessionAware
interface.


request.getSession().getAttribute("var");

Thanks & Regards,
Mano

-----Original Message-----
From: Kushan Jayathilake [mailto:kushanxp@gmail.com] 
Sent: Friday, June 18, 2010 10:32 AM
To: user@struts.apache.org
Subject: How to set a variable in session in the JSP

Hi Guys,

Can anyone suggest me a way to set a variable in the session in the JSP and
access that variable in the action class.. (variable must be set from the
JSP file to the session)

iam using struts 2.. is there anyway to do this..?

-- 
Regards
Kushan Jayathilake


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


Re: How to set a variable in session in the JSP

Posted by Kushan Jayathilake <ku...@gmail.com>.
Hi Mano,

Thanks for your reply, i got it sort out :)

-- 
Regards
Kushan Jayathilake


On Sun, Jun 20, 2010 at 11:58 AM, Mano Dasanayaka <d....@directfn.com>wrote:

> Hi Kushan,
>
> You can access the session object using a jsp scriplet and set your
> required
> variables there.
> <%
> request.getSession().setAttribute("your_variable",obj)
>
> %>
>
>
> From the Action class , you can get access to session either by
> implementing
> the ServletRequestAware
> Interface within your action class or by implementing the SessionAware
> interface.
>
>
> request.getSession().getAttribute("var");
>
> Thanks & Regards,
> Mano
>
> -----Original Message-----
> From: Kushan Jayathilake [mailto:kushanxp@gmail.com]
> Sent: Friday, June 18, 2010 10:32 AM
> To: user@struts.apache.org
> Subject: How to set a variable in session in the JSP
>
> Hi Guys,
>
> Can anyone suggest me a way to set a variable in the session in the JSP and
> access that variable in the action class.. (variable must be set from the
> JSP file to the session)
>
> iam using struts 2.. is there anyway to do this..?
>
> --
> Regards
> Kushan Jayathilake
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>