异常处理器

HandlerExceptionResolver 接口

1、SpringMVC 提供的一个处理控制器方法,执行过程中所出现的异常的接口

2、实现类

(1)DefaultHandlerExceptionResolver:默认异常处理器

(2)SimpleMappingExceptionResolver:自定义异常处理器

 

异常处理

1、基于配置

(1)springMVC 配置 xml

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <!--
                key表示处理器方法执行过程中可能出现的异常
                双标签中的值表示若出现指定异常时,设置一个新的视图名称,跳转到指定页面
             -->
            <prop key="java.lang.ArithmeticException">视图名</prop>
        </props>
    </property>
    <!-- exceptionAttribute属性:设置一个属性名,将出现的异常信息在请求域中进行共享 -->
    <property name="exceptionAttribute" value="message"></property>
</bean>

(2)html 显示错误页面

<!-- 获取错误信息 -->
<p th:text="${message}"></p>

2、基于注解的异常处理

@ControllerAdvice
//@ControllerAdvice是@Component的拓展
public class ExceptionController {
    //value为可能出现的异常
    @ExceptionHandler(value = {ArithmeticException.class, NullPointerException.class})
    //exception为出现的异常
    public String testException(Exception exception, ModelAndValue modelAndValue){
        modelAndValue.addObject("exception", exception);
        return "视图名(错误信息页面)";
    }
}
posted @   半条咸鱼  阅读(38)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示