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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/132118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:39:51  来源:igfitidea点击:

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

javaservletsstrutstiles

提问by

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

当您在 Struts 中使用 Tiles 并执行...

request.getRequestURL()

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

...你得到的 URL 是例如/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 javaranchand get the real URL using the request attribute ORIGINAL_URI_KEY.

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

But how to do this in Struts 1.2.x?

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

回答by Mwanji Ezana

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
  • use a Servlet Filter to do the same thing
  • 在 Action 中获取原始请求 URL 并将其设置在请求上,然后从 JSP 调用它
  • 使用 Servlet 过滤器来做同样的事情

回答by Steve McLeod

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;
}

回答by digz6666

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

回答by adlerer

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 中的拦截器来自动化这个过程。