1.Interceptor介绍
拦截器(Intercepter):拦截器是struts2的核心,struts2的众多功能都是通过拦截器来实现的,与Filter非常类似,不管是概念还是里面的方法
要想实现struts2的拦截器,那么就要实现Interceptor接口
对于拦截器而言,分为两个阶段:
- 定义阶段
- 使用阶段
一定要先定义才能使用
拦截器的实现步骤:
- 编写实现interceptor接口的类
- 在struts.xml中的package元素下定义拦截器
- 在struts.xml的action元素下使用拦截器
虽然拦截器与Filter的配置方式不一样,但是原理是一样的
如果配置了拦截器,那么在服务器启动的时候就会实例化它并调用对应的init方法,这一点也与Filter一样
有一点不同的是Filter可以过滤所有内容,而拦截器只能拦截Action
整个struts2框架是靠拦截器串联起来的
struts2提供了拦截器栈的概念,用来将拦截器按需进行归类,方便引用,看起来使用了组合模式,因为一个interceptor-stack可以包含多个interceptor-ref也可以包含多个interceptor-stack
defaultStack拦截器栈中包含了struts2所必须用到的一些拦截器类
strust2还提供了默认拦截器机制,即如果没有在struts.xml的Action元素下显式的使用拦截器,那么该Action默认使用了defaultStack拦截器栈,否则的话需要显式的引用默认拦截器栈,并通常配置在所有拦截器之后【注:经本人测试存在defaultStack不放在所有拦截器之后的情况】
对于struts.xml而言分为三部分:
- bean
- result-type
- interceptor
拦截器本身是xwork提供的,按照之前的规律,struts2会对它进行一些封装,成为自己的,类似StrutsTypeConverter
拦截器示例:
前端页面:

LoginAction配置:

拦截器代码编写:

struts.xml配置:

结果页面:

测试:
在前端页面输入hello、world并进行提交,如下图所示:

提交后的结果如下:

控制台输出信息如下:

和Filter的doFilter很相似吧
此外拦截器也支持初始化参数配置,如下图所示:

然后在对应的拦截器中设置成员变量并提供set方法即可,这一点与Filter配置初始化参数有点不同,这里为了测试init与set方法的先后执行顺序在涉及到的方法中增加了打印语句,如下图所示:

服务器启动后控制台的输出信息如下:

可以看出set方法优于init方法先执行,这也是符合逻辑的,因为init方法中可能要用到对象参数
2. AbstractInterceptor介绍
因为Interceptor接口需要实现init、interceptor、destroy三个方法,而通常对于开发者而言只需要用到interceptor方法就行了,但是因为是接口所以必须去实现另外两个方法,这样就给开发者增加了麻烦,所以产生了AbstractInterceptor,类似于swing中的监听器适配器
它是一个抽象类,实现了Interceptor接口,并且空实现了init、destroy,所以我们在使用的时候只需要继承它就可以了,这样我们编写的拦截器就更加简洁了
3. 拦截器链配置演示
首先定义两个拦截器分别为:MyInterceptor、MyAbstractInterceptor:


然后配置struts.xml文件:

使用之前的登录页面进行测试,控制台输出结果如图所示:

从这里可以看出拦截器链和FilterChain的执行流程一样,所以之前对Filter掌握好的话,这里就很快能够理解了
另外在这里简单的介绍下拦截器方法所携带的参数ActionInvocation,通过它我们可以获得被该拦截器所拦截的Action的对象,以及我们可以为ActionInvocation安装PreResultListener类型的监听器,这个之后会
详细介绍
4. MethodFilterInterceptor介绍
MethodFilterInterceptor(方法过滤拦截器) 它继承了AbstractInterceptor,并且提供了一个抽象方法doInterceptor来要求我们实现,通过在struts.xml中配置该类的excludeMethods、includeMethods属性可以来决定到底对哪个方法进行拦截,这里要注意一点,(这里的拦截指拦截器中要执行一些逻辑,不拦截指直接放行,不执行拦截器中的逻辑,流程直接进入到Action)拦截器只要配置对了,那么该拦截器就会执行,至于执行效果就由拦截器来控制了
当一个方法名字既可以在includeMethods里获得,又可以从excludeMethods里获得,那么MethodFilterInterceptor认为该方法是要被拦截的即includeMethods优先级大于excludeMethods
当在Action中引用了MethodFilterInterceptor但没配置其子元素param时,例如下图所示:

那么MethodFilterInterceptor拦截器会当inlcude来执行
5. PreResultListener介绍
可以对拦截器中的actionInvocation参数安装监听器,该监听器的执行时机是:action执行完之后,在渲染之前
如果在这个时机想要进行操作的话那么可以使用该监听器,下图是安装监听器的代码:

当被拦截的Action执行完之后,就会执行该监听器的beforeReuslt方法,然后再执行拦截器上的后续代码
6. 拦截器实战
需求:用拦截器实现全系统登录验证功能
实现:
登录页面代码:

添加新闻页面代码:

登录显示结果代码:

新闻显示结果代码:

LoginAction代码:

AddNewsAction代码:

登录拦截器代码:

配置文件代码:

7. struts.xml一拆多介绍
struts.xml一拆多其实也是为了分门别类而已,防止一个xml中内容太过庞大,在主struts.xml中通过如下语句即可引入从struts.xml 文件:

网友评论