当前位置:网站首页>十、拦截器
十、拦截器
2022-07-26 04:27:00 【时间邮递员】
文章目录
1、拦截器的配置
SpringMVC中的拦截器用于拦截控制器方法的执行
SpringMVC中的拦截器需要实现HandlerInterceptor
package com.yzh.interceptors;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstInterceptor implements HandlerInterceptor {
//在控制器方法执行之前执行
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor --> preHandle");
return true;//返回false表示拦截,true表示放行
}
//在控制器方法执行之后执行
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor --> postHandle");
}
//在视图渲染完毕之后执行
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor --> afterCompletion");
}
}
SpringMVC的拦截器必须在SpringMVC的配置文件中进行配置
<!--配置拦截器-->
<mvc:interceptors>
<!--<bean class="com.yzh.interceptors.FirstInterceptor"></bean>-->
<!--<ref bean="firstInterceptor"></ref>--> 这种方式记得在拦截器类上加上Component注解
<!-- 以上两种配置方式都是对DispatcherServlet所处理的所有的请求进行拦截 -->
<mvc:interceptor>
<mvc:mapping path="/**"/> /**表示所有 <mvc:exclude-mapping path="/"/> <ref bean="firstInterceptor"></ref> </mvc:interceptor> <!-- 以上配置方式可以通过ref或bean标签设置拦截器,通过mvc:mapping设置需要拦截的请求,通过mvc:exclude-mapping设置需要排除的请求,即不需要拦截的请求 --> </mvc:interceptors> 2、拦截器的三个抽象方法
SpringMVC中的拦截器有三个抽象方法:
preHandle
控制器方法执行之前执行preHandle(),其boolean类型的返回值表示是否拦截或放行,返回true为放行,即调用控制器方法;返回false表示拦截,即不调用控制器方法
postHandle
控制器方法执行之后执行postHandle()
afterComplation
处理完视图和模型数据,渲染视图完毕之后执行afterComplation()
3、多个拦截器的执行顺序
a>若每个拦截器的preHandle()都返回true
此时多个拦截器的执行顺序和拦截器在SpringMVC的配置文件的配置顺序有关
preHandle()会按照配置的顺序执行,而postHandle()和afterComplation()会按照配置的反序执行
FirstInterceptor --> preHandle
SecondInterceptor --> preHandle
SecondInterceptor --> postHandle
FirstInterceptor --> postHandle
SecondInterceptor --> afterCompletion
FirstInterceptor --> afterCompletion
b>若某个拦截器的preHandle()返回了false
若某个拦截器的preHandle()返回false,那这个拦截器和它之前拦截器的preHandle()都会执行,postHandle()都不执行,返回false的拦截器之前的拦截器的afterComplation()会执行
边栏推荐
猜你喜欢

What are the consequences and problems of computer system restoration

性能和成本的综合架构:单元化架构

MySQL的优化分析及效率执行

Steam science education endows classroom teaching with creativity

生活相关——十年的职业历程(转)

Life related - ten years of career experience (turn)

Matlab drawing

Integrated architecture of performance and cost: modular architecture

Spark Structured Streaming HelloWorld

1. Mx6u-alpha development board (main frequency and clock configuration experiment)
随机推荐
华为再发“天才少年”全球召集令,有人曾放弃360万年薪加入
人脸数据库收集总结
Graph theory: topological sorting
Rotate array minimum number
低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!
Postman 导入curl 、导出成curl、导出成对应语言代码
I.MX6U-系统移植-6-uboot图形化配置
Acwing刷题
Build a maker Education Laboratory for teenagers
Web Test Method Encyclopedia
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
Use of rule engine drools
Integrated architecture of performance and cost: modular architecture
数据仓库
机器学习之信用卡欺诈检测
Postman imports curl, exports curl, and exports corresponding language codes
旋转数组最小数字
机器学习之桑基图(用于用户行为分析)
UE4 获取玩家控制权的两种方式
APISIX 在 API 和微服务领域的探索