Struts2的拦截器,Struts2的执行流程,图解+详解(底层代码)以及 自定义拦截器(配置和使用)
发布日期:2021-06-29 15:01:59
浏览次数:2
分类:技术文章
本文共 1414 字,大约阅读时间需要 4 分钟。
一、拦截器的概述
1、什么是拦截器
Interceptor:拦截器,起到的是拦截Action的作用
Filter:过滤器,从客户端向服务器发送的请求。
Interceptor:拦截器,拦截的是客户端对Action的访问。更加细粒度化的拦截。(拦截Action当中具体的方法)。Struts2框架核心的功能是依赖拦截器实现的。
2、Struts2的执行流程
官方Struts2执行流程图
官方图的原码详解 客户端向服务器发送一个Action的请求,执行核心过滤器(doFilter)方法。 在这个方法当中,调用executeAction()方法, 在这个方法内部调用的是disoatcher.serviceAction(); 在这个方法的内部创建了一个Action的代理, 最终执行的是Action的代理当中execute(); 在这个代理中执行execute()方法中调用ActionInvocation的invoke方法。 在这个方法的内部递归执行一组拦截器(完成部分功能), 如果没有下一个拦截器,就会执行目标Action, 根据Action返回的结果来进行页面的跳转。二、自定义拦截器:
1、搭建Struts2的环境
web.xml
2、编写拦截器的类
编写一个类实现interceptor接口或者继承AbstractInterceptor类。
(1)自定义拦截器一:继承AbstractInterceptor类
编写两个拦截器
/* * 自定义拦截器一 */public class InterceptorDemo1 extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub System.out.println("InterceptorDemo1执行了。。。。。"); String obj = invocation.invoke(); System.out.println("InterceptorDemo1执行结束了"); return obj; }}
/* * 自定义拦截器一 */public class InterceptorDemo2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub System.out.println("InterceptorDemo2执行了。。。。。"); String obj = invocation.invoke(); System.out.println("InterceptorDemo2执行结束了"); return obj; }}
3、对拦截器进行配置
(1)定义拦截器进行配置
引入拦截器(一旦引入自定义拦截器,以下默认拦截器栈的拦截器就不执行了,所以需要手动设置默认的拦截器) 测试 结果及执行顺序(2)定义一个拦截器栈的配置
转载地址:https://code100.blog.csdn.net/article/details/106957526 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月16日 00时28分14秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
神奇的麦考林
2019-04-29
centos7使用rpm安装mysql遇到的error: Failed dependencies: mysql-community-common(x86-64) 5.7.9 is needed
2019-04-29
spark报错:warn util.utils::service ‘sparkUI‘ can not bind on part 4040.Attempting port 4041.4042等错误
2019-04-29
Error getting authority: Error initializing authority: Could not connect: No such file or directory
2019-04-29
编程程软件测试思维方式:如何科学制定测试计划
2019-04-29
BLE蓝牙4.0串口调试助手
2019-04-29
树莓派WIFI设置
2019-04-29
用vcgencmd获取树莓派硬件状态数据
2019-04-29
IIS 多域名多张证书配置
2019-04-29
树莓派LINUX 截屏
2019-04-29
树莓派Raspberry Pi的嵌入式QT平台
2019-04-29
apache https
2019-04-29
Debian Jessie安装支持HTML5音视频的Chromium浏览器听百度音乐
2019-04-29
nanopi2 启动信息
2019-04-29
POS打印机驱动大全
2019-04-29
phpstudy https
2019-04-29
centos apache 最新版HTTPS配置
2019-04-29
树莓派添加中文语音合成功能
2019-04-29
kangle https设置
2019-04-29