在struts的开发文档有一个图,从这个官方的picture,你也能大致的看出struts到底是怎么样执行的。
说明:一个请求(HttpServletRequest)到来,它会经过FilterDispatcher(这个原来的,现在已经的是StrutsPrepareAndExecuteFilter)。当HttpServletRequest到达FilterDispatcher的时候,FilterDispatcher会帮我们创建ActionPproxy,ActionProxy通过ConfigurationManager从struts.xml配置文件中读出来相应的配置信息。ActionProxy拥有一个AcionInvocation的实例,接下来,它会调用ActionInvocation的invoke方法,在invoke方法里面,它会先调用第一个interceptor1,然后再调用第二个interceptor2,再调用第三个interceptor3,最后再调用action,action会返回一个result,根据result找到相应的视图(jsp,FreeMarker等)。然后再经过interceptor3,interceptor2,interceptor1,FilterDispatcher将用户请求的信息反馈给用户。
下面我们就来模拟Struts2的拦截器
工程目录结构以及文件如下:
1.ActionInvocation.java里面有action的引用和很多interceptor
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ActionInvocation { protected Object action;// action的引用 protected Iterator<Interceptor> interceptors; protected String resultCode;// 保存action执行的result ActionInvocation() { action = new Action(); List<Interceptor> interceptorList = new ArrayList<Interceptor>(); interceptorList.add(new FirstInterceptor()); interceptorList.add(new SecondInterceptor()); interceptors = interceptorList.iterator(); } public String invoke() { if (interceptors.hasNext()) {// 是否还有下一个interceptor resultCode = this.interceptors.next().intercept( ActionInvocation.this); } else {// 说明所有的interceptor都调用完了,通过反射调用Action中的方法 resultCode = this.invokeActionOnly(); } return resultCode; } public String invokeActionOnly() { Action ac = (Action) action; return ac.execute(); } }
2.所有的拦截器都实现的interceptor接口
public interface Interceptor { public String intercept(ActionInvocation actionInvocation); }
3.生成2个拦截器FirstInterceptor,SecondInterceptor,都实现了Interceptor接口
FirstInterceptor
public class FirstInterceptor implements Interceptor{ @Override public String intercept(ActionInvocation invocation) { String result; System.out.println(1); result = invocation.invoke(); System.out.println(-1); return result; } }
SecondInterceptor
public class SecondInterceptor implements Interceptor{ @Override public String intercept(ActionInvocation invocation) { String result; System.out.println(2); result = invocation.invoke(); System.out.println(-2); return result; } }
3.编写Action
public class Action { public String execute(){ System.out.println("------execute----------"); return "success"; } }
4.编写测试类
public class Test { public static void main(String[] args) { new ActionInvocation().invoke(); } }
相关推荐
Struts2拦截器原理分析实例 博文链接:https://zmx.iteye.com/blog/457432
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
在action执行之前先进行拦截器的拦截处理,然后再执行action的execute方法,返回给拦截器,再对应相应result
仿struts 2 拦截器原理
Struts2拦截器.ppt Struts2拦截器.ppt Struts2拦截器.ppt
struts2拦截器原理模拟,示例里面做了大量注释,对所有可能让你产生疑惑的地方都详细描述了原因。
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
浅谈Struts2拦截器的原理与实现.docx
Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序
关于struts2 拦截器的详细的解释及其在开发中对struts2的拦截器的应用和拦截器在框架中的作用.
Struts2拦截器实现权限控制demo,我感觉对初学者还是有一定帮助的
NULL 博文链接:https://cmexico.iteye.com/blog/621208
struts2拦截器应用小例子,与大家共分享
模拟Struts2拦截器代码,模拟struts2拦截器的实现
基于struts2的拦截器测试,实现了页面的跳转,中间过程的拦截
struts2拦截器的使用说明文档,配置说明文档。可以看一下
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器 三、定义Struts2拦截器。 ...
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
使用struts的拦截器拦截一些敏感性的词或一些非法或不文秘的言论