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

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
=========================================================

分享到:
评论

相关推荐

    struts2简单模拟

    通过实践简单的Struts2模拟代码,可以更好地理解和体验Struts2框架的工作原理和优势。在实际开发中,还需要不断学习和熟悉Struts2的高级特性,如自定义拦截器、类型转换、插件使用等,以提升开发能力。

    struts2漏洞利用工具

    首先,我们要理解Struts2漏洞的核心在于其ActionServlet组件处理用户输入的方式。例如,一个著名的漏洞是CVE-2017-5638,也就是Struts2 OGNL注入漏洞。这个漏洞允许攻击者通过HTTP请求头中的Content-Type字段注入...

    Servlet简单模拟Struts2

    3. **动态生成Action的代理**:在Struts2中,Action是业务逻辑的载体,通过动态代理,Struts2可以在调用实际Action方法之前/之后执行一些额外的操作。在Servlet中,我们可以使用Java的反射API来创建Action的代理类。...

    struts2框架模拟-教学示范代码

    4. **结果类型(Result)**:Action执行完成后,会返回一个结果类型,决定如何展示给用户。常见的结果类型有dispatcher(用于转发到JSP页面)和stream(用于下载文件)等。 5. **插件支持**:Struts2有丰富的插件库...

    测试struts2中action

    然后通过一系列断言来验证Action执行后的预期结果,比如用户名是否正确、是否跳转到成功页面、是否存在Action错误等。 #### 5. 测试失败登录 ```java public void testFailedLogin() { addRequestParameter(...

    Struts2SpringUnitDemo单元测试

    4. **Action和Service的测试**:在Struts2中,Action类是处理用户请求的实体,它通常调用Service层来执行业务逻辑。Service层封装了与数据库或其他外部资源的交互。在单元测试中,Action的测试主要检查其接收请求、...

    struts1和struts2的区别

    #### Action执行过程 - **Struts1**: 支持为每个请求定义一组处理器(RequestProcessors),但这些处理器对所有Action都是一样的。 - **Struts2**: 支持为每个Action定义不同的拦截器堆栈(Interceptor Stacks)...

    Struts2+Ajax_模拟登陆Demo,包完整

    7. **异常处理**:在Struts2中,我们可以配置全局异常处理器,处理Action执行过程中可能出现的异常,返回统一的错误信息,保证系统的健壮性。 8. **调试与测试**:学习如何对Struts2和Ajax的组合进行调试和测试,...

    Struts2单元测试

    在Struts2框架中,单元测试对于验证Action类、拦截器、结果类型以及其他核心组件的行为尤其关键。 在Struts2的4.2.4版本中,提供了专门的单元测试jar包,使得开发者能够方便地对Struts2的组件进行单元测试。这个jar...

    struts2整合hibernate的网上银行模拟项目

    这个“struts2整合hibernate的网上银行模拟项目”结合了这两个框架,创建了一个模拟的网上银行系统,旨在展示如何在实际项目中使用它们。 首先,让我们深入了解一下Struts2。Struts2 是 Struts 的下一代,它继承了...

    struts2经典实例

    `Struts2_Action` 示例展示了如何定义和执行Struts2中的Action,这是框架的核心部分。通过这个实例,你可以学习到Action的生命周期、结果映射以及如何处理业务逻辑。 7. **Struts2 HelloWorld** `Struts2_Hello...

    moke测试struts的action

    5. **拦截器(Interceptor)**:Struts 2引入了拦截器,它们在Action执行前后执行,可以用来实现日志记录、权限检查、数据校验等通用功能。 6. **异常处理**:Struts提供了处理异常的方式,允许我们在全局或Action...

    模拟struts2框架代码.rar_模拟struts框架

    然后,Struts2的拦截器(Interceptor)机制是其一大特色,它可以对Action执行前后的逻辑进行增强。在模拟实现中,我们可以创建自定义的拦截器,例如日志拦截器、权限拦截器等,并在配置文件中声明它们。 ```xml ...

    Struts2调试方法

    8. **异常处理**:Struts2提供了全局异常处理机制,当Action执行抛出异常时,可以通过全局异常处理器进行统一处理。确保异常处理器配置正确,并能捕获到预期的异常。 9. **使用开发工具**:利用浏览器的开发者工具...

    servlet模拟struts1工作原理

    ActionForm用于封装请求参数,Action执行业务逻辑,最后,视图(View)负责展示结果。 在模拟Struts1的过程中,我们可以创建一个Servlet来替代ActionServlet的角色。这个Servlet需要实现以下功能: 1. **解析XML...

    Struts2工作原理模拟

    4. **Action上下文(ActionContext)**:ActionContext是Action执行期间共享数据的容器,它可以跨拦截器和Action之间传递信息,如请求、session、应用上下文等数据。 5. **Value Stack**:Value Stack是Struts2的一...

    转:struts1与struts2的区别

    Action执行流程的控制 - **Struts1**: 支持为每个模块单独配置Request Processors(即生命周期管理器),但模块内的所有Action必须共享相同的生命周期。 - **Struts2**: 通过配置拦截器堆栈(Interceptor Stacks)...

    使用struts2写的小项目

    3. **拦截器(Interceptors)**:Struts2的拦截器机制使得在Action执行前后可以添加自定义的处理逻辑,如日志记录、权限验证等。 4. **模型驱动(ModelDriven)**:一种设计模式,允许Action类通过持有模型对象来...

    struts2测试工具

    7. **IntelliJ IDEA或Eclipse的Struts2插件**:这些IDE插件可能包含特定于Struts2的测试支持,比如生成Action的测试模板。 压缩包内的"鬼哥struts2测试工具.exe"可能是一个由个人开发者或社区创建的专用测试工具,...

    基于Struts2的留言板系统.zip

    Struts2也支持Mock测试,模拟请求来测试Action的响应。 13. **部署与运行**: 最后,将整个项目打包成WAR文件,部署到应用服务器如Tomcat上,用户就可以通过浏览器访问留言板系统进行交互了。 基于Struts2的留言板...

Global site tag (gtag.js) - Google Analytics