2014-04-03 112 views
0

我有使用ControllerAdvice,爲IOException異常定義的全局異常處理程序,我回到404Spring MVC的404錯誤頁面

@ResponseStatus(HttpStatus.NOT_FOUND) 
@ExceptionHandler(IOException.class) 
public void handleIOException(){ 
    logger.error("IOException"); 
    //returning 404 error code 
} 

web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/spring.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<error-page> 
    <error-code>404</error-code> 
    <location>/resources/404.jsp</location> 
</error-page> 

我故意拋出IOException在控制器中,但它不使用404錯誤頁面。如果我給一些隨機的URL,404頁面正在使用。

我很困惑爲什麼它不能與異常處理程序一起工作。

+0

你可以發佈你的web.xml文件的相關部分嗎? (spring mvc配置) –

+0

爲什麼你認爲IOException最終會顯示'/ resources/404.jsp'?處理程序返回時,響應將完成。 – Bart

+0

@彈出容器時會拋出404錯誤碼,servlet容器會查看web.xml中的錯誤頁面配置併發送配置頁面作爲響應。我在這裏錯過了什麼嗎? 更新後的web.xml內容。 – Pankaj

回答

0

從tomcat webapps中刪除項目並做了全新的部署之後,它現在好像工作的很好。可能與tomcat熱部署有些問題。

因此,當我的控制器拋出404錯誤時,tomcat容器正在查看爲其配置的錯誤頁面併發送配置的錯誤頁面作爲響應。