如何在Struts with Tiles中获取真实的请求URL?

时间:2022-08-22 17:41:40

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

当你使用Tiles与Struts并做...

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.

...你得到的网址是/WEB-INF/jsp/layout/newLayout.jsp而不是用户输入/点击的真实URL,例如/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.

在较新的Struts版本,1.3.x及更高版本中,您可以使用javaranch上提到的解决方案,并使用请求属性ORIGINAL_URI_KEY获取真实URL。

But how to do this in Struts 1.2.x?

但是如何在Struts 1.2.x中做到这一点?

4 个解决方案

#1


14  

I use this, which also works on Spring:

我使用它,这也适用于Spring:

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

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

如果您还需要查询字符串(由matchew提供):

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

#2


2  

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

当您从view / jsp / tiles层查询request.getRequestURL()时,它已经是另一个重写的请求。

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.

正如Mwanji Ezana所提到的,最合适的方法是在动作执行阶段将其保存为单独的属性。您可能希望借助Struts2中的拦截器自动执行此过程。

#3


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:

我不知道Struts 1.2.x是否有类似的Globals常量,但你可以用至少两种方式创建自己的:

  • get the original request URL in the Action and set it on the request, and call that from the JSP
  • 获取Action中的原始请求URL并将其设置在请求上,并从JSP中调用它

  • use a Servlet Filter to do the same thing
  • 使用Servlet过滤器来做同样的事情

#4


1  

This works in Struts 1.2

这适用于Struts 1.2

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

#1


14  

I use this, which also works on Spring:

我使用它,这也适用于Spring:

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

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

如果您还需要查询字符串(由matchew提供):

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

#2


2  

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

当您从view / jsp / tiles层查询request.getRequestURL()时,它已经是另一个重写的请求。

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.

正如Mwanji Ezana所提到的,最合适的方法是在动作执行阶段将其保存为单独的属性。您可能希望借助Struts2中的拦截器自动执行此过程。

#3


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:

我不知道Struts 1.2.x是否有类似的Globals常量,但你可以用至少两种方式创建自己的:

  • get the original request URL in the Action and set it on the request, and call that from the JSP
  • 获取Action中的原始请求URL并将其设置在请求上,并从JSP中调用它

  • use a Servlet Filter to do the same thing
  • 使用Servlet过滤器来做同样的事情

#4


1  

This works in Struts 1.2

这适用于Struts 1.2

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