---
title: "Struts2源码分析讨论"
author: "HJXia"
date: "2007-12-26T17:26:35Z"
tags: []
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2007-12-26T17:26:35Z"
id: 151006
url: "https://hjxia.iteye.com/blog/151006"
---

最近一直在研究struts2,看了李刚写的一本struts2权威指南,但感觉讲的还是有点浅,但是做为入门的话,还是不错的.现在开始看struts2的源码,但由于本人能力有限,看的是头晕脑涨的,有很多不明白的地方,所以想把不明白的地方贴出来跟大家一起讨论讨论.
其中有一处也觉的是比较关键的一步:
以下是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);
    	}
    }
```

不明白是怎样迭代执行一个又一个的Interceptor中的intercept方法.
按照我的理解,至少我可以看到一个循环遍厉interceptors这个集合才对.
大家可以讨论讨论,有知道的也可以告诉我.谢谢.