SpringMVC 异常处理

方式一

方式一,在配置文件中,如果没有用<mvc:annotation-driven/>配置方式,而是手动配置

RequestMappingHandlerMapping和RequestMappingHandlerAdapter,那么异常是拦截不到的。

原因是,<mvc:annotation-driven/>的简易配置方式,不止帮我们注册了上面的两个类,还有一些其他类

在手动配置的时候,如果没有注册ExceptionHandlerExceptionResolver这个类,那么异常拦截不到。

@ExceptionHandler(RuntimeException.class)
    public String runtimeExceptionHandler(RuntimeException ex){
        System.out.println("exception Handler");
        return "redirect:error.jsp";
    }

方式二

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.RuntimeException">redirect:error.jsp</prop>
            </props>
        </property>
    </bean>

  

原文地址:https://www.cnblogs.com/yanqin/p/5828376.html