SpringMvc学习笔记(十)异常处理专题
遇见异常跳转到自己设置的页面,注解方式处理局部异常,监听Controller类的全局异常处理,springmvc配置统一的简单异常处理,监听所有的页面异常
源码获取github
1.项目结构
2.局部异常处理
ExceptionDemoController.java
package com.hs.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
/** * 经典异常处理_注解方式 */
@Controller
public class ExceptionDemoController {
@GetMapping("/ex01")
public String test01() {
int i = 10 / 0; //会报这个错误Request processing failed; nested exception is java.lang.ArithmeticException: / by zero
return "jsp/result";
}
/** * 注解方式,处理异常,程序遇见这个异常,就会执行注解里的内容, *作用范围只有本类有效!!! * @return */
//里面可以写多个异常,用,隔开,,,其实也可以总的写一个异常Exception,class
@ExceptionHandler(value = {ArithmeticException.class})
public ModelAndView exception01(ArithmeticException ex) {
ModelAndView mav = new ModelAndView();
mav.addObject("ex", ex);
mav.setViewName("jsp/error");
return mav;
}
}
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>${requestScope.message}</h2>
<a href="javascript:void(0)" id="debug">[DEBUG]</a>
<p id="error" style="display: none">
${requestScope.ex}
</p>
<script> window.onload = function () { document.getElementById("debug").onclick = function () { document.getElementById("error").style.display = "block"; } } </script>
</body>
</html>
3.@ControllerAdvice监听所有Controller类的异常
ExceptionAllController.java
package com.hs.web;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/** * ControllerAdvice这个表示监听只要有Controller的类的异常,同样如果自身类有注解监听,满足谁近就运行谁的原则 */
@ControllerAdvice
public class ExceptionAllController {
/** * 监听Controller类的异常方式一 * * @param ex * @return */
@ExceptionHandler(value = {Exception.class})
public ModelAndView exception01(Exception ex) {
ModelAndView mav = new ModelAndView();
mav.addObject("ex", ex);
mav.setViewName("jsp/error02");
return mav;
}
}
ExceptionDemoController.java把上面那个局部处理异常注释就行,
error02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
本页是全局的异常监听跳转页面,本次监听到的异常是
${requestScope.ex}
</body>
</html>
4.springmvc.xml配置简单处理异常
springmvc.xml加入
<!--6.统一的简单异常处理,监听所有的页面异常-->
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--出现异常,默认跳转的页面-->
<property name="defaultErrorView" value="jsp/error"/>
<!--类似于这个mav.addObject("ex", ex);-->
<property name="exceptionAttribute" value="ex"/>
<!--异常映射,若出现下列异常,就跳转相应的页面,不跳默认的-->
<property name="exceptionMappings">
<props>
<prop key="ArithmeticException">jsp/error</prop>
<prop key="ArrayIndexOutOfBoundsException">jsp/error</prop>
<prop key="RuntimeException">jsp/error</prop>
<prop key="Exception">jsp/error</prop>
</props>
</property>
</bean>
5.自定义处理异常
ExceptionAllController.java
/** * 监听Controller类的异常方式二自定义异常 * @param ex * @param model * @return */
@ExceptionHandler(value = {Exception.class})
public String exception02(Exception ex, Model model) {
//判断类的类型
if (ex instanceof ArrayIndexOutOfBoundsException) {
model.addAttribute("message", "数组越界异常");//给客户看的
model.addAttribute("ex", ex);
// return "jsp/error";
}
if (ex instanceof ArithmeticException) {
model.addAttribute("message", "运算异常"); //给客户看的
model.addAttribute("ex", ex);
// return "jsp/error";
}
//处理自定义异常
return "jsp/error";
}
/** * 监听Controller类的异常方式二自定义异常____返回json格式 * @param ex * @return */
@ExceptionHandler(value = {Exception.class})
@ResponseBody
public Map<String,Object> exception03(Exception ex) {
Map<String, Object> map = new HashMap<String,Object>();
//判断类的类型
if (ex instanceof ArrayIndexOutOfBoundsException) {
map.put("message", "数组越界异常");//给客户看的
map.put("ex", ex);
// return "jsp/error";
}
if (ex instanceof ArithmeticException) {
map.put("message", "运算异常"); //给客户看的
map.put("ex", ex);
// return "jsp/error";
}
//处理自定义异常
return map;
}
result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
没有遇见注解里的异常,成功跳转
</body>
</html>