Spring MVC 404错误页面

时间:2014-04-03 19:05:42

标签: java spring

我使用ControllerAdvice定义了全局异常处理程序,对于IOException,我返回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页面。

我很困惑为什么它不能处理异常处理程序。

1 个答案:

答案 0 :(得分:0)

在我从tomcat webapps删除项目并进行了全新部署后,它现在似乎工作正常。可能是tomcat热部署的一些问题。

因此,当我的控制器抛出404错误时,tomcat容器正在查看为其配置的错误页面并发送已配置的错误页面作为响应。