- 浏览: 1020131 次
文章分类
最新评论
-
cjh_android:
我想你是对的,至少对于现实是对的,不过对于技术岗位竞争越来越激 ...
程序员的思考--终于确定了自己的技术发展方向 -
dongbiying:
现在情况如何 。。
创业,不能兼职 -
jackyrong:
ie 9 下时,老显示关闭窗口提示,有办法去掉么,就是关闭掉那 ...
jquery右下角弹窗效果 -
lmaxjj:
加点班如果都称得上是累,只能说明还没体会到真正的生活。。
IT男的进化论 -
馨雨轩:
IT男要进化需要先穿越
IT男的进化论
今天看了看拦截器,不由得想对拦截器做个描述,在我们进行项目拦截器编写的时候,让我们先把前期的配置给搭建好。
首先我们需要在web.xml中配置信息如下
<filter>
<filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.summer.action.server</param-value> </init-param> </filter> // org.apache.struts2.dispatcher.FilterDispatcher这个已经过时了、 // org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecute替代以上 // <filter> // <filter-name>struts2</filter-name> // <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> // </filter> <struts> <package name="methodFilter" extends="struts-default"> <interceptors> <interceptor name="myInterceptor11" class="com.sharme.interceptor.MyInterceptor11"> <param name="hello">world</param> </interceptor> <!--<param name="hello">world</param>表示为拦截器增加一个名为hello值为world的属性--> <!--但是这里也只是为hello赋了初值为world,如若在下面的<action>中配置使用该拦截器的时候--> <!--增加了<param name="hello">welcome</param>,那么则最后拦截器累中的hello属性值即为welcome--> <interceptor name="myInterceptor21" class="com.sharme.interceptor.MyInterceptor21"/> </interceptors> <action name="methodFilter" class="com.sharme.action.MethodFilterAction" method="test"> <result name="success">/methodFilteResult.jsp</result> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myInterceptor11"/> </action> </struts>
以上是对于 "方法过滤拦截器" 的使用:
默认的情况下,拦截器会拦截Action中的所有的方法,这里不包括setter或getter方法,这时就可以使用方法过滤拦截器指定的方法,这是一种更加
细化的拦截器的配置方式,它可以细化到拦截具体的某个方法,而不是拦截某个Action,因为拦截Action是一种粗粒度的实现方式,使用includeMethods
指明拦截器所要拦截的方法。使用excludeMethods指明拦截去不再拦截的方法,这里excludeMethods和includeMethods是在MethodFilterInterceptor
类中定义的成员变量,而且只要includeMethods进来的方法就一定会被拦截,而不管是否已经把它excludeMethods在外了
也就是说includeMethods的优先级要高于excludeMethods
也可以使用<param name="includeMethods"/>在上面定义拦截器的时候指定全局性的过滤的方法
区别:对方法的过滤有全局性和局部性的区分。而当发生冲突时,就依照就近原则,以局部性的配置为准,所谓的发生冲突,指的是类似于
全局中有一个includeMethods配置,而局部中也有一个includeMethods配置,另外,还有一种情况,假设全局性过滤定义为<param name="includeMethods">
test</param>而在局部性过滤中定义为<param name="excludeMethods">test</param>将生效,即拦截Action中的test()方法,这个时候全局中配置的拦截
局部中配置的是不拦截,二者并没有发生冲突,所有仍是以includeMethods优先级高,可以认为在局部的配置中,已经隐含的把<param name="includeMethods">
test</param>继承过来了
实现Interceptor接口的自定义拦截器MyInterceptor.java
public class MyInterceptor implements Interceptor{
//这里属性名要与struts.xml配置的<param name=""/>中的name值相同 //然后struts2会自动将struts.xml中配置的world值赋值到这里的hello属性中 //前提是要提供setter和getter方法,只要符合JavaBean的要求即可 private String hello; public String getHello(){return hello;} public void setHello(String hello){this.hello=hello;} //initialization method public void init(){ System.out.println("----MyInterceptor initialization method invoked---"); } //destory method public void destory(){ System.out.println("----MyInterceptor destory method invoked-----"); } public String interceptor(ActionInvocation invocation)throws Exception{ System.out.println("----MyInterceptor invoked begin"); //调用invoke()方法 //还有下一个拦截器的话,执行下一个拦截器 //如何没有下一个拦截器的话,就执行Action中的方法 String result=invocation.invoke(); System.out.println("--MyInterceptor invoked finish--------"); return result; } }
------下面来谈谈Struts2的拦截器------------
struts2中的拦截器,实际上就是用来拦截Action的。它就相当于入口和出口一样,把Action的相关方法包裹在中间了
,过滤器可以组成过滤器链,也就是有多个过滤器来过滤相同的东西。拦截器同样也有拦截器链,在struts2中称为
拦截器栈,拦截器执行的顺序是按照配置的顺序执行的。
假设先配置myInterceptor1,再配置myInterceptor2,所有在执行时,先执行myInterceptor1,后执行myInterceptor2
,但是结束时,先执行myInterceptor2,后执行myInterceptor1,就好stack,压栈和弹栈的过程
过程是:先会进入第一个拦截器,出来后再进入第二个拦截器,以此类推,最后进入Action的execute()方法,之后是进行
相反地顺序。
因此invoke就是用来判断,若还有下一个拦截器,就调用下一个拦截器,否则,直接跳到Action的execute方法
------------继承了AbstractInterceptor类的自定义拦截器MyInterceptor3
public class MyInterceptor3 extends AbstractInterceptor{
public String interceptor(ActionInvocation invocation)throws Exception{ System.out.println("---------MyInterceptor3 invoked begin"); String result=invocation.invoke(); System.out.println("---------MyInterceptor3 invoked finish-----"); return result; } }
继承AbstractInterceptor类的拦截器就不需要自己去实现init和destory方法了,在我们应用之中,更多的时候还是
继承AbstractInterceptor而不是实现Interceptor
-------------继承MethodFilterInterceptor拦截器
public class MyInterceptor4 extends MethodFilterInterceptor{ protected String doIntercept(ActionInvocation invocation)throws Exception{ System.out.println("------MyInterceptor4 invoked begin"); String result=invocation.invoke(); System.out.pritnln("----MyInterceptor4 invoked finish-----"); } }
MethodFilterInterceptor拦截器继承了AbstractInterceptor,他就是一个拦截某一个具体的方法的过滤拦截器。
而MethodFilterInterceptor类已经自动实现好了Intercept方法,实际上MethodFilterInterceptor类中的Intercept
方法真正执行的是它本身的一个doIntercept()抽象方法,因此我们需要拦截某一个方法时候:只要继承
methodFilterInterceptor类,然后实现doIntercept
原创文章,转载请表明出处.
更多信息请查看 java进阶网 http://www.javady.com/index.php/category/thread
发表评论
-
struts第一篇:struts诞生记
2012-04-23 22:29 1103Struts是一个开源框架,学Struts有一段时间 ... -
Struts2教程 - 5.4.4 debug标签使用介绍
2012-04-16 21:23 1034技术要点 本节代码具体介绍 debug 数据 标签的使 ... -
Struts2教程 - 5.4.3 date标签使用介绍
2012-04-16 21:23 1144技术要点 本节代码具体介绍 date 数据 标签的使用 ... -
Struts2教程 - 5.2 OGNL表达式语言介绍
2012-04-14 21:15 2117在进行 Struts2 的标签库介绍之前,有必要着重对 ... -
Struts2教程 - 4.1 拦截器在Struts2中的缺省应用
2012-04-09 10:11 1111通过前几章介绍,读者应该明白在 Web 项目中,客户先在视图界 ... -
Struts2教程 - 使用Action类控制导航业务数据
2012-04-09 10:10 1233Struts2 中 Action 充当着一个关键的角色。它解决 ... -
Struts2教程 - 3.4 使用ActionSupport进行校验
2012-04-08 09:04 1554如果 From 数据操作有误,比如输入不是我们需要的数据, ... -
Struts2教程 - 3.3使用Action类控制导航业务数据
2012-04-08 09:04 1131Struts2 中 Action 充当着一个关键的角色。 ... -
Struts2教程 - 5.4.8--10 url标签使用介绍
2012-04-16 21:24 10255.4.8 url 标签使用介绍 ... -
Struts2教程 - 5.4.6-7push、set标签使用介绍
2012-04-16 21:23 1174技术要点 本节代码具体介绍 push ... -
Struts2教程 - 5.4.5 include标签使用介绍
2012-04-16 21:23 1212技术要点 本节代码具体介绍 inclu ... -
Struts2教程 - 5.4.4 debug标签使用介绍
2012-04-17 23:46 1014技术要点 本节代码具体介绍 debug ... -
Struts2教程 - 5.4.3 date标签使用介绍
2012-04-17 23:46 1103技术要点 本节代码具体介绍 date ... -
Struts2教程 - 5.4.2 bean标签使用介绍
2012-04-15 22:57 846技术要点 本节代码具体介绍 bean ... -
Struts2教程 - 5.4.1 action标签使用介绍
2012-04-15 22:57 1130技术要点 本节代码具体介绍 actio ... -
Struts2教程 - 5.3.7 subset标签使用介绍
2012-04-15 22:57 11705.3.7 subset 标签使用介绍 ... -
Struts2教程 - 5.3.5-6 merge、sort标签使用介绍
2012-04-15 22:57 10545.3.5 merge 标签使用介绍 ... -
Struts2教程 - 5.3.4 iterator标签使用介绍
2012-04-14 21:14 1081技术要点 本节代码具体介绍 itera ... -
Struts2教程 - 5.3.3 if、else、elseif标签使用介绍
2012-04-14 21:14 1303技术要点 本节代码具体介绍 if 、 ... -
Struts2教程 - 5.3.2 generator标签使用介绍
2012-04-14 21:14 936技术要点 本节代码具体介绍 gener ...
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
J2EE企业级项目开发-3期(KC007) 8.3 Struts之拦截器使用技巧考试.doc 学习资料 复习资料 教学资源
J2EE企业级项目开发-3期(KC007) 8.3 Struts之拦截器使用技巧文档.doc 学习资料 复习资料 教学资源
Struts2拦截器.ppt Struts2拦截器.ppt Struts2拦截器.ppt
Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
关于struts2 拦截器的详细的解释及其在开发中对struts2的拦截器的应用和拦截器在框架中的作用.
struts1.2拦截器使用插件
NULL 博文链接:https://cmexico.iteye.com/blog/621208
Struts2拦截器实现权限控制demo,我感觉对初学者还是有一定帮助的
struts2拦截器应用小例子,与大家共分享
struts2拦截器的使用说明文档,配置说明文档。可以看一下
使用struts的拦截器拦截一些敏感性的词或一些非法或不文秘的言论
基于struts2的拦截器测试,实现了页面的跳转,中间过程的拦截
1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. 拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按...
Struts2拦截器原理分析实例 博文链接:https://zmx.iteye.com/blog/457432
创建自己struts2拦截器,手工创建自己的拦截器实现
struts2.0描述了拦截器并有对应的例子,用struts实现crud 用法及例子,并介绍了struts2的action的流程 及请求过程
struts2拦截器和验证框架
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.