如何从通过 apache ErrorDocument 重定向到达的 Struts 2 Action 中获取原始 REQUEST_URI?

发布于 2024-07-29 19:32:48 字数 1328 浏览 5 评论 0原文

如何从 Struts 2 Action 中访问 REQUEST_URI? 在 perl/php/ruby 中,可以通过 ENV["REQUEST_URI"] 等轻松获得。 在java中,似乎PageContext.getErrorData().getRequestURI()就是我正在寻找的,但遗憾的是,PageContext似乎没有在操作中定义,要么是因为ErrorDocument重定向使请求看起来不像错误,或者因为它是稍后定义的。

一个特定的例子

给定一个apache前端(mod_jk/ajp)struts 2应用程序在tomcat上通过apache中的ErrorDocument 404配置到达。 具有以下详细信息:

-- 原始请求 url(触发 404) --

http://server/totally/bogus/path

-- http.conf --

ErrorDocument 404 /struts2app/lookup.action

-- struts 操作 --

public String bogusUrlLookup() {
    HttpServletRequest request = ServletActionContext.getRequest();

    // contains /lookup.action as does request.getRequestURI();
    String url = RequestUtils.getServletPath(request);

    // PageContext is null, so I cannot reach ErrorData from it.
    log.info("pageContext="+ServletActionContext.getPageContext());

    // Not in the ENV
    // Map env = System.getenv();

    // Not in the ATTRIBUTES
    // request.getAttributeNames()

    // Not in HEADER
    // request.getHeaderNames()

    return ERROR;
}

同样,我需要的是字符串“/totally/bogus/path”,但在在上面的操作中,我能找到的唯一网址字符串是“/struts2app/lookup.action”。 我与ErrorDocument联系在一起,因为totally/bogus/path不太可能位于我的应用程序的命名空间内,因为apache提供其他非tomcat资源。

How can you access the REQUEST_URI from within a Struts 2 Action? In perl/php/ruby it is readily available via ENV["REQUEST_URI"] and the like. In java it seems that the PageContext.getErrorData().getRequestURI() is what I'm looking for, but sadly, the PageContext does not appear to be defined within the action, either because the ErrorDocument redirection makes the request not look like an error, or because it is defined later.

A particular example

Given an apache fronted (mod_jk/ajp) struts 2 app on tomcat reached via the ErrorDocument 404 configuration in apache. With the following details:

-- Original request url (which triggers the 404) --

http://server/totally/bogus/path

-- http.conf --

ErrorDocument 404 /struts2app/lookup.action

-- struts action --

public String bogusUrlLookup() {
    HttpServletRequest request = ServletActionContext.getRequest();

    // contains /lookup.action as does request.getRequestURI();
    String url = RequestUtils.getServletPath(request);

    // PageContext is null, so I cannot reach ErrorData from it.
    log.info("pageContext="+ServletActionContext.getPageContext());

    // Not in the ENV
    // Map env = System.getenv();

    // Not in the ATTRIBUTES
    // request.getAttributeNames()

    // Not in HEADER
    // request.getHeaderNames()

    return ERROR;
}

Again all I need is the string "/totally/bogus/path", but in the above action the only url string that I can find is "/struts2app/lookup.action". I'm tied to the ErrorDocument because the totally/bogus/path is not likely to be within the namespace of my application because apache serves other non-tomcat resources.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

長街聽風 2024-08-05 19:32:48

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

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

海风掠过北极光 2024-08-05 19:32:48
baseUri = (String)request.getAttribute("struts.request_uri");
baseUri = (String)request.getAttribute("struts.request_uri");
多彩岁月 2024-08-05 19:32:48

使用:

JkEnvVar REDIRECT_URL ""

在您的 httpd.conf 文件中。 然后使用 request.getAttribute("REDIRECT_URL"); 获取 jsp/servlet 中的变量。

Use:

JkEnvVar REDIRECT_URL ""

in your httpd.conf file. Then use request.getAttribute("REDIRECT_URL"); to get the variable in your jsp/servlets.

旧竹 2024-08-05 19:32:48

如果您不想错过查询字符串部分,那么更好:

final String referrer = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if(referrer != null) {
    final String query = (String) request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
    if(query != null && query.length() > 0) {
        url = referrer+ "?" + query;
    }
    else {
        url = referrer;
    }
// do something

}

If you don't wanna miss the query string part, this is better:

final String referrer = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if(referrer != null) {
    final String query = (String) request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
    if(query != null && query.length() > 0) {
        url = referrer+ "?" + query;
    }
    else {
        url = referrer;
    }
// do something

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文