You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laurie Harper <la...@holoweb.net> on 2005/10/28 04:17:27 UTC

html:base and Tiles

I feel like I should know the answer to this one but I seem to be 
stumped... How do I get the actual request URI (or URL) in a JSP that's 
included via Tiles?

Both ${pageContext.request.requestURI} and 
${pageContext.request.requestURL} are pointing to the Tiles layout JSP, 
whether or not I include an <html:base/> tag in the <head> section in 
the layout.

To be exact, what I actually need is the full context-relative request 
path -- and I'm using URL prefix mapping, so for example the URL I 
browse to would be http://host/bin/some/path and the bit I need is 
'/some/path'.

Thanks,

L.


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


Re: html:base and Tiles

Posted by Laurie Harper <la...@holoweb.net>.
Craig McClanahan wrote:
> Ah ... there might be a timing issue here, depending on when you ask the
> question.
> 
> If you ask this question inside an Action.execute() method, the original URI
> is available via request.getRequestURI() ... doing a
> RequestDispatcher.include() call does not change those path properties.
> 
> However, Struts does a RequestDispatcher.forward() at the end of the request
> processing lifecycle (i.e. after your Action.execute() method returns). At
> that point, the container modifies the path properties in the request to
> reflect the page (or tile) that you forwarded to.

Yep, that's what I was figuring -- and I'm asking the question in the JSP.

> Now, if you're on a Servlet 2.4 container (Tomcat 5.x, for example), all is
> still not lost. There is a corresponding set of request attributes (
> javax.servlet.forward.request_uri and friends) that will be set to reflect
> the original request, so you can ask this question (for example) from within
> a tile's JSP page.

I'm on Tomcat 5.5.x, but unfortunately I'm not seeing that behaviour:

javax.servlet.include.request_uri	/pages/projects/Projects.jsp
javax.servlet.include.context_path	
javax.servlet.include.servlet_path	/pages/projects/Projects.jsp
javax.servlet.forward.request_uri	/pages/core/layout.jsp
javax.servlet.forward.context_path	
javax.servlet.forward.servlet_path	/pages/core/layout.jsp

So I can get the context-relative path of either the Tiles layout or the 
current JSP included into the layout, but still not the original 
request. FWIW I'm just using a ForwardAction pointing to a Tiles 
definition. If I forward directly to the JSP, I get the 'forward' 
attributes set to the same values as the 'include' attributes above.

> On a pre-2.4 container, this feature won't be available ... but it is
> something you could probably program into a custom RequestDispatcher
> implementation.

Yep, looks like I'll need to do something along those lines. I always 
thought that (as long as you didn't have a redirect along the way) the 
original request URL got preserved somewhere, but I guess it makes sense 
that forwards have an impact too.

L.


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


Re: html:base and Tiles

Posted by Craig McClanahan <cr...@apache.org>.
On 10/28/05, Laurie Harper <la...@holoweb.net> wrote:
>
> Craig McClanahan wrote:
> > On 10/27/05, Laurie Harper <la...@holoweb.net> wrote:
> >
> >>I feel like I should know the answer to this one but I seem to be
> >>stumped... How do I get the actual request URI (or URL) in a JSP that's
> >>included via Tiles?
> >>
> >>Both ${pageContext.request.requestURI} and
> >>${pageContext.request.requestURL} are pointing to the Tiles layout JSP,
> >>whether or not I include an <html:base/> tag in the <head> section in
> >>the layout.
> >>
> >>To be exact, what I actually need is the full context-relative request
> >>path -- and I'm using URL prefix mapping, so for example the URL I
> >>browse to would be http://host/bin/some/path and the bit I need is
> >>'/some/path'.
> >
> > If you're within the context of a RequestDispatcher.include() -- which
> is
> > what Tiles is doing under the covers -- the servlet container promises
> to
> > create some request attributes for you that contain useful information
> about
> > the included URI, rather than the original request URI. In particular,
> the
> > attribute named "javax.servlet.include.request_uri" will include what
> you
> > are looking for. Other attributes of interest:
> > * javax.servlet.include.context_path
> > * javax.servlet.include.servlet_path
> > * javax.servlet.include.path_info
> > * javax.servlet.include.query_string
> >
> > (See section SRV.8.3.1 of the Servlet Spec for more info.)
>
> Thanks for the reference; I'd seen those attributes but wasn't sure if
> they were standard or not. Unfortunately, I don't think they solve this
> particular problem; request_uri has the context-relative path of the JSP
> page included by Tiles, but that doesn't match the original request URL.


Ah ... there might be a timing issue here, depending on when you ask the
question.

If you ask this question inside an Action.execute() method, the original URI
is available via request.getRequestURI() ... doing a
RequestDispatcher.include() call does not change those path properties.

However, Struts does a RequestDispatcher.forward() at the end of the request
processing lifecycle (i.e. after your Action.execute() method returns). At
that point, the container modifies the path properties in the request to
reflect the page (or tile) that you forwarded to.

Now, if you're on a Servlet 2.4 container (Tomcat 5.x, for example), all is
still not lost. There is a corresponding set of request attributes (
javax.servlet.forward.request_uri and friends) that will be set to reflect
the original request, so you can ask this question (for example) from within
a tile's JSP page.

On a pre-2.4 container, this feature won't be available ... but it is
something you could probably program into a custom RequestDispatcher
implementation.

I think what's going on there is that Tiles is doing multiple, nested
> includes so I'm still at least one too many steps removed from the
> original request.
>
> L.


Craig


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

Re: html:base and Tiles

Posted by Laurie Harper <la...@holoweb.net>.
Craig McClanahan wrote:
> On 10/27/05, Laurie Harper <la...@holoweb.net> wrote:
> 
>>I feel like I should know the answer to this one but I seem to be
>>stumped... How do I get the actual request URI (or URL) in a JSP that's
>>included via Tiles?
>>
>>Both ${pageContext.request.requestURI} and
>>${pageContext.request.requestURL} are pointing to the Tiles layout JSP,
>>whether or not I include an <html:base/> tag in the <head> section in
>>the layout.
>>
>>To be exact, what I actually need is the full context-relative request
>>path -- and I'm using URL prefix mapping, so for example the URL I
>>browse to would be http://host/bin/some/path and the bit I need is
>>'/some/path'.
> 
> If you're within the context of a RequestDispatcher.include() -- which is
> what Tiles is doing under the covers -- the servlet container promises to
> create some request attributes for you that contain useful information about
> the included URI, rather than the original request URI. In particular, the
> attribute named "javax.servlet.include.request_uri" will include what you
> are looking for. Other attributes of interest:
> * javax.servlet.include.context_path
> * javax.servlet.include.servlet_path
> * javax.servlet.include.path_info
> * javax.servlet.include.query_string
> 
> (See section SRV.8.3.1 of the Servlet Spec for more info.)

Thanks for the reference; I'd seen those attributes but wasn't sure if 
they were standard or not. Unfortunately, I don't think they solve this 
particular problem; request_uri has the context-relative path of the JSP 
page included by Tiles, but that doesn't match the original request URL.

I think what's going on there is that Tiles is doing multiple, nested 
includes so I'm still at least one too many steps removed from the 
original request.

L.


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


Re: html:base and Tiles

Posted by Craig McClanahan <cr...@apache.org>.
On 10/27/05, Laurie Harper <la...@holoweb.net> wrote:
>
> I feel like I should know the answer to this one but I seem to be
> stumped... How do I get the actual request URI (or URL) in a JSP that's
> included via Tiles?
>
> Both ${pageContext.request.requestURI} and
> ${pageContext.request.requestURL} are pointing to the Tiles layout JSP,
> whether or not I include an <html:base/> tag in the <head> section in
> the layout.
>
> To be exact, what I actually need is the full context-relative request
> path -- and I'm using URL prefix mapping, so for example the URL I
> browse to would be http://host/bin/some/path and the bit I need is
> '/some/path'.


If you're within the context of a RequestDispatcher.include() -- which is
what Tiles is doing under the covers -- the servlet container promises to
create some request attributes for you that contain useful information about
the included URI, rather than the original request URI. In particular, the
attribute named "javax.servlet.include.request_uri" will include what you
are looking for. Other attributes of interest:
* javax.servlet.include.context_path
* javax.servlet.include.servlet_path
* javax.servlet.include.path_info
* javax.servlet.include.query_string

(See section SRV.8.3.1 of the Servlet Spec for more info.)

Craig


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