如何从通过 apache ErrorDocument 重定向到达的 Struts 2 Action 中获取原始 REQUEST_URI?
如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
request.getAttribute("javax.servlet.forward.request_uri")
request.getAttribute("javax.servlet.forward.request_uri")
使用:
在您的
httpd.conf
文件中。 然后使用 request.getAttribute("REDIRECT_URL"); 获取 jsp/servlet 中的变量。Use:
in your
httpd.conf
file. Then userequest.getAttribute("REDIRECT_URL");
to get the variable in your jsp/servlets.如果您不想错过查询字符串部分,那么更好:
If you don't wanna miss the query string part, this is better: