当前位置:网站首页>十一、异常处理器
十一、异常处理器
2022-07-26 04:27:00 【时间邮递员】
1、基于配置的异常处理
SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolver
HandlerExceptionResolver接口的实现类有:DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver
SpringMVC提供了自定义的异常处理器SimpleMappingExceptionResolver,使用方式:
<!--异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!--
prop的键表示处理器方法执行过程中出现的异常
prop的值表示若出现指定异常时,设置一个新的视图名称,跳转到指定页面
-->
<prop key="java.lang.ArithmeticException">error</prop>
</props>
</property>
<!--
exceptionAttribute:设置一个属性名,将出现的异常信息在请求域中进行共享
-->
<property name="exceptionAttribute" value="ex"></property>
</bean>
2、基于注解的异常处理
//@ControllerAdvice将当前类标识为异常处理的组件
@ControllerAdvice
public class ExceptionController {
//@ExceptionHandler用于设置所标识方法处理的异常
@ExceptionHandler(ArithmeticException.class)
//ex表示当前请求处理中出现的异常对象
public String handleArithmeticException(Exception ex, Model model){
model.addAttribute("ex", ex);
return "error";
}
}
边栏推荐
- How to write the introduction and conclusion of an overview paper?
- Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
- 1. Mx6u-alpha development board (GPIO interrupt experiment)
- Credit card fraud detection based on machine learning
- p-范数(2-范数 即 欧几里得范数)
- Compiled by egg serialize TS
- How to choose the key words of the thesis?
- The auxiliary role of rational cognitive educational robot in teaching and entertainment
- Low cost, fast and efficient construction of digital collection app and H5 system, professional development of scallop technology is more assured!
- Leetcode:1184. Distance between bus stops -- simple
猜你喜欢

支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布

Keil V5 installation and use

How engineers treat open source -- the heartfelt words of an old engineer

Li Kou daily question - day 42 -661. Picture smoother

1. Excel的IF函数

Soft simulation rasterization renderer

华为高层谈 35 岁危机,程序员如何破年龄之忧?

I.MX6U-ALPHA开发板(主频和时钟配置实验)

人脸数据库收集总结

UE4 靠近物体时显示文字,远离时文字消失
随机推荐
1. Mx6u-alpha development board (GPIO interrupt experiment)
Li Kou daily question - day 42 -661. Picture smoother
当你尝试删除程序中所有烂代码时 | 每日趣闻
Makefile knowledge rearrangement (super detailed)
View and modify the number of database connections
How to choose the key words of the thesis?
I.MX6U-系统移植-6-uboot图形化配置
Wu Enda's machine learning after class exercises - logical regression
华为高层谈 35 岁危机,程序员如何破年龄之忧?
Credit card fraud detection based on machine learning
How engineers treat open source -- the heartfelt words of an old engineer
Getting started with mongodb Basics
egg-ts-sequelize-CLI
旋转数组最小数字
1. Mx6u system migration-6-uboot graphical configuration
Design and implementation of smart campus applet based on cloud development
雅迪高端之后开始变慢
egg-sequelize TS编写
综合评价与决策方法
Keil v5安装和使用