You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pierre Thibaudeau <pi...@gmail.com> on 2008/07/14 19:20:01 UTC

[S2] newly-created Session context

I have an interceptor that puts an object in the Session scope.

The interceptor calls the following when retrieving the Session:

    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext context = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest)
context.get(StrutsStatics.HTTP_REQUEST);
        HttpSession session = request.getSession(true /* create if need be
*/);
        session.setAttribute(attributeName, someObject);
        ...
    }

Given that code, I don't see how the Session could be null after that
interception --- otherwise an exception would be thrown at the point when I
attempt to set the Session attribute.

Yet, immediately after I start the server (therefore no Session has yet been
created), if I issue a browser request on an action that uses that
interceptor, OGNL reports the Session as being indeed null --- or so the
xml-debug output of the context shows.  It's only on the second request that
the xml-debug shows a Session context containing the desired object.

Is there a way of explaining this discrepancy?
Apart from this custom interceptor, I use the paramPrepareParam stack.
Would there be another interceptor in there interfering with my custom
newly-created Session (but not interfering when the Session is not new
anymore)?

Toughts?

Re: [S2] newly-created Session context

Posted by Pierre Thibaudeau <pi...@gmail.com>.
Thanks for the heads up! I'm learning every day!
And yes, I concur:  this definitely needs to be documented!

2008/7/14 Gabriel Belingueres <be...@gmail.com>:
> Seems like something is not correctly initialized in OGNL when you get
> the session from the HTTPServletRequest object this way (it should be
> documented somewhere or otherwise fix it.)
>
> But you don't need to get both the HTTPSession and the Map to put
> something in session scope. The SessionMap is an adapter to ease unit
> testing of actions, and every change is is done to the SessionMap it
> is delegated to the HTTPSession object.
>
> 2008/7/14 Pierre Thibaudeau <pi...@gmail.com>:
>> 2008/7/14 Gabriel Belingueres <be...@gmail.com>:
>>> Please test your interceptor using the following to get the session:
>>> Map session = invocation.getInvocationContext().getSession();
>>
>> Well, Gabriel:
>>
>> a)  I'll be d*mned!
>> b)  You're a genius!  ;) (I would never have thought of looking that way!)
>>
>> Now, it's worth making an extra comment about this strange situation.
>> I patched my interceptor as follows:
>>
>>        public String intercept(ActionInvocation invocation) throws Exception {
>>                ActionContext context = invocation.getInvocationContext();
>>                HttpServletRequest request = (HttpServletRequest)
>> context.get(StrutsStatics.HTTP_REQUEST);
>>                HttpSession session = request.getSession(true /* create if not yet
>> in existence */);
>>                Map<?,?> sessionMap = invocation.getInvocationContext().getSession();
>>                ...
>>        }
>>
>> In other words, I didn't merely REPLACE my previous HttpSession by the
>> Map<?,?>, but I added the Map<?,?>.  With this new setup, so long as I
>> put one object in the sessionMap, then the other object (yes, there
>> were two of them;  I hadn't mention the second one) is set properly
>> with HttpSession.setAttribute(String).  And BOTH object are recognized
>> by OGNL, not merely the one that I had Map.put(Object, Object)...
>>
>> So basically, we are facing an initialization issue somewhere.  I wish
>> there was a more elegant way of preparing that new session...  But at
>> least, now the interceptor behaves as intended.
>>
>> I bet this issue should be made explicit in the Strut2 /
>> ActionInvocation /OGNL guides...
>>
>> Thanks!
>>
>> ---------------------------------------------------------------------
>> 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: [S2] newly-created Session context

Posted by Gabriel Belingueres <be...@gmail.com>.
Seems like something is not correctly initialized in OGNL when you get
the session from the HTTPServletRequest object this way (it should be
documented somewhere or otherwise fix it.)

But you don't need to get both the HTTPSession and the Map to put
something in session scope. The SessionMap is an adapter to ease unit
testing of actions, and every change is is done to the SessionMap it
is delegated to the HTTPSession object.

2008/7/14 Pierre Thibaudeau <pi...@gmail.com>:
> 2008/7/14 Gabriel Belingueres <be...@gmail.com>:
>> Please test your interceptor using the following to get the session:
>> Map session = invocation.getInvocationContext().getSession();
>
> Well, Gabriel:
>
> a)  I'll be d*mned!
> b)  You're a genius!  ;) (I would never have thought of looking that way!)
>
> Now, it's worth making an extra comment about this strange situation.
> I patched my interceptor as follows:
>
>        public String intercept(ActionInvocation invocation) throws Exception {
>                ActionContext context = invocation.getInvocationContext();
>                HttpServletRequest request = (HttpServletRequest)
> context.get(StrutsStatics.HTTP_REQUEST);
>                HttpSession session = request.getSession(true /* create if not yet
> in existence */);
>                Map<?,?> sessionMap = invocation.getInvocationContext().getSession();
>                ...
>        }
>
> In other words, I didn't merely REPLACE my previous HttpSession by the
> Map<?,?>, but I added the Map<?,?>.  With this new setup, so long as I
> put one object in the sessionMap, then the other object (yes, there
> were two of them;  I hadn't mention the second one) is set properly
> with HttpSession.setAttribute(String).  And BOTH object are recognized
> by OGNL, not merely the one that I had Map.put(Object, Object)...
>
> So basically, we are facing an initialization issue somewhere.  I wish
> there was a more elegant way of preparing that new session...  But at
> least, now the interceptor behaves as intended.
>
> I bet this issue should be made explicit in the Strut2 /
> ActionInvocation /OGNL guides...
>
> Thanks!
>
> ---------------------------------------------------------------------
> 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: [S2] newly-created Session context

Posted by Pierre Thibaudeau <pi...@gmail.com>.
2008/7/14 Gabriel Belingueres <be...@gmail.com>:
> Please test your interceptor using the following to get the session:
> Map session = invocation.getInvocationContext().getSession();

Well, Gabriel:

a)  I'll be d*mned!
b)  You're a genius!  ;) (I would never have thought of looking that way!)

Now, it's worth making an extra comment about this strange situation.
I patched my interceptor as follows:

	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext context = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest)
context.get(StrutsStatics.HTTP_REQUEST);
		HttpSession session = request.getSession(true /* create if not yet
in existence */);
		Map<?,?> sessionMap = invocation.getInvocationContext().getSession();
                ...
        }

In other words, I didn't merely REPLACE my previous HttpSession by the
Map<?,?>, but I added the Map<?,?>.  With this new setup, so long as I
put one object in the sessionMap, then the other object (yes, there
were two of them;  I hadn't mention the second one) is set properly
with HttpSession.setAttribute(String).  And BOTH object are recognized
by OGNL, not merely the one that I had Map.put(Object, Object)...

So basically, we are facing an initialization issue somewhere.  I wish
there was a more elegant way of preparing that new session...  But at
least, now the interceptor behaves as intended.

I bet this issue should be made explicit in the Strut2 /
ActionInvocation /OGNL guides...

Thanks!

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


Re: [S2] newly-created Session context

Posted by Gabriel Belingueres <be...@gmail.com>.
Please test your interceptor using the following to get the session:
Map session = invocation.getInvocationContext().getSession();

2008/7/14 Pierre Thibaudeau <pi...@gmail.com>:
> 2008/7/14 Gabriel Belingueres <be...@gmail.com>:
>
>> That's strange.
>> Which S2 version are you using?
>>
>
> Struts 2.1.2
>

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


Re: [S2] newly-created Session context

Posted by Pierre Thibaudeau <pi...@gmail.com>.
2008/7/14 Gabriel Belingueres <be...@gmail.com>:

> That's strange.
> Which S2 version are you using?
>

Struts 2.1.2

Re: [S2] newly-created Session context

Posted by Gabriel Belingueres <be...@gmail.com>.
That's strange.
Which S2 version are you using?

2008/7/14 Pierre Thibaudeau <pi...@gmail.com>:
> Extra update.
>
> What makes this even more puzzling is that EL can find the object while OGNL
> cannot.  On the first request,
> ${userLanguage} will display the correct value, but
> <s:property value="#session['userLanguage']"/> will display nothing.
>
> On the second request, both expressions will display the same (and correct)
> value.
>

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


Re: [S2] newly-created Session context

Posted by Pierre Thibaudeau <pi...@gmail.com>.
Extra update.

What makes this even more puzzling is that EL can find the object while OGNL
cannot.  On the first request,
${userLanguage} will display the correct value, but
<s:property value="#session['userLanguage']"/> will display nothing.

On the second request, both expressions will display the same (and correct)
value.

Re: [S2] newly-created Session context

Posted by Pierre Thibaudeau <pi...@gmail.com>.
2008/7/14 Gabriel Belingueres <be...@gmail.com>:

> Where in the interceptor chain do your custom interceptor execute?
>

            <interceptor-stack name="customStack">
                <interceptor-ref name="userLanguage"/>
                <interceptor-ref name="debugging"/>
                <interceptor-ref name="autoLogin"/>
                <interceptor-ref name="paramsPrepareParamsStack" />
            </interceptor-stack>

The interceptor I mentioned in my earlier post is the first one:
userLanguage.

BTW, to make more accurate what I stated earlier, the xml-debug tree does
not actually claim that the Session is null;  rather that it's empty.
Here's the relevant section:

<debug>

  <parameters/>

  <context>
   ...
 </context>

 <request>
   ...
 </request>

  <session/>

  <valueStack>
   ...
  </valueStack>

</debug>

As you can see, the <session/> tag is empty...

Re: [S2] newly-created Session context

Posted by Gabriel Belingueres <be...@gmail.com>.
Where in the interceptor chain do your custom interceptor execute?

2008/7/14 Pierre Thibaudeau <pi...@gmail.com>:
> I have an interceptor that puts an object in the Session scope.
>
> The interceptor calls the following when retrieving the Session:
>
>    public String intercept(ActionInvocation invocation) throws Exception {
>        ActionContext context = invocation.getInvocationContext();
>        HttpServletRequest request = (HttpServletRequest)
> context.get(StrutsStatics.HTTP_REQUEST);
>        HttpSession session = request.getSession(true /* create if need be
> */);
>        session.setAttribute(attributeName, someObject);
>        ...
>    }
>
> Given that code, I don't see how the Session could be null after that
> interception --- otherwise an exception would be thrown at the point when I
> attempt to set the Session attribute.
>
> Yet, immediately after I start the server (therefore no Session has yet been
> created), if I issue a browser request on an action that uses that
> interceptor, OGNL reports the Session as being indeed null --- or so the
> xml-debug output of the context shows.  It's only on the second request that
> the xml-debug shows a Session context containing the desired object.
>
> Is there a way of explaining this discrepancy?
> Apart from this custom interceptor, I use the paramPrepareParam stack.
> Would there be another interceptor in there interfering with my custom
> newly-created Session (but not interfering when the Session is not new
> anymore)?
>
> Toughts?
>

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