一、异常处理
1.SpringMVC默认的异常处理流程
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb2050b48fafd30efedf438686a363a8.png)
2.异常情况演示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/daeb925ec64ca65e04d24d75df5d94ff.png)
3.SpringMVC提供的异常解决思路
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b510da107c3d628e5cd762eef8e054dd.png)
4.解决步骤
4.1编写异常类
public class SysException extends Exception {
private String message;
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public SysException(String message) {
this.message = message;
}
}
4.2自定义异常处理类
public class SysExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) {
SysException ex = null;
if (e instanceof SysException){
ex = (SysException)e;
}else {
ex = new SysException("系统正在维护....");
}
ModelAndView mv = new ModelAndView();
mv.addObject("errorMsg",ex.getMessage());
mv.setViewName("error");
return mv;
}
}
4.2.1注意点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9048ff9a33b2f820e27e5ba02b7ab670.png)
4.3 springmvc.xml中配置该异常处理器
<bean id="sysExceptionResolver" class="com.xpt.exception.SysExceptionResolver"/>
4.4 提供跳转异常界面
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ed2084f9d8219545f1b165963ee9e280.png)
4.5 测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bba9f2fdec2014fdefea93c5a04e053e.png)
二、拦截器
1.拦截器思路图解
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc9db756318518faa647c5837f556fa7.png)
2.拦截器入门实现代码
2.1编写拦截器类,实现HandlerInterceptor
2.1.1接口实现细节
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d9b62597b7363fc85edd128f0b3b5d13.png)
2.1.2 编写拦截器类
public class MyInterceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("MyInterceptor1执行了....该拦截器是前置拦截器");
return true;
}
}
2.2 springmvc.xml中配置拦截器
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/134bd520bd23eb27d211c8d0f9b5d350.png)
2.3测试效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e5add9d1c088ee2b50e6c7dfc8a0c039.png)
3.一个拦截器配置多个方法
3.1代码
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("前置拦截器:MyInterceptor1执行了....");
return true;
}
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("后置拦截器:MyInterceptor1执行了...");
}
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("最后拦截器:MyInterceptor1执行了...");
}
3.2效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9268d559ac6eb76abe71034b31e5b4e9.png)
4. 配置多个拦截器以及其执行顺序
4.1多个拦截器实现类
4.1.1拦截器1
public class MyInterceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("前置拦截器:MyInterceptor1执行了....111");
return true;
}
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("后置拦截器:MyInterceptor1执行了...111");
}
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("最后拦截器:MyInterceptor1执行了...111");
}
}
4.1.2拦截器2
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("前置拦截器:MyInterceptor2执行了....222");
return true;
}
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("后置拦截器:MyInterceptor2执行了...222");
}
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("最后拦截器:MyInterceptor2执行了...222");
}
}
4.2springmvc.xml配置
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/03ef2c2b23a8301fe7a0eb01a997f1a1.png)
4.3测试执行顺序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a3b0f2c20664c1dab2b58045c2d243be.png)