SpringMVC异常处理
1、异常解决方法1
(1)ErrorController
@Controller @RequestMapping("/error") public class ErrorController { @RequestMapping("/firstRequest") public String firstRequest(){ //模拟异常 int result=5/0; return "index"; } }
(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>
(3)效果展示
2 、异常解决方法2
(当空指针错误走error,名称错误走nameError,年龄错误走ageError)
(1)login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
(2)nameError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(3)ageError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(4)ErrorController
@RequestMapping("/secondRequest") public String secondRequest(String userName,Integer userAge) throws NameException, AgeException { if (!userName.equals("admin")){ throw new NameException("用户名错误!"); } if (userAge>99){ throw new AgeException("年龄不符合规定!"); } return "success"; }
(5)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.NameException">nameError</prop> <prop key="com.springmvc.exception.AgeException">ageError</prop> </props> </property> </bean>
(6)效果展示
3、自定义异常解决方法
(1)NameException
package com.springmvc.exception;
public class NameException extends Exception{
public NameException() {
}
public NameException(String message) {
super(message);
}
}
(2)AgeException
package com.springmvc.exception;
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
(3)login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
(4)nameError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(5)ageError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(6)MyHandlerException
/** * 自定义异常处理器 */ 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"); } //如果发生age异常,则跳转到age异常页面 if (ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; } }
(7)spring-mvc.xml
<!--将自定义异常处理器注册到Spring容器--> <bean class="com.springmvc.exception.MyHandlerException"/>
(8)效果展示
4、局部异常解决方法
(1)NameException
package com.springmvc.exception;
public class NameException extends Exception{
public NameException() {
}
public NameException(String message) {
super(message);
}
}
(2)AgeException
package com.springmvc.exception;
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
(3)login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
(4)nameError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(5)ageError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(6)ErrorController
@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"); } //如果发生age异常,则跳转到age异常页面 if (ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; }
(7)效果展示
5、全局异常解决方法
(1)NameException
package com.springmvc.exception;
public class NameException extends Exception{
public NameException() {
}
public NameException(String message) {
super(message);
}
}
(2)AgeException
package com.springmvc.exception;
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
(3)login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
(4)nameError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(5)ageError.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
(6)ExceptionControllerAdvice
/** * 全局注解处理 */ @ControllerAdvice public class ExceptionControllerAdvice { @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"); } //如果发生age异常,则跳转到age异常页面 if (ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; } }
(7)效果展示