java中的异常分为两类,一种是运行时异常,一种是非运行时异常。在JavaSE中,运行时异常都是通过try{}catch{}捕获的,这种只能捕获显示的异常,通常项目上抛出的异常都是不可预见。那么我们能够有什么方法能够解决这种问题吗?当然有,SpringMVC中的异常处理机制就很好的做到了这一点。

SpringMVC中的异常处理一共有3种方式

  • (1)使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver; 
  • (2)实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器;
  • (3)使用@ExceptionHandler注解实现异常处理。

直接将SimpleMappingExceptionResolver类配置到SpringMVC配置文件中

        <!-- 
            只是对全局的Controller有效果
                所有的被RequestMapping注解所添加的方法中的异常才有效果
         -->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
            <!--    
                    key: 异常的类全称
                    value: ModelAndView中的viewName
                    表示当key指定的异常产生时 , 则请求转发至 value指向的视图    -->
                    <prop key="java.lang.Exception">errorPage</prop>
                </props>
            </property>
        </bean>

从配置文件上可以看出,如果发生了异常就跳转到名为errorPage的视图上。

完整的applicationContext.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
        <!-- 
            开启注解扫描
         -->
         <context:component-scan base-package="cn"></context:component-scan>
         <!-- 
             开启mvc注解扫描
          -->
        <mvc:annotation-driven/>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!-- 
            只是对全局的Controller有效果
                所有的被RequestMapping注解所添加的方法中的异常才有效果
         -->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
            <!--    
                    key: 异常的类全称
                    value: ModelAndView中的viewName
                    表示当key指定的异常产生时 , 则请求转发至 value指向的视图    -->
                    <prop key="java.lang.Exception">errorPage</prop>
                </props>
            </property>
        </bean>
    
        
</beans>
applicationContext.xml

相关文章: