当前位置:网站首页>十、拦截器
十、拦截器
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()会执行
边栏推荐
- 华为再发“天才少年”全球召集令,有人曾放弃360万年薪加入
- Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
- Threadpooltaskexecutor and ThreadPoolExecutor
- TIA botu WinCC Pro controls the display and hiding of layers through scripts
- 理性认知教育机器人寓教于乐的辅助作用
- 旋转数组最小数字
- The auxiliary role of rational cognitive educational robot in teaching and entertainment
- 1. If function of Excel
- How to write abstract in English thesis?
- Segger embedded studio cannot find xxx.c or xxx.h file
猜你喜欢

Sangi diagram of machine learning (for user behavior analysis)

人脸数据库收集总结

1. Mx6u-alpha development board (main frequency and clock configuration experiment)

1. If function of Excel

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

Can literature | relationship research draw causal conclusions

当你尝试删除程序中所有烂代码时 | 每日趣闻

qt编译报错整理及Remote模块下载

FFmpeg 视频编码
![[SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup](/img/44/69c434fc9564078e11735881d9bf34.png)
[SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup
随机推荐
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
TIA botu WinCC Pro controls the display and hiding of layers through scripts
MATLAB绘图
How is the launch of ros2 different?
qt编译报错整理及Remote模块下载
Optimization analysis and efficiency execution of MySQL
Firewall command simple operation
SwiftUI一日速成
性能和成本的综合架构:单元化架构
吴恩达机器学习课后习题——线性回归
Recommendation | scholar's art and Tao: writing papers is a skill
SEGGER Embedded Studio找不到xxx.c或者xxx.h文件
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
p-范数(2-范数 即 欧几里得范数)
What are the consequences and problems of computer system restoration
Dynamic planning for stair climbing
VM virtual machine has no un bridged host network adapter, unable to restore the default configuration
How to download the supplementary literature?
数组排序3
吴恩达机器学习课后习题——逻辑回归