1.SpringMVC提供了三种异常处理方式
简单异常处理器:使用springMVC内置的异常处理器处理 SimpleMappingExceptionResolver;
自定义异常处理器:实现HandlerExceptionResolver接口,自定义异常进行处理;
注解方式:使用 @ControllerAdvice + @ExceptionHandler 来处理。
第三种方式
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) public ModelAndView runtimeExceptionResolverMethod(RuntimeException ex) { System.out.println(ex); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/error1.html"); return modelAndView; } @ExceptionHandler(IOException.class) @ResponseBody public Result ioExceptionResolverMethod(IOException ex) { System.out.println(ex); Result result = new Result(); result.setCode(0); result.setMessage("错误"); result.setData("error"); return result; } }
标签:SpringMVC,处理,result,modelAndView,ModelAndView,异常,ex From: https://www.cnblogs.com/record-100/p/17588837.html