当前位置:网站首页>十、拦截器
十、拦截器
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()会执行
边栏推荐
- Under the high debt of Red Star Macalline, is it eyeing new energy?
- [binary tree] the longest interleaved path in a binary tree
- How is the launch of ros2 different?
- Solution: runtimeerror: expected object of scalar type int but got scalar type double
- makefile知识再整理(超详细)
- [C language foundation] 13 preprocessor
- The auxiliary role of rational cognitive educational robot in teaching and entertainment
- Recommendation | scholar's art and Tao: writing papers is a skill
- Design and implementation of smart campus applet based on cloud development
- egg-sequelize TS编写
猜你喜欢

SEGGER Embedded Studio找不到xxx.c或者xxx.h文件

Life related - ten years of career experience (turn)

I.MX6U-系统移植-6-uboot图形化配置

Postman 导入curl 、导出成curl、导出成对应语言代码

Comprehensive evaluation and decision-making method

Can literature | relationship research draw causal conclusions

荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因

UE4 多个角色控制权的切换

How to download the supplementary literature?

支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布
随机推荐
Use of rule engine drools
SEGGER Embedded Studio找不到xxx.c或者xxx.h文件
Postman 导入curl 、导出成curl、导出成对应语言代码
How to write the introduction and conclusion of an overview paper?
吴恩达机器学习课后习题——线性回归
荐书丨《教育心理学》:送给明日教师的一本书~
建设面向青少年的创客教育实验室
Steam science education endows classroom teaching with creativity
1. Mx6u-alpha development board (main frequency and clock configuration experiment)
Apisex's exploration in the field of API and microservices
egg-sequelize JS编写
Makefile knowledge rearrangement (super detailed)
低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!
Acwing刷题
机器学习之桑基图(用于用户行为分析)
Acwing brush questions
Acwing_ 12. Find a specific solution for the knapsack problem_ dp
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
Design and implementation of smart campus applet based on cloud development
p-范数(2-范数 即 欧几里得范数)