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 06:52:35 UTC

[S2] current URL in ValueStack?

(Struts 2.1.2)
In the JSP, is it possible through some OGNL expression to get hold of the
URL of the current request?  I looked through the XML output obtained
through debugging, but could not find anything.

(I know I can get hold of the action and parameters; so there might a
complicated way out...)

Re: [S2] current URL in ValueStack?

Posted by Pierre Thibaudeau <pi...@gmail.com>.
> After a forward, the requestURI is now available in the request map:
>
> #request['javax.servlet.forward.request_uri']

Great, I'm learning!


> Snippet:
>           StringBuilder uri = new StringBuilder();
>           RequestMap requestMap = (RequestMap)
> stack.getContext().get("request");
>           String forwardURI = (String)
> requestMap.get("javax.servlet.forward.request_uri");
>           if ((forwardURI != null) && (forwardURI.length() > 0)) {
>                  uri.append(forwardURI);                         } else {
>                  uri.append(request.getRequestURI());
>   }

Thank you!

I did go ahead and implement my interceptor solution, but this tag
idea is definitely leaner for the structure, which is very good!

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


Re: [S2] current URL in ValueStack?

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Pierre Thibaudeau wrote:
> Earlier in this conversation, Jeromy proposed two ways of accessing
> the current URL (or URI), one through EL and the other through OGNL
>
>   
>> ${pageContext.request.requestURI}
>> <s:property value="%{#context['com.opensymphony.xwork2.dispatcher.HttpServletRequest'].requestURI}"/>
>>     
>
> That works great until (it seems) Tiles2 gets in the picture.  By that
> point, the address returned is that of the Tiles layout (with
> extension ".jsp") and not the logical address of the request.  (I
> haven't made a systematic study of the exact conditions for this
> phenomenon to occur.)
>
> It seems to me that, having access to the current (logical) URL (the
> one that generated this request) is a fairly common need.  Has anyone
> come up with another way of getting hold of it?
>   

I was hoping you wouldn't encounter that little problem.  When your 
container forwards to a JSP, the requestURI is now the JSP rather than 
the original URI.

After a forward, the requestURI is now available in the request map:

#request['javax.servlet.forward.request_uri']

But of course, that won't be set if there wasn't a forward, so you can't 
always use that.

As logic's involved, my solution was to create a custom tag with the 
following code extract.  I never understood why the requestURI was never 
readily available in the action's context though. However I rarely need 
it and most pages don't require it.

Snippet:
            StringBuilder uri = new StringBuilder();
            RequestMap requestMap = (RequestMap) 
stack.getContext().get("request");
            String forwardURI = (String) 
requestMap.get("javax.servlet.forward.request_uri");
            if ((forwardURI != null) && (forwardURI.length() > 0)) 
{          
                   uri.append(forwardURI);              
            } else {
                   uri.append(request.getRequestURI());              
            }


regards,
 Jeromy Evans


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


Re: [S2] current URL in ValueStack?

Posted by Pierre Thibaudeau <pi...@gmail.com>.
Earlier in this conversation, Jeromy proposed two ways of accessing
the current URL (or URI), one through EL and the other through OGNL

> ${pageContext.request.requestURI}
> <s:property value="%{#context['com.opensymphony.xwork2.dispatcher.HttpServletRequest'].requestURI}"/>

That works great until (it seems) Tiles2 gets in the picture.  By that
point, the address returned is that of the Tiles layout (with
extension ".jsp") and not the logical address of the request.  (I
haven't made a systematic study of the exact conditions for this
phenomenon to occur.)

It seems to me that, having access to the current (logical) URL (the
one that generated this request) is a fairly common need.  Has anyone
come up with another way of getting hold of it?

There is a failsafe option:  make an interceptor that places the URL
in question in the Request scope, but that seems a little heavy-handed
to me...

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


Re: [S2] current URL in ValueStack?

Posted by Pierre Thibaudeau <pi...@gmail.com>.
>
> The benefit of EL is that your IDE (hopefully) understands it,


I wish my MyEclipse JSP editor was as clever as that...  (Should I just
revert to Eclipse with the correct collection of plugins? or maybe switch to
IntelliJ?)
In any case, thanks a million!

Re: [S2] current URL in ValueStack?

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Pierre Thibaudeau wrote:
>
> But your statement about EL intrigues me.  How would you do that in EL?  Is
> there some value available to EL in the request context?
>
>   
simply ${pageContext.request.requestURI}

The benefit of EL is that your IDE (hopefully) understands it, which in 
my opinion is much better than hardcoding obscure constant strings into 
OGNL expressions that fail silently :-)

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


Re: [S2] current URL in ValueStack?

Posted by Pierre Thibaudeau <pi...@gmail.com>.
Hello Jeromy,
I thought of your OGNL solution a few minutes ago, just before reading your
post.  I had been so adamant on finding the value explicitly within the
value stack, that I had momentarily forgotten that I had access to the
HttpServletRequest object.

But your statement about EL intrigues me.  How would you do that in EL?  Is
there some value available to EL in the request context?


2008/7/14 Jeromy Evans <je...@blueskyminds.com.au>:

> The HttpServletRequest includes the getRequestURI() method.  You can access
> that easily using EL but OGNL is a little obscure.
>
> The request object is available in the context map in a property called
> "com.opensymphony.xwork2.dispatcher.HttpServletRequest".
>
> <s:property
> value="%{#context['com.opensymphony.xwork2.dispatcher.HttpServletRequest'].requestURI}"/>
>

Re: [S2] current URL in ValueStack?

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Pierre Thibaudeau wrote:
> (Struts 2.1.2)
> In the JSP, is it possible through some OGNL expression to get hold of the
> URL of the current request?  I looked through the XML output obtained
> through debugging, but could not find anything.
>
> (I know I can get hold of the action and parameters; so there might a
> complicated way out...)
>   

The HttpServletRequest includes the getRequestURI() method.  You can 
access that easily using EL but OGNL is a little obscure.

The request object is available in the context map in a property called 
"com.opensymphony.xwork2.dispatcher.HttpServletRequest".

<s:property 
value="%{#context['com.opensymphony.xwork2.dispatcher.HttpServletRequest'].requestURI}"/>




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