2013-03-15 42 views
0

我正在研究一個有幾個類的彈簧項目。現在該項目中的代碼不符合標準,因爲在任何類中都沒有定義try catch塊。 我在我的web.xml如何在春季處理泛型異常

<error-page> 
    <exception-code>404</exception-type> 
    <location>/error.do</location> 
</error-page> 

以下映射/error.do映射到從控制器,其中其前端的一般性錯誤視圖。現在,是否有可能獲得我的異常的堆棧跟蹤。我唯一的要求是在錯誤處理程序中獲取異常詳細信息,例如 - 異常源自類名,方法名和異常消息,我覺得它們可以在堆棧跟蹤中使用。 任何想法如何處理這個問題陳述?

+0

以上定義了「404未找到」錯誤的錯誤頁面。當收到對未知資源的請求時,404發生。當Spring控制器拋出異常時不行。 – 2013-03-15 11:46:33

+0

請參閱http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-exceptionhandlers – 2013-03-15 12:07:04

+0

但即使當存在通用異常時捕捉,它去這個控制器。這不僅僅是處理404錯誤。 – 2013-03-15 12:20:44

回答

0

是的,你可以做到這一點Spring提供@ExceptionHandler annotation.So在控制器中寫一個方法:

@ExceptionHandler(YourException.class) 
public ModelAndView handleYourException(YourException ex, HttpServletRequest request) { 
    mav = new ModelAndView(); 
    mav.addObject("exception", exception); 
    mav.setViewName("YourException Page"); 
    return mav; 
} 

希望你有我的觀點。