SpringMVC-异常解析器和拦截器
Posted 永旗狍子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC-异常解析器和拦截器相关的知识,希望对你有一定的参考价值。
一.异常解析器,统一处理
Controller中的每个Handler不再自己处理异常,而是直接throws所有异常。
package com.cos.qf.common;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author: qiuyongqi
* @time: 15:01 2021/5/22
* @description: 异常解析器:主体逻辑
* 执行时刻:当handler中抛出异常时,会执行:捕获异常,并可以跳到错误页面
*/
public class ExceptionRes implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
//创建一个ModelAndView
ModelAndView modelAndView = new ModelAndView();
System.out.println("异常解析器运行,处理全局问题");
//识别异常
if (e instanceof NullPointerException) {
modelAndView.setViewName("redirect:/error.jsp");
}else if (e instanceof NumberFormatException){
modelAndView.setViewName("redirect:/error.jsp");
}else {
modelAndView.setViewName("redirect:/error.jsp");
}
return modelAndView;
}
}
<!-- 声明异常解析器 -->
<bean class="com.baizhi.exception.resolver.MyExResolver"></bean>
二.拦截器
1.定义拦截器
执行顺序: preHandle--postHandle--afterCompletion
package com.cos.qf.web.interceptor;
import com.cos.qf.entity.Emp;
import com.cos.qf.servlet.impl.EmpServicesImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MyInterceptor implements HandlerInterceptor {
@Autowired
private EmpServicesImpl empServices;
/**
* @author: qiuyongqi
* @time: 11:12 2021/5/20
* @description: 进入执行xcontroller方法之前的拦截方法
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("执行controller之前");
HttpSession session = request.getSession();
Emp login = (Emp) session.getAttribute("login");
System.out.println(login);
if (login!=null) {
empServices.sayHi();
return true;//放行,后续的拦截器或handler就会执行
}
response.sendRedirect("/error.jsp");
return false;//中断请求
}
/**
* @author: qiuyongqi
* @time: 11:12 2021/5/20
* @description: 执行完controller方法给出响应之前执行的拦截
*/
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("执行controller方法后,的拦截");
}
/**
* @author: qiuyongqi
* @time: 11:13 2021/5/20
* @description: 在view视图渲染数据结束后执行,资源回收
*/
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("已经给视图渲染玩数据,在响应给用户前,回收资源");
}
}
2.配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/inter/test1"/>
<mvc:mapping path="/inter/test2"/>
<mvc:mapping path="/inter/test*"/> <!-- test开头 -->
<mvc:mapping path="/inter/**"/> <!-- /** 任意多级任意路径 -->
<mvc:exclude-mapping path="/inter/a/**"/> <!--不拦截此路径-->
<bean class="com.baizhi.interceptor.MyInter1"></bean> <!--拦截器类-->
</mvc:interceptor>
</mvc:interceptors>
以上是关于SpringMVC-异常解析器和拦截器的主要内容,如果未能解决你的问题,请参考以下文章
重温SSM框架系列9 - SpringMVC中的拦截器和异常处理
学习笔记——SpringMVC拦截器的两种装配方式;SpringMVC拦截器工作原理;SpringMVC中的异常处理器;SpringMVC工作原理