`
wmj2003
  • 浏览: 97549 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

struts2的action执行过程模拟

 
阅读更多

package xwork;

import java.io.Serializable;

/**
* @author wangmingjie
* @date 2008-9-26上午11:09:05
*/
public interface Interceptor extends Serializable {
String intercept(ActionInvocation invocation) throws Exception;
}
==============================================

package xwork;
/**
* @author wangmingjie
* @date 2008-9-26上午11:11:56
*/
public class FirstInterceptor implements Interceptor {

public String intercept(ActionInvocation invocation) throws Exception {
String resultCode = null;
System.out.println("before first Interceptor ");
resultCode = invocation.invoke();
System.out.println("after first Interceptor ");
return resultCode;
}

}
==============================================

package xwork;
/**
* @author wangmingjie
* @date 2008-9-26上午11:13:34
*/
public class SecondInterceptor implements Interceptor {

public String intercept(ActionInvocation invocation) throws Exception {
String resultCode = null;
System.out.println("before second Interceptor ");
resultCode = invocation.invoke();
System.out.println("after second Interceptor ");
return resultCode;
}

}

==================核心调用过程============================

package xwork;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* @author wangmingjie
* @date 2008-9-26上午11:10:00
*/
public class ActionInvocation {
private boolean executed = false;
private String resultCode = null;
private Iterator interceptors;
private void init(){
List<Interceptor> interceptorList = new ArrayList<Interceptor>();
interceptorList.add(new FirstInterceptor());
interceptorList.add(new SecondInterceptor());
interceptors = interceptorList.iterator();
}

public String invoke() throws Exception {
if(executed){
throw new Exception("已经执行了");
}
if (interceptors.hasNext()) {//下面就是核心实现代码
resultCode = ((Interceptor)interceptors.next()).intercept(ActionInvocation.this);//注意这里,将自己作为参数传入,这是一种递归的调用方法。
} else {
resultCode = "success";//执行action的方法
System.out.println("执行action");
}

if (!executed) {
System.out.println("执行preResultListener");
executed = true;
}
return null;
}

public static void main(String[] args) throws Exception{
//再进行断点跟踪一下。
ActionInvocation ai = new ActionInvocation();
ai.init();
ai.invoke();
}

}

================执行结果如下==============================

before first Interceptor
before second Interceptor
执行action
执行preResultListener
after second Interceptor
after first Interceptor
=========================================================

分享到:
评论

相关推荐

    Servlet简单模拟Struts2

    用sevrlet模拟Struts2的简单功能。从拦截请求、解析自定义xml数据文件以及动态生成action的代理去执行目标方法,并实现了简单的日志拦截【interceptor】

    struts2模拟

    struts2模拟模拟工具,可以实现action访问,我表单属性的自动封装。提供了页面显示和跌带器,利用struts2值栈(valueStack)和对象栈map栈的思想。可以用来了解struts2的执行过程,纯属个人学习

    struts2.0.jar

    · 简化的Action: Struts 2 Action类独立于框架,是简化的普通Java对象(POJO)。拥有execute()方法的任何Java类都可以用做Action类。 · POJO表单: Struts 2不支持ActionForms特性。ActionForms中定义的属性可以...

    Spring in Action(第二版 中文高清版).part2

    16.2 协同使用Spring和WebWork 2/Struts 2 16.3 集成Spring和Tapestry 16.3.1 集成Spring和Tapestry 3 16.3.2 集成Spring和Tapestry 4 16.4 协同使用Spring和JSF 16.4.1 解析JSF管理的属性 16.4.2 解析Spring...

    Spring in Action(第2版)中文版

    16.2协同使用spring和webwork2/struts2 16.3集成spring和tapestry 16.3.1集成spring和tapestry3 16.3.2集成spring和tapestry4 16.4协同使用spring和jsf 16.4.1解析jsf管理的属性 16.4.2解析springbean 16.4.3...

    名为责任链或者拦截器或者过滤器的简单模拟

    比如说,struts2中Action在执行之前会首先执行一些Interceptor,完成诸如权限验证/属性注入/Validation/国际化等等的功能, 我看过一个比较好的功能是,利用strut2的拦截器和*-Validation.xml文件以及标签(strut2...

    基于SSH模拟当当网项目(电子商务平台)

    struts.xml不需要改变,因为引入struts-spring-plugin.jar后,Action创建会交给插件的ObjectFactory. web.xml需要定义ContextLoaderListener,实例化容器配置 5.将事务管理交个Spring,采用AOP方式,删除原有Struts事务...

    Spring in Action(第二版 中文高清版).part1

    16.2 协同使用Spring和WebWork 2/Struts 2 16.3 集成Spring和Tapestry 16.3.1 集成Spring和Tapestry 3 16.3.2 集成Spring和Tapestry 4 16.4 协同使用Spring和JSF 16.4.1 解析JSF管理的属性 16.4.2 解析Spring...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

Global site tag (gtag.js) - Google Analytics