You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Brad A Cupit <br...@lsu.edu> on 2008/06/24 23:51:58 UTC

use OGNL to automatically look in the session?

I have an object that I need to access in a JSP, and the object has a
getter on my Action. When the page is initially viewed the object is
retrieved off the Value Stack, but if the user is redirected back to the
page (due to validation errors) the value comes from the session.

When using EL things work as expected: ${myObject.property} first looks
in the request, then in the session, so the page doesn't need any
special case code.

What I was hoping was the OGNL would check the Value Stack, and if it
didn't find it there, it would automatically look in the request, then
the session, etc.

I'm using the Scope plugin to put this object in the session (FLASH
scope), so unfortunately the object isn't there until after the result
has been executed.

The goal is to have the same syntax for accessing the object, regardless
of where the object is (on the ValueStack or in the session).

I've got a few ideas, but none of them are very nice.

Brad Cupit
Louisiana State University - UIS


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


RE: use OGNL to automatically look in the session?

Posted by Brad A Cupit <br...@lsu.edu>.
Musachy Barroso wrote:
> "#attr" does something similar:

oh wow! you gave me the answer and I didn't read it!

I incorrectly assumed you were using attr to refer to a property on the
value stack. I should have followed the link instead of being arrogant.
Sorry!

Thanks for the answer Musachy! That's exactly what I was looking for!!

Brad Cupit
Louisiana State University - UIS

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


Re: use OGNL to automatically look in the session?

Posted by Martin <mg...@hotmail.com>.
The OGNL creators/authors were definitely 'page' centric e.g.
#attr['foo'] or #attr.foo 
resolves to
1)Access to PageContext if available, otherwise searches 
2)request
3)session
4)application respectively

http://struts.apache.org/2.0.11.1/docs/ognl-basics.html

IMHO
Martin

----- Original Message ----- 
From: "Brad A Cupit" <br...@lsu.edu>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, June 24, 2008 6:19 PM
Subject: RE: use OGNL to automatically look in the session?


Musachy Barroso wrote:
> "#attr" does something similar:

right, but it's kind of yucky in the sense that each of these objects
needs a block like this:

<s:if test="%{myObject == null}">
    <s:set name="myObject" value="%{#session.myObject}" />
</s:if>
<s:else>
    <s:set name="myObject" value="%{myObject}" />
</s:else>

and then I change use of it from:
<s:label value="%{myObject.property}" />

to:
<s:label value="%{#myObject.property}" />

I can live with that, but it seems like a step backward. JSP EL could
automatically look in the session, but now I have to explicitly state
that it's in the session.

Maybe I'm in the minority here...

Brad Cupit
Louisiana State University - UIS


---------------------------------------------------------------------
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: use OGNL to automatically look in the session?

Posted by Brad A Cupit <br...@lsu.edu>.
Brad Cupit wrote:
> I'm not sure which order the scopes are checked

For anyone who's interested, #attr appears to check scopes in the
following order:

1. page / action *
2. request
3. value stack
4. session
5. application

* I used <s:set> to set one value at page scope and one at action scope,
and whichever was declared last was the one that #attr found first.

<s:set name="stuff" value="%{'action'}" scope="action" />
<s:set name="stuff" value="%{'page'}" scope="page" />
<s:label value="%{#attr.stuff}" />

outputs page

<s:set name="stuff" value="%{'page'}" scope="page" />
<s:set name="stuff" value="%{'action'}" scope="action" />
<s:label value="%{#attr.stuff}" />

outputs action

I'm not really sure what the action scope is. Perhaps it's only related
to <s:action>. Either way, maybe this post will help someone searching
the mailing list!

Brad Cupit
Louisiana State University - UIS

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


RE: use OGNL to automatically look in the session?

Posted by Brad A Cupit <br...@lsu.edu>.
Chris Pratt wrote:
> #attr doesn't sound like it will work in your case since
> it only searches the old four scopes, and never searches
> the value stack.

After I figured out that Musachy had given me the right answer (and
Martin also gave the right answer), I was a little worried that this
wouldn't do what I wanted, which is to search the Value Stack, then the
page, then the request, then the session, then the application. So I
wrote a quick little experiment and #attr actually does search the value
stack.

I'm not sure which order the scopes are checked, but it works great for
my needs!

I wonder if the request that it's checking is the 'raw' request or the
specially-wrapped request which delegates to the value stack. If it's
the second one, then that makes sense why #attr does what I want! 

Thanks again for the help guys!!

Brad Cupit
Louisiana State University - UIS

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


Re: use OGNL to automatically look in the session?

Posted by Chris Pratt <th...@gmail.com>.
According to http://struts.apache.org/2.0.11.1/docs/ognl-basics.html
"Struts 2 places request parameters and request, session, and
application attributes on the OGNL stack."  So, you'd think you could
just request the attribute and it would search the stack for the
value, but I agree that I haven't seen this behavior work in practice.

#attr doesn't sound like it will work in your case since it only
searches the old four scopes, and never searches the value stack.

Your best bet might be to use the old JSP EL and let the, aptly named,
request doohickey check the value stack for you.
  (*Chris*)

On Tue, Jun 24, 2008 at 3:19 PM, Brad A Cupit <br...@lsu.edu> wrote:
> Musachy Barroso wrote:
>> "#attr" does something similar:
>
> right, but it's kind of yucky in the sense that each of these objects
> needs a block like this:
>
> <s:if test="%{myObject == null}">
>    <s:set name="myObject" value="%{#session.myObject}" />
> </s:if>
> <s:else>
>    <s:set name="myObject" value="%{myObject}" />
> </s:else>
>
> and then I change use of it from:
> <s:label value="%{myObject.property}" />
>
> to:
> <s:label value="%{#myObject.property}" />
>
> I can live with that, but it seems like a step backward. JSP EL could
> automatically look in the session, but now I have to explicitly state
> that it's in the session.
>
> Maybe I'm in the minority here...
>
> Brad Cupit
> Louisiana State University - UIS
>
>
> ---------------------------------------------------------------------
> 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: use OGNL to automatically look in the session?

Posted by Brad A Cupit <br...@lsu.edu>.
Musachy Barroso wrote:
> "#attr" does something similar:

right, but it's kind of yucky in the sense that each of these objects
needs a block like this:

<s:if test="%{myObject == null}">
    <s:set name="myObject" value="%{#session.myObject}" />
</s:if>
<s:else>
    <s:set name="myObject" value="%{myObject}" />
</s:else>

and then I change use of it from:
<s:label value="%{myObject.property}" />

to:
<s:label value="%{#myObject.property}" />

I can live with that, but it seems like a step backward. JSP EL could
automatically look in the session, but now I have to explicitly state
that it's in the session.

Maybe I'm in the minority here...

Brad Cupit
Louisiana State University - UIS


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


Re: use OGNL to automatically look in the session?

Posted by Musachy Barroso <mu...@gmail.com>.
"#attr" does something similar:

http://struts.apache.org/2.x/docs/ognl.html

musachy

On Tue, Jun 24, 2008 at 5:51 PM, Brad A Cupit <br...@lsu.edu> wrote:
> I have an object that I need to access in a JSP, and the object has a
> getter on my Action. When the page is initially viewed the object is
> retrieved off the Value Stack, but if the user is redirected back to the
> page (due to validation errors) the value comes from the session.
>
> When using EL things work as expected: ${myObject.property} first looks
> in the request, then in the session, so the page doesn't need any
> special case code.
>
> What I was hoping was the OGNL would check the Value Stack, and if it
> didn't find it there, it would automatically look in the request, then
> the session, etc.
>
> I'm using the Scope plugin to put this object in the session (FLASH
> scope), so unfortunately the object isn't there until after the result
> has been executed.
>
> The goal is to have the same syntax for accessing the object, regardless
> of where the object is (on the ValueStack or in the session).
>
> I've got a few ideas, but none of them are very nice.
>
> Brad Cupit
> Louisiana State University - UIS
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: use OGNL to automatically look in the session?

Posted by Dave Newton <ne...@yahoo.com>.
JSP EL? It checks JEE scopes first then the stack due to S2's custom request doohickey.

Dave

--- On Tue, 6/24/08, Brad A Cupit <br...@lsu.edu> wrote:

> From: Brad A Cupit <br...@lsu.edu>
> Subject: use OGNL to automatically look in the session?
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Date: Tuesday, June 24, 2008, 5:51 PM
> I have an object that I need to access in a JSP, and the
> object has a
> getter on my Action. When the page is initially viewed the
> object is
> retrieved off the Value Stack, but if the user is
> redirected back to the
> page (due to validation errors) the value comes from the
> session.
> 
> When using EL things work as expected: ${myObject.property}
> first looks
> in the request, then in the session, so the page
> doesn't need any
> special case code.
> 
> What I was hoping was the OGNL would check the Value Stack,
> and if it
> didn't find it there, it would automatically look in
> the request, then
> the session, etc.
> 
> I'm using the Scope plugin to put this object in the
> session (FLASH
> scope), so unfortunately the object isn't there until
> after the result
> has been executed.
> 
> The goal is to have the same syntax for accessing the
> object, regardless
> of where the object is (on the ValueStack or in the
> session).
> 
> I've got a few ideas, but none of them are very nice.
> 
> Brad Cupit
> Louisiana State University - UIS
> 
> 
> ---------------------------------------------------------------------
> 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