2017-09-24 41 views
1

如何在spring mvc中處理傳入的請求到錯誤的contextpath?Spring MVC:如何處理傳入的請求到錯誤的上下文路徑

我已經部署了Spring MVC中有contextPath中的應用作爲

http://exampledomain.com/mycontext

但是當我嘗試訪問的URL http://exampledomain.com/wrongcontext我得到錯誤的HTTP狀態404 -/wrongcontext/

我已經實現錯誤處理當使用正確的上下文路徑時,它對所有錯誤的url都能正常工作,但對錯誤的上下文路徑不起作用。

我想了解我們如何重定向所有傳入的請求到具體頁面,在生產環境中..

在此先感謝

回答

0

您的應用程序只能看到請求它的上下文/mycontext沒有什麼應用程序可以處理其他情況下的請求。

但是,您可以在根上下文/處部署應用程序,並在那裏實現錯誤處理程序。

退房這樣的回答:How to customize JBoss AS7 404 page

這個答案涉及到JBoss的,但同樣的想法適用於其他服務器。

0

我們可以在@Controller@ControllerAdvice中使用@ExceptionHandler來處理此類異常並決定要呈現的視圖頁面。

@ExceptionHandler({ HttpRequestMethodNotSupportedException.class, 
     HttpMediaTypeNotSupportedException.class, HttpMediaTypeNotAcceptableException.class, 
     MissingPathVariableException.class, MissingServletRequestParameterException.class, 
     ServletRequestBindingException.class,MethodArgumentNotValidException.class, MissingServletRequestPartException.class, 
     NoHandlerFoundException.class}) 
    public ModelAndView handleException(){ 
     return new ModelAndView("errorpage"); 
    }