2013-06-27 41 views
0

我試圖獲取返回404狀態的URL以顯示在我的錯誤頁面上。在Spring MVC中404'd的訪問URL

我有這個在我的web.xml

<error-page> 
    <error-code>404</error-code> 
    <location>/errors/404.html</location> 
</error-page> 

而這個控制器設置

@Controller 
public class HTTPErrorController { 

@RequestMapping(value="/errors/404.html", method = RequestMethod.GET) 
    public String handle404(Model model, HttpServletRequest request) { 
     model.addAttribute("referer", request.getRequestURL()); 

     return "error404"; 
    } 
} 

這給了我的/error/404.html URL。

如果我嘗試從HttpServletRequest的訪問「引薦」頭:

request.getHeader("referer"); 

它總是空。

我想通過模型傳遞URL,以便我可以在我的錯誤頁面上顯示它。因此,例如,如果我在我的應用程序中使用「http://www.example.com/garbage」,並且404s將顯示「error404」視圖,並且我希望「http://www.example.com/garbage」在視圖上可用。

+0

託弗 - 它使用的是春天的版本? – Vinay

+0

我使用的是spring 3.1.1 –

回答

0

https://tomcat.apache.org/tomcat-7.0-doc/servletapi/constant-values.html

靜態最終java.lang.String中ERROR_REQUEST_URI 應當由容器調用自定義錯誤處理servlet或JSP頁面時可以設置的請求屬性的名稱。該屬性的值是java.lang.String類型。有關詳細信息,請參閱Servlet規範中的「錯誤處理」一章。 因爲: 的Servlet 3.0 另請參見: 常量字段值