【问题标题】:Custom error page - get originally requested URL自定义错误页面 - 获取最初请求的 URL
【发布时间】:2013-08-17 23:04:01
【问题描述】:

问题

这是对昨天(未回答的)问题 (see here) 的后续处理,因为我正在尝试寻找替代方法。

我添加了基本的

    <error-page>  
            <error-code>404</error-code>  
            <location>/404search.jsf</location>  
    </error-page>

..给我的web.xml。我现在需要获取用户输入的 URL 以提交到我的搜索功能,但我只设法获取当前 URL(在本例中为 ...404search.jsf),而不是用户输入的实际查询。

尝试

  • HttpServletRequest.getRequestURL 返回http://www.website.com/foldername/404search.jsf
  • HttpServletRequest.getRequestURI 返回/foldername/404search.jsf
  • HttpServletRequest.getQueryString 什么都不返回

我希望它返回/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 运行搜索查询。

有什么建议吗?

【问题讨论】:

    标签: jsf servlets http-status-code-404


    【解决方案1】:

    &lt;error-page&gt; 隐藏在RequestDispatcher#forward() 电话的掩护下。原始请求的所有详细信息都可作为请求属性提供,这些属性由RequestDispatcher#FORWARD_XXX 常量标识的键作为键:

    作为初学者,您应该知道所有请求属性都可以通过implicit EL object#{requestScope} 在EL 中使用。

    所以,总而言之,这应该在视图中完成:

    <p>Unfortunately, the page you requested, #{requestScope['javax.servlet.forward.request_uri']} does not exist</p>
    

    如果需要,这应该在 bean 中执行:

    String forwardRequestURI = externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
    

    【讨论】:

    • 在我的自定义 404 页面上,#{requestScope['javax.servlet.forward.request_uri']} 不起作用(没有显示),但我发现 #{requestScope['javax.servlet.error.request_uri']} 完成了这项工作。
    • @Bobby:答案假设页面是由FacesServlet 通过*.jsf 映射转发的,但你是对的。
    【解决方案2】:

    您也许可以使用“referer”(拼写错误)请求标头获取该 URL。

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36

    Java 用法:HttpServletRequest - how to obtain the referring URL?

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多