自定义错误页面-获取最初请求的网址 [英] Custom error page - get originally requested URL
问题描述
问题
这是对昨天(未回答)问题的后续操作(请参阅此处 ),因为我试图找到另一种方法.
This is a follow-up to yesterday's (unanswered) question (see here) as I try to find an alternative approach.
我添加了基本的
<error-page>
<error-code>404</error-code>
<location>/404search.jsf</location>
</error-page>
..到我的web.xml
.现在,我需要获取用户输入的URL才能提交给我的搜索功能,但是我只能设法获取当前URL(在这种情况下为... 404search.jsf
),而不是用户输入的实际查询.
..to my web.xml
. I now need to get the URL the user entered to submit to my search function, but I only manage to get the current URL (in this case, ...404search.jsf
) instead of the actual query the user entered.
尝试
-
HttpServletRequest.getRequestURL
返回http://www.website.com/foldername/404search.jsf
-
HttpServletRequest.getRequestURI
返回/foldername/404search.jsf
-
HttpServletRequest.getQueryString
完全不返回任何内容
HttpServletRequest.getRequestURL
returnshttp://www.website.com/foldername/404search.jsf
HttpServletRequest.getRequestURI
returns/foldername/404search.jsf
HttpServletRequest.getQueryString
returns nothing at all
我希望它返回/foldername/wrong-url-the-user-entered#anchor-if-there-is-any
详细信息...
想法是获取用户输入的URL(例如www.site.com/product/99999-product-that-cant-be-found
或www.site.com/faq/support-question-that-doesnt-exist
),对其进行正则表达式以删除连字符,并使用99999 product that cant be found
或support question that doesnt exist
运行搜索查询.
The idea is to get the URL the user entered (such as www.site.com/product/99999-product-that-cant-be-found
or www.site.com/faq/support-question-that-doesnt-exist
), REGEX it to remove the hyphens and run a search query with 99999 product that cant be found
or support question that doesnt exist
.
有什么建议吗?
推荐答案
<error-page>
位于 RequestDispatcher#FORWARD_XXX
常量:
The <error-page>
is under the covers served by a RequestDispatcher#forward()
call. All details of the original request are available as request attribues which are keyed by the keys as identified by RequestDispatcher#FORWARD_XXX
constants:
-
FORWARD_CONTEXT_PATH
: -
FORWARD_PATH_INFO
: -
FORWARD_QUERY_STRING
: -
FORWARD_REQUEST_URI
: -
FORWARD_SERVLET_PATH
:
FORWARD_CONTEXT_PATH
:"javax.servlet.forward.context_path"
FORWARD_PATH_INFO
:"javax.servlet.forward.path_info"
FORWARD_QUERY_STRING
:"javax.servlet.forward.query_string"
FORWARD_REQUEST_URI
:"javax.servlet.forward.request_uri"
FORWARD_SERVLET_PATH
:"javax.servlet.forward.servlet_path"
作为初学者,您应该知道所有请求属性都可以通过隐式EL对象 #{requestScope}
.
You as starter should know that all request attributes are in EL available via the implicit EL object #{requestScope}
.
总而言之,这应该在视图中完成:
So, all with all, this should do in the view:
<p>Unfortunately, the page you requested, #{requestScope['javax.servlet.forward.request_uri']} does not exist</p>
等效地,如果需要的话,这应该在bean中完成:
And equivalently, this should do in the bean, if necessary:
String forwardRequestURI = externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
这篇关于自定义错误页面-获取最初请求的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!