@ControllerAdvice
没有切点只有增强
1.全局统一异常处理类
1 package com.cn.globalexpction; 2 3 import org.springframework.ui.ModelMap; 4 import org.springframework.web.bind.annotation.ControllerAdvice; 5 import org.springframework.web.bind.annotation.ExceptionHandler; 6 import org.springframework.web.servlet.ModelAndView; 7 8 /** 9 * @Author hxy 10 * @Description 全局统一异常处理类 11 * @Date 2023/9/15 10:11 12 * @Version 1.0 13 */ 14 @ControllerAdvice//声明页面控制器增强类(处理Controller的所有异常) 15 public class GlobalExceptionHandle { 16 //@ExceptionHandler:声明该方法可以处理的异常类型 17 @ExceptionHandler({NameException.class}) 18 public ModelAndView doNameException(Exception e){ 19 System.out.println("-------doNameException--------"); 20 ModelAndView modelAndView = new ModelAndView(); 21 modelAndView.addObject("msg","用戶名错误异常"); 22 modelAndView.addObject("e",e); 23 modelAndView.setViewName("error"); 24 e.printStackTrace(); 25 return modelAndView; 26 } 27 28 @ExceptionHandler({PasswordException.class}) 29 public ModelAndView doPasswordException(Exception e){ 30 System.out.println("-------doPasswordException--------"); 31 ModelAndView modelAndView = new ModelAndView(); 32 modelAndView.addObject("msg","密码错误异常"); 33 modelAndView.addObject("e",e); 34 modelAndView.setViewName("error"); 35 e.printStackTrace(); 36 return modelAndView; 37 } 38 39 /**此方法用来处理任意异常(也就是上面两个方法不能处理的其他异常) 40 * @return {@link ModelAndView} 41 */ 42 @ExceptionHandler 43 public ModelAndView doException(Exception e){ 44 System.out.println("-----------doException----------"); 45 ModelAndView modelAndView = new ModelAndView(); 46 modelAndView.addObject("msg","系统异常"); 47 modelAndView.addObject("e",e); 48 modelAndView.setViewName("error"); 49 e.printStackTrace(); 50 return modelAndView; 51 } 52 }
2.异常页面控制器
1 package com.cn.globalexpction; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.ModelMap; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 /** 8 * @Author hxy 9 * @Description 异常页面控制器 10 * @Date 2023/9/15 9:42 11 * @Version 1.0 12 */ 13 @Controller 14 public class ExceptionController { 15 @RequestMapping("/exception.do") 16 public String exception(String name, String password, ModelMap modelMap)throws Exception{ 17 //用户名判断 18 if (!"binge".equals(name)){ 19 //抛出用户名错误异常 20 throw new NameException("用户名错误异常"); 21 } 22 if (!"666".equals(password)){ 23 //抛出密码错误异常 24 throw new PasswordException("密码错误异常"); 25 } 26 // int i=1/0; 27 modelMap.addAttribute("msg","用户名和密码正确"); 28 return "result"; 29 } 30 }
3.自定义异常
1 package com.cn.globalexpction; 2 3 /** 4 * @Author hxy 5 * @Description 自定义用户名异常 6 * @Date 2023/9/15 9:46 7 * @Version 1.0 8 */ 9 public class NameException extends Exception{ 10 public NameException(String message){ 11 super(message); 12 } 13 }
1 package com.cn.globalexpction; 2 3 /** 4 * @Author hxy 5 * @Description 自定义密码错误异常 6 * @Date 2023/9/15 9:48 7 * @Version 1.0 8 */ 9 public class PasswordException extends Exception{ 10 public PasswordException(String message){ 11 super(message); 12 } 13 }
标签:SpringMVC,import,springframework,处理,modelAndView,ModelAndView,异常,public From: https://www.cnblogs.com/hxysg/p/17742650.html