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?

zb226
  • 9,586
  • 6
  • 49
  • 79

5 Answers5

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")); %>
Community
  • 1
  • 1
digz6666
  • 1,798
  • 1
  • 27
  • 37
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.

adlerer
  • 1,010
  • 11
  • 14
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
Mwanji Ezana
  • 924
  • 8
  • 15
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;
}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
0

You just need to do this in your action:

    request.getAttribute("javax.servlet.forward.request_uri")