`
gutou9
  • 浏览: 142808 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Xwork2 源码阅读(三)

阅读更多

接着看Dispatcher

 

           ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                    namespace, name, extraContext, true, false);

 

用Container 构造一个 ActionProxyFactory,在用ActionProxyFactory create一个 ActionProxy。

 

    public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map extraContext, boolean executeResult, boolean cleanupContext) {
        
        ActionInvocation inv = new DefaultActionInvocation(extraContext, true);
        container.inject(inv);
        return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
    }

 

 

xwork中,action的调用是借助 Proxy 模式,

Proxy 模式的实现,则是 DefaultActionProxy, Action, ActionInvocation 合伙完成的。

下边是一些关键代码

 

DefaultActionProxy

 

    public String execute() throws Exception {
        ActionContext nestedContext = ActionContext.getContext();
        ActionContext.setContext(invocation.getInvocationContext());

        String retCode = null;

        String profileKey = "execute: ";
        try {
        	UtilTimerStack.push(profileKey);
        	
            retCode = invocation.invoke();
        } finally {
            if (cleanupContext) {
                ActionContext.setContext(nestedContext);
            }
            UtilTimerStack.pop(profileKey);
        }

        return retCode;
    }

 

 

 DefaultActionInvocation 得 invoke 方法。

 

 public String invoke() throws Exception {
    	String profileKey = "invoke: ";
    	try {
    		UtilTimerStack.push(profileKey);
    		
    		if (executed) {
    			throw new IllegalStateException("Action has already executed");
    		}

    		if (interceptors.hasNext()) {
    			final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
    			UtilTimerStack.profile("interceptor: "+interceptor.getName(), 
    					new UtilTimerStack.ProfilingBlock<String>() {
							public String doProfiling() throws Exception {
				    			resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
				    			return null;
							}
    			});
    		} else {
    			resultCode = invokeActionOnly();
    		}

    		// 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();
    					
    					String _profileKey="preResultListener: ";
    					try {
    						UtilTimerStack.push(_profileKey);
    						listener.beforeResult(this, resultCode);
    					}
    					finally {
    						UtilTimerStack.pop(_profileKey);
    					}
    				}
    			}

    			// now execute the result, if we're supposed to
    			if (proxy.getExecuteResult()) {
    				executeResult();
    			}

    			executed = true;
    		}

    		return resultCode;
    	}
    	finally {
    		UtilTimerStack.pop(profileKey);
    	}
    }

 

resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
是判断这个action是否有未被执行的拦截器,

 

resultCode = invokeActionOnly();

如没有或拦截器执行完毕,则执行invokeActionOnly

 

 invokeActionOnly方法

 

    public String invokeActionOnly() throws Exception {
    	return invokeAction(getAction(), proxy.getConfig());
    }

   protected String invokeAction(Object action, ActionConfig actionConfig) throws Exception {
        String methodName = proxy.getMethod();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing action method = " + actionConfig.getMethodName());
        }

        String timerKey = "invokeAction: "+proxy.getActionName();
        try {
            UtilTimerStack.push(timerKey);
            
            boolean methodCalled = false;
            Object methodResult = null;
            Method method = null;
            try {
                method = getAction().getClass().getMethod(methodName, new Class[0]);
            } catch (NoSuchMethodException e) {
                // hmm -- OK, try doXxx instead
                try {
                    String altMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
                    method = getAction().getClass().getMethod(altMethodName, new Class[0]);
                } catch (NoSuchMethodException e1) {
                	// well, give the unknown handler a shot
                	if (unknownHandler != null) {
	                	try {
	                		methodResult = unknownHandler.handleUnknownActionMethod(action, methodName);
	                		methodCalled = true;
	                	} catch (NoSuchMethodException e2) {
	                		// throw the original one
	                		throw e;
	                	}
                	} else {
	            		throw e;
	            	}
                }
            }
        	
        	if (!methodCalled) {
        		methodResult = method.invoke(action, new Object[0]);
        	}
        	
            if (methodResult instanceof Result) {
            	this.explicitResult = (Result) methodResult;
            	return null;
            } else {
            	return (String) methodResult;
            }
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException("The " + methodName + "() is not defined in action " + getAction().getClass() + "");
        } catch (InvocationTargetException e) {
            // We try to return the source exception.
            Throwable t = e.getTargetException();

            if (actionEventListener != null) {
                String result = actionEventListener.handleException(t, getStack());
                if (result != null) {
                    return result;
                }
            }
            if (t instanceof Exception) {
                throw(Exception) t;
            } else {
                throw e;
            }
        } finally {
            UtilTimerStack.pop(timerKey);
        }
    }

 

method = getAction().getClass().getMethod(methodName, new Class[0]);
取出需执行的action的方法,如 execute()

 

methodResult = method.invoke(action, new Object[0]);
利用反射,执行action 方法

 

上边就是利用proxy模式,调用一个action的具体逻辑 的 过程。

 

一般的action里,都有一些变量,如从request中取出的参数等,

如要执行action 方法,如execute(),则必须先吧这些变量赋值 已供方法中使用,

这个工作,就是 拦截器(Interceptor) 的任务之一,

 

下篇探讨一下拦截器。

 

分享到:
评论

相关推荐

    Xwork2 源码阅读(一)

    《Xwork2 源码阅读(一)——深度解析框架基础与核心机制》 Xwork2是Struts2框架的核心部分,它提供了一种基于Action的模型-视图-控制器(MVC)架构,是Java Web开发中的重要组件。这篇分析文章将深入探讨Xwork2的...

    Xwork2 源码阅读.pdf(附源码)

    《Xwork2 源码阅读.pdf》是一个深入解析Xwork2框架的文档,结合了Webwork和Struts2的源码分析,旨在帮助开发者理解这两个著名MVC框架的内部工作机制。Xwork2是Struts2的核心部分,负责处理Action的执行流程、拦截器...

    xwork2.1.2源码与xwork2.2.1源码

    《深入解析xwork 2.x源码:从2.1.2到2.2.1的演进》 xwork作为一个强大的Action框架,是Struts2的...通过深入学习xwork源码,开发者可以更好地掌握MVC设计模式,理解Web应用的控制层实现,从而在实践中发挥更大的效能。

    xwork2.1.6源码及其之前版本的源码

    8. **设计模式**:XWork源码中运用了多种设计模式,如工厂模式(Factory)、单例模式(Singleton)、装饰器模式(Decorator)等,这些都是Java开发中的经典模式,值得深入研究。 通过学习和分析XWork 2.1.6的源码,...

    struts2 xwork2 源码

    在06170300180这个文件中,可能包含了Struts2和XWork2的源码,你可以通过阅读这些源码来进一步了解这两个框架的实现细节。这将是一个宝贵的资源,帮助你深入学习和掌握Struts2和XWork2,从而提升你的Java Web开发...

    xwork官网源码 下载 xwork

    《深入理解XWork框架:官方源码解析》 XWork是一个强大的Action框架,它为Java Web应用程序提供了模型-视图-控制器(MVC)模式的支持。这个框架的主要目标是简化企业级应用的开发,提高代码的可维护性和可扩展性。...

    xwork_struts2 源码

    这次我们讨论的是"xwork_struts2"源码,包括"struts2-core-2.3.1.2.jar"和"xwork-core-2.3.1.2.jar"两个部分。 1. **XWork核心**:XWork是Struts2的基础,它提供了一套动作框架,负责处理HTTP请求、执行业务逻辑...

    struts2下的xwork源码

    深入学习XWork源码,你可以了解到以下核心部分: 1. **ActionInvocation**:这是执行Action的入口点,它维护了拦截器栈,并负责调用每个拦截器和Action。 2. **ActionProxy**:ActionProxy是ActionInvocation的...

    xwork源码 xwork src Eclipse关联xwork源码

    深入理解XWork源码对于学习和优化Struts2应用至关重要。在这个压缩包中,包含的是XWork 2.0.7版本的源代码,特别适合开发者进行研究和调试。以下是关于XWork源码及如何在Eclipse中关联XWork源码的详细讲解。 1. **...

    xwork源码及札包

    总之,这个"XWork源码及打包"资源包为深入学习和开发基于Struts2的Web应用提供了宝贵的材料,无论是初学者还是经验丰富的开发者,都能从中受益。通过研究源码,开发者不仅可以提高技能,还能更好地利用和定制这个...

    XWork源码+docs全

    XWork是Struts2框架的核心组件,它提供了一种基于拦截器(Interceptor)的Action管理机制,为构建可维护、可扩展的企业级Web应用程序...深入学习XWork源码,对于提升对Struts2乃至整个MVC框架的理解都是非常有益的。

    struts-xwork-core源码

    Struts-xwork-core是Struts2框架的核心组件,它提供了Action和结果的执行模型,以及类型...总之,深入学习Struts-xwork-core的源码,将帮助你更好地掌握Struts2框架的精髓,提高开发效率,解决实际项目中遇到的问题。

    Struts2中xwork源码

    本文将深入探讨XWork的源码,解析其设计理念和关键实现,帮助开发者更好地理解和使用Struts2。 1. **ActionInvocation**:XWork的核心是ActionInvocation,它是执行动作的实际载体。ActionInvocation维护了动作执行...

    XWork 源码

    深入学习XWork源码,我们可以关注以下几个核心概念: - **Action接口与ActionSupport类**:Action接口定义了动作的基本行为,而ActionSupport是它的默认实现,提供了默认的错误处理和结果返回机制。 - **...

    struts2.014和xwork2.07源码下载

    通过阅读源码,我们可以学习到如何设计可扩展的框架,以及如何利用拦截器实现AOP。此外,对于性能优化和问题排查,熟悉源码也具有极大的帮助。 总之,Struts2和XWork是Java Web开发中的重要工具,它们提供了强大的...

    Xwork的源码

    **Xwork源码详解** Xwork是Struts2框架的核心组成部分,主要负责处理Action的业务逻辑,它是表现层框架Struts2的内核。在深入理解Struts2的工作原理时,解析Xwork的源码至关重要。Xwork为Struts2提供了强大的命令...

    xwork-2.0.4源码

    这个源码包是理解Struts2工作原理的关键,因为它包含了xwork的核心实现,包括动作调度、依赖注入、类型转换、拦截器机制等多个关键模块。现在,让我们一起深入探讨xwork-2.0.4源码中的核心知识点。 1. **动作调度...

    xwork的源码jar包

    xwork 2.8的源码,最新版本欢迎下载!

    struts2中xwork源码

    通过对XWork源码的学习,开发者可以深入理解Struts2框架的内部运作机制,包括Action的生命周期、拦截器的执行顺序、OGNL的表达式解析等。这不仅有助于调试和优化代码,也能为自定义扩展或开发更复杂的web应用提供...

    xwork2.0.3 源码

    在XWork源码中,我们可以看到OGNL的解析和表达式求值过程,这对于理解Struts2中的数据绑定至关重要。 5. **TypeConverter**:XWork提供了一套强大的类型转换机制,使得不同类型的参数能够被自动转换成动作方法所需...

Global site tag (gtag.js) - Google Analytics