You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Wes Wannemacher <we...@wantii.com> on 2008/01/07 20:36:42 UTC

getting namespace of action through OGNL

Hello,

I have been struggling to find a way to see what the namespace is from
within a JSP. So, from the view, I would like to know what the namespace
is of the action that was called. 

The following scriptlet correctly returns the namespace - 
<%
com.opensymphony.xwork2.ActionContext ac =
com.opensymphony.xwork2.ActionContext.getContext();
com.opensymphony.xwork2.ActionInvocation invo =
ac.getActionInvocation();
com.opensymphony.xwork2.ActionProxy proxy = invo.getProxy();
out.println(proxy.getNamespace());
%>

But, if I use the following OGNL -
<s:property
value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/>

I just get a big fat nothing :(

I am open to any suggestions, I just want to know what namespace is... I
don't really want to parse the requested URL. If I can't figure it out
in OGNL, I will probably write an interceptor to put the value somewhere
that I can read it in OGNL. Before I write the interceptor through, I
figured I would ask here since I might be doing something blatantly
wrong.

Thanks!

-Wes


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


Re: getting namespace of action through OGNL

Posted by Ted Husted <hu...@apache.org>.
I could see an argument for exposing the namespace as an attribute in
the normal course.

If nothing else, I wonder if we should roll this functionality into
the SiteMesh plugin.

-Ted.

On Jan 7, 2008 3:39 PM, Wes Wannemacher <we...@wantii.com> wrote:
>
> On Mon, 2008-01-07 at 15:20 -0500, Ted Husted wrote:
> > The Action (or an Interceptor) could set it as a property that a tag
> > could then access.
>
> I went ahead and wrote an interceptor. The only problem I have with
> doing it this way though is that the interceptor is doing some magic
> that isn't quite apparent when looking at the JSP / OGNL code.
>
> >
> > We can set properties directly to the stack from an Interceptor. (This
> > is how the validators expose bad input to a typed field.)
> >
> > As a general rule, pages should be as dumb as possible, and only know
> > what the value stack tells it.
>
> Yeah, I agree with that, but in my case this is a sitemesh decorator
> page. I am rendering a menu based on the section of the site they are
> using. Each namespace is like a 'module,' so I figure if the user is
> looking at namespace X, they are presented with a menu that corresponds
> to namespace X. I just didn't realize at design time that there wasn't a
> quick way to get the namespace within OGNL.
>
> >
> > Note that the value stack properties are exposed as request
> > properties, so any property set should be available to the tags and
> > JSTL without any weird OGNL syntax.
> >
>
> I can normally get what I want pretty easily with OGNL. This call was a
> bit more complex but I still don't understand why it doesn't work. I'm
> happy with my current solution, but was hoping someone could explain
> what is wrong with the expression as I had it originally.
>
> -Wes
>
>
> > HTH, Ted
> >  * <http://www.StrutsMentor.com/>
> >
> > On Jan 7, 2008 2:36 PM, Wes Wannemacher <we...@wantii.com> wrote:
> > > Hello,
> > >
> > > I have been struggling to find a way to see what the namespace is from
> > > within a JSP. So, from the view, I would like to know what the namespace
> > > is of the action that was called.
> > >
> > > The following scriptlet correctly returns the namespace -
> > > <%
> > > com.opensymphony.xwork2.ActionContext ac =
> > > com.opensymphony.xwork2.ActionContext.getContext();
> > > com.opensymphony.xwork2.ActionInvocation invo =
> > > ac.getActionInvocation();
> > > com.opensymphony.xwork2.ActionProxy proxy = invo.getProxy();
> > > out.println(proxy.getNamespace());
> > > %>
> > >
> > > But, if I use the following OGNL -
> > > <s:property
> > > value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/>
> > >
> > > I just get a big fat nothing :(
> > >
> > > I am open to any suggestions, I just want to know what namespace is... I
> > > don't really want to parse the requested URL. If I can't figure it out
> > > in OGNL, I will probably write an interceptor to put the value somewhere
> > > that I can read it in OGNL. Before I write the interceptor through, I
> > > figured I would ask here since I might be doing something blatantly
> > > wrong.
> > >
> > > Thanks!
> > >
> > > -Wes

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


Re: getting namespace of action through OGNL

Posted by Wes Wannemacher <we...@wantii.com>.
On Mon, 2008-01-07 at 15:20 -0500, Ted Husted wrote:
> The Action (or an Interceptor) could set it as a property that a tag
> could then access.

I went ahead and wrote an interceptor. The only problem I have with
doing it this way though is that the interceptor is doing some magic
that isn't quite apparent when looking at the JSP / OGNL code.

> 
> We can set properties directly to the stack from an Interceptor. (This
> is how the validators expose bad input to a typed field.)
> 
> As a general rule, pages should be as dumb as possible, and only know
> what the value stack tells it.

Yeah, I agree with that, but in my case this is a sitemesh decorator
page. I am rendering a menu based on the section of the site they are
using. Each namespace is like a 'module,' so I figure if the user is
looking at namespace X, they are presented with a menu that corresponds
to namespace X. I just didn't realize at design time that there wasn't a
quick way to get the namespace within OGNL.

> 
> Note that the value stack properties are exposed as request
> properties, so any property set should be available to the tags and
> JSTL without any weird OGNL syntax.
> 

I can normally get what I want pretty easily with OGNL. This call was a
bit more complex but I still don't understand why it doesn't work. I'm
happy with my current solution, but was hoping someone could explain
what is wrong with the expression as I had it originally.

-Wes

> HTH, Ted
>  * <http://www.StrutsMentor.com/>
> 
> On Jan 7, 2008 2:36 PM, Wes Wannemacher <we...@wantii.com> wrote:
> > Hello,
> >
> > I have been struggling to find a way to see what the namespace is from
> > within a JSP. So, from the view, I would like to know what the namespace
> > is of the action that was called.
> >
> > The following scriptlet correctly returns the namespace -
> > <%
> > com.opensymphony.xwork2.ActionContext ac =
> > com.opensymphony.xwork2.ActionContext.getContext();
> > com.opensymphony.xwork2.ActionInvocation invo =
> > ac.getActionInvocation();
> > com.opensymphony.xwork2.ActionProxy proxy = invo.getProxy();
> > out.println(proxy.getNamespace());
> > %>
> >
> > But, if I use the following OGNL -
> > <s:property
> > value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/>
> >
> > I just get a big fat nothing :(
> >
> > I am open to any suggestions, I just want to know what namespace is... I
> > don't really want to parse the requested URL. If I can't figure it out
> > in OGNL, I will probably write an interceptor to put the value somewhere
> > that I can read it in OGNL. Before I write the interceptor through, I
> > figured I would ask here since I might be doing something blatantly
> > wrong.
> >
> > 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: getting namespace of action through OGNL

Posted by Ted Husted <hu...@apache.org>.
The Action (or an Interceptor) could set it as a property that a tag
could then access.

We can set properties directly to the stack from an Interceptor. (This
is how the validators expose bad input to a typed field.)

As a general rule, pages should be as dumb as possible, and only know
what the value stack tells it.

Note that the value stack properties are exposed as request
properties, so any property set should be available to the tags and
JSTL without any weird OGNL syntax.

HTH, Ted
 * <http://www.StrutsMentor.com/>

On Jan 7, 2008 2:36 PM, Wes Wannemacher <we...@wantii.com> wrote:
> Hello,
>
> I have been struggling to find a way to see what the namespace is from
> within a JSP. So, from the view, I would like to know what the namespace
> is of the action that was called.
>
> The following scriptlet correctly returns the namespace -
> <%
> com.opensymphony.xwork2.ActionContext ac =
> com.opensymphony.xwork2.ActionContext.getContext();
> com.opensymphony.xwork2.ActionInvocation invo =
> ac.getActionInvocation();
> com.opensymphony.xwork2.ActionProxy proxy = invo.getProxy();
> out.println(proxy.getNamespace());
> %>
>
> But, if I use the following OGNL -
> <s:property
> value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/>
>
> I just get a big fat nothing :(
>
> I am open to any suggestions, I just want to know what namespace is... I
> don't really want to parse the requested URL. If I can't figure it out
> in OGNL, I will probably write an interceptor to put the value somewhere
> that I can read it in OGNL. Before I write the interceptor through, I
> figured I would ask here since I might be doing something blatantly
> wrong.
>
> Thanks!
>
> -Wes

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


Re: [struts] getting namespace of action through OGNL

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Wes Wannemacher wrote:
> On Tue, 2008-01-08 at 01:21 -0500, Dale Newfield wrote:
>   
>> Jeromy Evans wrote:
>>     
>>> Wes Wannemacher wrote:
>>>       
>>>> But, if I use the following OGNL -
>>>> <s:property
>>>> value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/> 
>>>>
>>>>
>>>> I just get a big fat nothing :(
>>>>         
>>> Works for me!
>>>       
>> Maybe it's working for both of you--Wes, are you certain that the 
>> appropriate answer for the page you're viewing isn't ""?  Jeromy gets 
>> "/secure" for the page he tested, but had he tested a different page...
>>     
> There have been some recent updates in SVN, so I am going to rebuild and
> redeploy. If it works as advertised in 2.0.x but not in 2.1.x I may file
> a JIRA. 
>
>   

Hi Wes, as you suspected:

struts 2.0.11: works
struts 2.1.0 (default settings): big fat nothing

But I just remembered, struts 2.1 has a new constant:

struts.ognl.allowStaticMethodAccess that's false by default.

Setting the constant to true made it work in 2.1 as well.

cheers,
 Jeromy Evans


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


Re: [struts] getting namespace of action through OGNL

Posted by Wes Wannemacher <we...@wantii.com>.
On Tue, 2008-01-08 at 01:21 -0500, Dale Newfield wrote:
> Jeromy Evans wrote:
> > Wes Wannemacher wrote:
> >> But, if I use the following OGNL -
> >> <s:property
> >> value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/> 
> >>
> >>
> >> I just get a big fat nothing :(
> >
> > Works for me!
> 
> Maybe it's working for both of you--Wes, are you certain that the 
> appropriate answer for the page you're viewing isn't ""?  Jeromy gets 
> "/secure" for the page he tested, but had he tested a different page...

Yes, I am sure, in my original message I had a scriptlet that executed
and output what I wanted, but the OGNL right below it did not. I am
using the 2.1.x snapshot, so there may be an issue with it. I actually
tried a few different permutations of the OGNL code I listed and was
unable to get any results. The call to the static method 'getContext()'
returned null. The ActionContext is threadlocal so I wonder if things
maybe have changed between 2.0 / 2.1 making it unavailable within OGNL.

To be sure that Sitemesh / ActionContextCleanup weren't the issue, I had
tried it within an undecorated action too and got null.

There have been some recent updates in SVN, so I am going to rebuild and
redeploy. If it works as advertised in 2.0.x but not in 2.1.x I may file
a JIRA. 

In the meantime I worked around the issue, so my current project is
still rolling along! Thanks for the help!

-Wes


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


Re: [struts] getting namespace of action through OGNL

Posted by Dale Newfield <Da...@Newfield.org>.
Jeromy Evans wrote:
> Wes Wannemacher wrote:
>> But, if I use the following OGNL -
>> <s:property
>> value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/> 
>>
>>
>> I just get a big fat nothing :(
>
> Works for me!

Maybe it's working for both of you--Wes, are you certain that the 
appropriate answer for the page you're viewing isn't ""?  Jeromy gets 
"/secure" for the page he tested, but had he tested a different page...

-Dale

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


Re: getting namespace of action through OGNL

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Wes Wannemacher wrote:
> Hello,
>
> I have been struggling to find a way to see what the namespace is from
> within a JSP. So, from the view, I would like to know what the namespace
> is of the action that was called. 
>
> The following scriptlet correctly returns the namespace - 
> <%
> com.opensymphony.xwork2.ActionContext ac =
> com.opensymphony.xwork2.ActionContext.getContext();
> com.opensymphony.xwork2.ActionInvocation invo =
> ac.getActionInvocation();
> com.opensymphony.xwork2.ActionProxy proxy = invo.getProxy();
> out.println(proxy.getNamespace());
> %>
>
> But, if I use the following OGNL -
> <s:property
> value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/>
>
> I just get a big fat nothing :(
>
>   
Hi Wes,

Works for me!

<s:property value="@com.opensymphony.xwork2.ActionContext@getContext().actionInvocation.proxy.namespace"/>

Outputs:
/secure

when used in a JSP result for an action within the package definition :

<package name="admin" extends="secure" namespace="/secure">

in Struts 2.0.11

cheers,
Jeromy Evans

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