How to get the real request URL in Struts with Tiles?
Asked Answered
E

5

13

When you're using Tiles with Struts and do...

request.getRequestURL()

...you get the URL to e.g. /WEB-INF/jsp/layout/newLayout.jsp instead of the real URL that was entered/clicked by the user, something like /context/action.do.

In newer Struts versions, 1.3.x and after, you can use the solution mentioned on javaranch and get the real URL using the request attribute ORIGINAL_URI_KEY.

But how to do this in Struts 1.2.x?

Entreaty answered 25/9, 2008 at 8:28 Comment(0)
F
17

I use this, which also works on Spring:

<% out.println(request.getAttribute("javax.servlet.forward.request_uri")); %>

If you also need the query string (contributed by matchew):

<% out.println(request.getAttribute("javax.servlet.forward.query_string")); %>
Figueroa answered 21/7, 2010 at 11:12 Comment(0)
H
2

When you query request.getRequestURL() from your view/jsp/tiles layer, it's already another rewritten request.

As Mwanji Ezana mentions, the most suitable way is to save it to separate property on the action execution phase. You may want to automate this process with the help of interceptors in Struts2.

Housebound answered 14/9, 2010 at 18:43 Comment(0)
C
1

I don't know if Struts 1.2.x has a similar Globals constant, but you could create your own in at least two ways:

  • get the original request URL in the Action and set it on the request, and call that from the JSP
  • use a Servlet Filter to do the same thing
Camise answered 25/9, 2008 at 20:10 Comment(0)
F
1

This works in Struts 1.2

private String getOriginalUri(HttpServletRequest request) {
    String targetUrl = request.getServletPath();
    if (request.getQueryString() != null) {
        targetUrl += "?" + request.getQueryString();
    }
    return targetUrl;
}
Furlough answered 1/10, 2008 at 11:4 Comment(1)
No, in the case given by the OP, it surely doesn't.Benzocaine
G
0

You just need to do this in your action:

    request.getAttribute("javax.servlet.forward.request_uri")
Gruber answered 14/7, 2020 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.