当前位置:网站首页>12 异常处理器
12 异常处理器
2022-08-06 17:26:00 【猿 白】
12、异常处理器
12.1、基于配置的异常处理
SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolver
HandlerExceptionResolver接口的实现类有:DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver
SpringMVC提供了自定义的异常处理器SimpleMappingExceptionResolver,使用方式:
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- properties的键表示处理器方法执行过程中出现的异常 properties的值表示若出现指定异常时,设置一个新的视图名称,跳转到指定页面 -->
<prop key="java.lang.ArithmeticException">error</prop>
</props>
</property>
<!-- exceptionAttribute属性设置一个属性名,将出现的异常信息在请求域中进行共享 -->
<property name="exceptionAttribute" value="ex"></property>
</bean>
控制类:
System.out.println(1/0);
@Controller
public class TestController {
@RequestMapping("/test/hello")
public String test(){
System.out.println(1/0);
return "success";
}
}
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>error.html</h1>
<p th:text="${ex}"></p>
</body>
</html>

12.2、基于注解的异常处理
//@ControllerAdvice将当前类标识为异常处理的组件
@ControllerAdvice
public class ExceptionController {
//@ExceptionHandler用于设置所标识方法处理的异常
@ExceptionHandler(ArithmeticException.class)
//ex表示当前请求处理中出现的异常对象
public String handleArithmeticException(Exception ex, Model model){
model.addAttribute("ex", ex);
return "error";
}
}
边栏推荐
- `English` 2022/8/4
- office2019密钥永久有效 office2019密钥大全分享
- 键盘突然失灵怎么回事灯还亮着 键盘失灵但是灯是亮的
- 怎么解除谷歌浏览器的防火墙 如何关闭防火墙对chrome浏览器的拦截
- IDEA04:动态加载配置文件
- `Mathematics` Fundamental Theorem of Calculus
- win7输入法不见了怎么调出来 win7输入法图标不见了
- win7缺失dll文件一键修复 msvcr120.dll丢失怎样修复win7
- win7打印机共享设置(详细图文步骤)
- How to format computer to restore factory settings in win7
猜你喜欢

How to reset the power-on password of win7 without u disk

Windows 7 input method is missing how to adjust out Windows 7 input method icon is missing

win7开机0x0000007b修复不了 win7开机蓝屏0x0000007b

win7运行速度提高90% 怎么提高win7系统运行速度

win7网络出现感叹号一步搞定 win7网络黄色感叹号

开机f1修复电脑步骤 开机需要按F1才能进入系统

How to turn on the wireless screen mirroring function in win7

2022 华数杯 C 题 插层熔喷非织造材料的性能控制研究

电脑每次开机都要按f1怎么解决 电脑按f1才能启动什么原因

ESP8266-Arduino编程实例-磁簧开关传感器驱动
随机推荐
DMA/DMA2D concept
word2vec原理
FR9608SP SOP-8 28V、3A、340 KHz同步降压DC/DC转换器
win7升级win10数据会丢失吗 win7升级win10怎么备份数据
`数学` 一致连续性
`English` 2022/8/4
Talking about the Jmeter performance test process
win7升级win10数据会丢失吗 win7升win10怎么保留文件
Live-Charts在WPF中的使用
win7彻底重置清除所有软件 win7怎么重置系统
2022.8.5-----leetcode.623
2020最新office产品密钥永久激活码_office激活密钥永久key免费(附激活方法)
王学岗——钉钉视频会议实战,从零手写音视频会议项目
How to reset the power-on password of win7 without u disk
Win7旗舰版永久激活密钥 win7激活密钥序列号 win7激活神key大全
电容器选型指南-电子元器件选型指导系列
2022年华数杯C题资料汇总
win7资源管理器反复停止工作 win7资源管理器已停止工作不断重复
【TOML配置文件】配置文件我用TOML!人性化,少出错!
`数学` 夹逼定理