Spring MVC异常处理的方式有以下几种
1.
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"/> <property name="exceptionAttribute" value="ex"/>
@Controller @RequestMapping("/first") public class FirstController { @RequestMapping("/firstRequest") public String firstRequest(){ //模拟异常 int result=5/0; return "index"; }
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>错误页面</title> </head> <body> 发生错误:${ex.message} </body> </html>
2.
<body> <form action="/second/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body>
<!--系统异常处理器--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"/> <property name="exceptionAttribute" value="ex"/> <!--定制异常--> <property name="exceptionMappings"> <props> <prop key="com.springmvc.exception.NameException">nameError</prop> <prop key="com.springmvc.exception.AgeException">ageError</prop> </props> </property> </bean>
@RequestMapping("/secondRequest") public String secondRequest(String userName,Integer userAge) throws NameException, AgeException { if(!userName.equals("admin")){ //手动创建一个Name异常 throw new NameException("名称错误!!!!!!!!"); } if(userAge>80){ //手动创建一个Age异常 throw new AgeException("年龄太大,不符合规定!!!!!!"); } return "index"; }
public class NameException extends Exception{ public NameException() { } public NameException(String message) { super(message); } }
public class AgeException extends Exception{ public AgeException() { } public AgeException(String message) { super(message); } }
3.
/** * 自定义异常处理器 */ public class MyHandlerException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView=new ModelAndView(); //如果发生异常,我们给他一个默认的异常处理页面 modelAndView.setViewName("error"); modelAndView.addObject("ex",ex); //如果发生Name异常,则跳转到Name异常页面 if(ex instanceof NameException){ modelAndView.setViewName("nameError"); } if(ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; } }
<!--将自定义异常处理器注册到Spring容器当中--> <bean class="com.springmvc.exception.MyHandlerException"/>
4.
@ExceptionHandler public ModelAndView exceptionHandler(Exception ex){ ModelAndView modelAndView=new ModelAndView(); //如果发生异常,我们给他一个默认的异常处理页面 modelAndView.setViewName("error"); modelAndView.addObject("ex",ex); //如果发生Name异常,则跳转到Name异常页面 if(ex instanceof NameException){ modelAndView.setViewName("nameError"); } if(ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; }
5.
/** * 要让这个类处理所有Controller的异常 */ @ControllerAdvice public class MyControllerAdvice { @ExceptionHandler public ModelAndView exceptionHandler(Exception ex){ ModelAndView modelAndView=new ModelAndView(); //如果发生异常,我们给他一个默认的异常处理页面 modelAndView.setViewName("error"); modelAndView.addObject("ex",ex); //如果发生Name异常,则跳转到Name异常页面 if(ex instanceof NameException){ modelAndView.setViewName("nameError"); } if(ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; } }