ringMVC——SpringMVC其他处理异常类(七)
1.ExceptionHandlerf方法只能捕捉当前页面的
@ExceptionHandler(Exception.class) public ModelAndView getThrows(Exception e){ System.out.println(e.getMessage()); ModelAndView modelAndView = new ModelAndView("error"); modelAndView.addObject("error",e.getMessage()); return modelAndView; }
[email protected]捕捉不同页面的异常
@org.springframework.web.bind.annotation.ControllerAdvice public class ControllerAdvice { @ExceptionHandler public ModelAndView getError(Exception e){ ModelAndView error = new ModelAndView("error"); error.addObject("error",e); return error; } }
3.自定义错误界面
方法1.新建其他的类添加@ResponseStatus
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "自定义了个错误提示") public class ResponseSatus extends Exception { }
方法2.直接在方法上添加
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "自定义了个错误提示2") @RequestMapping(value = "getError2") public String error(){ return "error"; }
4.其他异常类DefaultHandlerExceptionResolver
5.其他异常类配置来实现
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!--指定request域存放的错误信息的ex信息值--> <property name="exceptionAttribute" value="ex" /> <property name="exceptionMappings"> <props> <!--异常的类型 --> <prop key="java.lang.Exception"> <!--异常跳转的页面--> error </prop> </props> </property> </bean>