springmvc异常

第一种方式。

bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
&lt;!&ndash;这里可以添加多个自定义异常&ndash;&gt;
<prop key="com.shankai.myexception.myerror">/jsp/error/myerror</prop>
</props>
</property>
&lt;!&ndash;默认的异常跳转页面&ndash;&gt;
<property name="defaultErrorView" value="/jsp/error/error"></property>
<property name="exceptionAttribute" value="ex"/>
</bean>


这里可以设置自定义异常和默认异常的跳转界面,exceptionAttribute中的value ex用于跳转后jsp页面的el表达式的获取。

第二种方式。

定义一个类实现HandlerExceptionResolver。

当一个异常发生时,必定会实现类中的重载的方法resolveException

//  全局异常处理
//所有发生的异常都会调用此方法
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        System.out.println("出现异常啦!!!!");
        return null;
    }
 

猜你喜欢

转载自www.cnblogs.com/shank/p/10274182.html