`

Action的执行机制

阅读更多
ActionProxyFactory:
  create ActionProxy factory,create ActionInvocation,create    ActionProxy
ActionProxy:
serves as client code's handle to execute an action,it holds an ActionInvocation which reprents the current   satate of the execution of the action.ActionProxy is created by a ActionProxyFactory in a dispatcher.ActionProxy sets up the execute context for the ActionInvocation and then calls invoke() on the ActionInvocation.
ActionInvocation: reprents the current state of the execution of the action,it holds the action instance ,the interceptors to be applied,the map of results,and an ActionContext.
ActionContext: which includes the request parameters,the application map ,the session map,the Local, and the ServletRequest and ServletResponse.

执行过程如下:
     (1)在web.xml中指定的ServletDispacter或者FilterDispatcher,负责调用ActionProxyFactory完成ActionProxy的创建,
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, context);
     (2)ActionProxy 为ActionInvocation设定执行环境上下文,并调用ActionInvocation的invoke()方法
     (3)关键在于ActionInvocation的invoke()方法,

public String invoke() throws Exception {
        if (executed) {
            throw new IllegalStateException("Action has already executed");
        }
        if (interceptors.hasNext()) {  // notice here is if ,not  while 
            InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
                  /** 调用拦截器的方法,将ActionInvocation作为参数
                    *  在intercept(this);中回调invoke() 方法,是一种递归的方式
                    */
                resultCode = interceptor.getInterceptor().intercept(this);
        } else {
            resultCode = invokeActionOnly();//当没有了拦截器没有调用时就invokeAction!然后返回!因为是递归调用的!然后按反顺序返回进行剩下的没完成部分...
        }

        // 做结果返回前的处理
        // this is needed because the result will be executed, then control will return to the Interceptor, which will
        // return above and flow through again
        if (!executed) {
            if (preResultListeners != null) {
                for (Iterator iterator = preResultListeners.iterator();
                     iterator.hasNext();) {
                    PreResultListener listener = (PreResultListener) iterator.next();
                    listener.beforeResult(this, resultCode);
                }
            }
            // now execute the result, if we're supposed to
            if (proxy.getExecuteResult()) {
               //执行结果的返回
                executeResult();
            }
            executed = true;
        }
        return resultCode;
    }

分享到:
评论

相关推荐

    Oozie之JavaAction测试实例

    #### 二、JavaAction执行机制 根据官方文档,JavaAction将会执行指定主Java类中的`public static void main(String[] args)`方法。Java应用程序将以单个Mapper任务的形式在Hadoop集群中执行。工作流任务会等待Java...

    struts2运行机制

    ### Struts2运行机制详解 #### 一、Struts2框架概述 Struts2是一个基于MVC(Model-View-Controller)设计模式的Java Web应用框架,它为开发者提供了构建可扩展、易于维护的Web应用程序的工具。Struts2不仅继承了...

    Struts1框架执行原理(附Action、ActionServlet、ActionForm工作机制)

    本篇文章将深入解析Struts1框架的执行原理,以及Action、ActionServlet、ActionForm三个关键组件的工作机制。 首先,我们来看Struts1的工作流程: 1. 用户通过浏览器发起HTTP请求,请求的目标是应用中的一个JSP或...

    jsp文件运行的机制

    ### JSP 文件运行机制详解 #### 一、JSP 文件的基本概念 JSP(Java Server Pages)是一种基于Java技术的动态网页技术标准。它允许在HTML文档中嵌入Java代码和表达式,使得Web页面能够根据请求动态生成内容。通过...

    action之间的跳转

    当 type 为 chain 时,说明是 action 链,运行完第一个 action java 文件接着会运行第二个 action JAVA 文件,相当于 forward(客户端的 url 不会改变)。这个 result 调用另外的一个 action,连接自己的拦截器栈和 ...

    Android action 分类大全

    20. `ACTION_EMBED`:允许Activity在其他Activity中内嵌运行,常用于实现组件嵌入。 21. `ACTION_EMERGENCY_DIAL`:拨打紧急电话,如911或112等,用于紧急情况。 22. `ACTION_MAIN`:通常与`CATEGORY_LAUNCHER`...

    struts1和struts2的区别(详细)

    #### 一、Action执行机制的不同 - **Struts1**: 在Struts1框架中,Action是基于单例模式的,这意味着所有的请求都会共享同一个Action实例。这就导致了如果在Action中保存实例变量,则可能会出现线程安全问题。此外...

    jsp自动调用action

    在Struts框架中,通常通过配置文件(struts.xml)定义Action,并且在用户请求到达时,框架会根据请求参数匹配相应的Action执行逻辑。然而,在某些场景下,我们希望在不进行任何用户交互的情况下就执行某些逻辑,比如在...

    structs in action中文完全版\6. 连线 ActionForward.pdf

    1. **标准流程控制**:最常见的应用场景是在一个Action执行完毕后,根据特定的逻辑条件选择不同的ActionForward,从而决定是显示某个JSP页面还是调用另一个Action。 2. **携带参数的转发**:通过在ActionForward的...

    sturts运行机制

    综上所述,Struts的运行机制是通过配置文件、控制器组件(ActionServlet和RequestProcessor)、ActionForm以及Action对象的交互来实现对客户端请求的处理,并将处理结果呈现给用户。这种机制使得开发者能够以一种...

    strtus_xwork源码

    拦截器是Struts2中的一个重要概念,它们按照配置的顺序在Action执行前后运行,提供诸如日志、权限检查、结果映射等功能。XWork的拦截器模型使得我们能灵活地扩展和定制框架的行为。 3. **类型转换(Type Conversion...

    PHP运行机制.pdf

    ### PHP运行机制解析 #### 一、概述 本文档旨在深入探索PHP的运行机制,通过图解的方式,帮助读者理解PHP在服务器环境中的工作原理及其内部处理流程。这对于那些希望深入了解PHP底层实现以及如何优化其应用程序...

    java的映射机制

    如果在Action执行过程中抛出异常,框架可以根据预先定义的映射规则,自动跳转到错误页面或者特定的处理流程,提高了错误处理的灵活性和统一性。 #### 映射机制的优势 - **提高代码的可维护性和可扩展性**:通过...

    android Action call 拨打电话 Intent.ACTION.CALL

    然而,为了安全和隐私考虑,使用ACTION_CALL权限需要声明`<uses-permission>`标签,并且在Android 6.0(API级别23)及以上版本,还需要在运行时请求权限。 以下是一个简单的示例,展示了如何创建并发送一个ACTION_...

    Struts2的工作机制及分析

    开发者可以根据需要组合和配置拦截器栈,这些拦截器会在Action执行前后按顺序执行。 视图组件在Struts2中得到了增强,例如,Struts2提供了一系列组件如updownselect、doubleselect、datetimepicker、token和tree等...

    通过表单提交实现调用不同的action方法

    在Web开发中,我们经常需要通过用户交互来执行不同的业务逻辑。...总之,理解URL、Action、jsp以及表单提交的交互机制是Web开发中的基础技能。通过学习和实践,你将能够更有效地构建动态、响应式的Web应用程序。

    Android自定义action与permission_java_action_

    Intent是一种消息传递机制,用于启动其他组件(如Activity、Service或BroadcastReceiver)来执行特定任务。自定义Action允许开发者创建特定于应用的行为,使得组件间的交互更加灵活。例如,你可以定义一个...

    struts2 对action中的所有方法进行校验

    Struts2通过Action类的校验机制,帮助开发者实现这一目标。下面我们将深入探讨Struts2如何对Action中的所有方法进行校验。 首先,Struts2的校验机制基于两个主要组件:Validator和ValidationInterceptor。Validator...

Global site tag (gtag.js) - Google Analytics