目录
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- public class MyController {
- @RequestMapping("/t1")
- public String t1(){
- String str = null;
- //str.length();//空指针异常
- // int flag = 1/0; //数学逻辑异常
- int[] arr = new int[1];
- arr[2] = 10; //越界异常
- return "index";
- }
-
- /**
- * 异常处理方法
- * @param ex
- * @param model
- * @return
- */
- //添加@ExceptionHandler,表示该方法是处理异常的方法,属性为处理的异常类(这里设置为空指针异常和数学逻辑异常)
- @ExceptionHandler({java.lang.NullPointerException.class,java.lang.ArithmeticException.class})
- public String exceptionHandle1(Exception ex,Model model){
- //向模型添加异常对象
- model.addAttribute("msg",ex);
- //跳转到异常页面
- return "error";
- }
-
- //方法一不能处理的异常交给方法二处理,即处理除了空指针异常和数学逻辑异常以外的异常
- @ExceptionHandler(java.lang.Exception.class)
- public String exceptionHandle2(Exception ex,Model model){
- //向模型添加异常对象
- model.addAttribute("msg",ex);
- //跳转到异常页面
- return "error2";
- }
-
- }
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>出错了!</title>
- </head>
- <body>
- <h3>ERROR 发生异常!${msg}</h3>
- </body>
- </html>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>出错了!</title>
- </head>
- <body>
- <h3>ERROR2 发生严重异常!${msg}</h3>
- </body>
- </html>
- package com.first.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- public class MyController2 {
- @RequestMapping("/t2")
- public String t2(){
- int[] arr = new int[1];
- arr[2] = 10;
- return "index";
- }
- }
- package com.first.controller;
-
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
-
- //全局异常处理器类,需要添加@ControllerAdvice
- @ControllerAdvice
- public class GlobalExceptionHandler {
- /**
- * 异常处理方法
- *
- * @param ex 异常对象
- * @param model 模型对象
- * @return
- */
- // 添加@ExceptionHandler,表示该方法是处理异常的方法,属性为处理的异常类
- @ExceptionHandler({java.lang.NullPointerException.class, java.lang.ArithmeticException.class})
- public String exceptionHandle1(Exception ex, Model model) {
- // 向模型中添加异常对象
- model.addAttribute("msg", ex);
- // 跳转到异常页面
- return "error";
- }
-
- // 方法一不能处理的异常交给方法二处理
- @ExceptionHandler(java.lang.Exception.class)
- public String exceptionHandle2(Exception ex, Model model) {
- model.addAttribute("msg", ex);
- return "error2";
- }
- }
自定义异常处理器需要实现HandLerExceptionResolver。
- package com.first.controller;
-
- import org.springframework.stereotype.Component;
- import org.springframework.web.servlet.HandlerExceptionResolver;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- // 自定义异常处理器实现HandlerExceptionResolver接口,并放入Spring容器中
- @Component
- public class MyExceptionHandler implements HandlerExceptionResolver {
- @Override
- public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
- ModelAndView modelAndView = new ModelAndView();
- if(e instanceof NullPointerException){
- //跳转
- modelAndView.setViewName("error");
- }else{
- modelAndView.setViewName("error2");
- }
- //向modelAndView中添加数据
- modelAndView.addObject("msg",e);
- return modelAndView;
- }
- }
访问 http://localhost:8080/t2 和 http://localhost:8080/t1 试试。