eltehaem
eltehaem

Reputation:

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

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?

Upvotes: 13

Views: 21090

Answers (5)

Carlos Fernando
Carlos Fernando

Reputation: 76

You just need to do this in your action:

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

Upvotes: 0

digz6666
digz6666

Reputation: 1828

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")); %>

Upvotes: 17

adlerer
adlerer

Reputation: 1125

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.

Upvotes: 2

Steve McLeod
Steve McLeod

Reputation: 52458

This works in Struts 1.2

private String getOriginalUri(HttpServletRequest request) {
    String targetUrl = request.getServletPath();
    if (request.getQueryString() != null) {
        targetUrl += "?" + request.getQueryString();
    }
    return targetUrl;
}

Upvotes: 1

Mwanji Ezana
Mwanji Ezana

Reputation: 924

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

Upvotes: 1

Related Questions