SpringMVC异常处理
1. 异常解决方法一
1.1 Controller层
@Controller @RequestMapping("/firsts") public class FirstController { @RequestMapping("/firstException") public String firstRequest(){ //模拟异常 int result=5/0; return "index"; } }
1.2 Spring-mvc.xml文件
<!--系统异常处理器--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"></property> <property name="exceptionAttribute" value="ex"></property> </bean>
1.3 页面
2.异常解决方法二
2.1 jsp页面
<body> <form method="post" action="/firsts/secondException"> 姓名:<input id="userName" name="userName" class="userName"/><span></span><br/> 年龄:<input id="userAge"name="userAge" class="userAge"/><br/> <input type="submit" class="submit"/> </form> </body>
2.2 Controller层
/*自动义异常处理器*/ @RequestMapping("/secondException") public String secondException(String userName,Integer userAge) throws NameException, AgeException { if (!userName.equals("admin")){ //手动创建一个Name异常 throw new NameException("名称错误"); } if(userAge>80){ //手动创建一个异常 throw new AgeException("年龄太大 "); } return "index"; }
2.3 Spring-mvc.xml文件
<!--系统异常处理器--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"></property> <property name="exceptionAttribute" value="ex"></property> <property name="exceptionMappings"> <props> <prop key="com.springmvc.exception.AgeException">ageerror</prop> <prop key="com.springmvc.exception.NameException">nameerror</prop> </props> </property> </bean>
2.4 编写年龄异常类
public class AgeException extends Exception{ public AgeException() { } public AgeException(String message) { super(message); } }
2.5 编写年龄异常类
public class NameException extends Exception{ public NameException() { } public NameException(String message) { super(message); } }
2.6 名称输错时
2.7 年龄输错时
3. 自动义解决异常
3.1 创建类
public class MyHanlerException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, 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; } }
3.2 创建异常类同上
3.3 spring-mvc.xml文件
<!--将自动义异常处理器注册到spring容器中--> <bean class="com.springmvc.exception.MyHanlerException"></bean>
3.4 名称输错时
3.5 年龄输错时
4. 局部注解处理异常
4.1 Controller层
/*局部注解处理*/ @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; }
4.2 名称输错时
4.3 年龄输错时
5. 全局异常处理
5.1 创建解决异常的类
/*要让这个类处理所有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; } }
5.2 名称输错时
5.3 年龄输错时