深入研究Struts2(二)-StrutsPrepareAndExecuteFilter源码剖析

本文绝对原创,呕心沥血读了2天, 欢迎大家转载, 但是请朋友们尊重一下劳动成果,转载请注明出处

http://blog.csdn.net/izard999/article/details/40143439


在面试的时候,很多人经常会被问到:Struts2与Struts1的区别..我只想说, 最根本的区别是Struts2基于Filter,Struts1基于Servlet, 在Web容器中, Filter的优先级是高于Servlet的

那么上一篇文章中, 我给大家呈现那个官方的大图上面有个FilterDispatcher, 没错.  它就是Struts2的核心过滤器. 但是后来被遗弃

Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one

为什么遗弃我之前有所提到.  所以下面会研究StrutsPrepareAndExecuteFilter , 而不会再提及FilterDispatcher了.

在web.xml中我们指定Struts2的Filter为:

<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

从这里看得出来, Struts2官方起名字还是挺有意思的.. ng是什么.?   ng是next generation的缩写, 下一代产品

下面正题开始, StrutsPrepareAndExecuteFilter源码剖析,  有三个属性:

protected PrepareOperations prepare; //准备的一些操作
protected ExecuteOperations execute; //执行的一些操作
protected List<Pattern> excludedPatterns; //排斥的一些匹配路径

StrutsPrepareAndExecuteFilter与普通的Filter并无区别,方法除继承自Filter外,仅有一个回调方法,我们将按照Filter方法调用顺序,由init—>doFilter—>destroy顺序地分析源码。

1、init()

public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations();
        Dispatcher dispatcher = null;
        try {
<span style="white-space:pre">	</span>    //封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中 
            FilterHostConfig config = new FilterHostConfig(filterConfig);
            // 初始化struts内部日志  
            init.initLogging(config);
	    //创建dispatcher ,并初始化,这部分下面我们重点分析,初始化时加载哪些些资源 
            dispatcher = init.initDispatcher(config);
	    // 初始化静态资源(静态资源后面会有讲解)
            init.initStaticContentLoader(config, dispatcher);

	    //初始化类属性:prepare 、execute   
            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
	    //创建排斥路径列表
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
	    //回调空的postInit方法  
            postInit(dispatcher, filterConfig);
        } finally {
            if (dispatcher != null) {
                dispatcher.cleanUpAfterInit();
            }
            init.cleanup();
        }
    }
首先看下FilterHostConfig ,源码如下:

/**
 * Host configuration that wraps FilterConfig
 */
public class FilterHostConfig implements HostConfig {

    private FilterConfig config;

    public FilterHostConfig(FilterConfig config) {
        this.config = config;
    }
    /** 
     *  根据init-param配置的param-name获取param-value的值 
     */   
    public String getInitParameter(String key) {
        return config.getInitParameter(key);
    }

    /** 
     *  返回初始化参数名的List 
     */   
    public Iterator<String> getInitParameterNames() {
        return MakeIterator.convert(config.getInitParameterNames());
    }

    public ServletContext getServletContext() {
        return config.getServletContext();
    }
}
 只有短短的几行代码,getInitParameterNames是这个类的核心,将Filter初始化参数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。

重点来了,创建并初始化Dispatcher   
 public Dispatcher initDispatcher( HostConfig filterConfig ) {
        Dispatcher dispatcher = createDispatcher(filterConfig);
        dispatcher.init();
        return dispatcher;
    }

创建Dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根据ServletContext和参数Map构造Dispatcher :  
private Dispatcher createDispatcher( HostConfig filterConfig ) {
        Map<String, String> params = new HashMap<String, String>();
        for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
            String name = (String) e.next();
            String value = filterConfig.getInitParameter(name);
            params.put(name, value);
        }
        return new Dispatcher(filterConfig.getServletContext(), params);
    }
很多人问Struts2配置文件的加载顺序是什么? 不用死记硬背, 源码里面就有.
Dispatcher初始化,加载struts2的相关配置文件,将按照顺序逐一加载:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……
</pre><pre name="code" class="java">/**
 *初始化过程中依次加载如下配置文件
 */
public void init() {

    	if (configurationManager == null) {
    		configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
    	}

        try {
            //加载org/apache/struts2/default.properties
            init_DefaultProperties(); // [1]
            //加载struts-default.xml,struts-plugin.xml,struts.xml
            init_TraditionalXmlConfigurations(); // [2]
            init_LegacyStrutsProperties(); // [3]
            //用户自己实现的ConfigurationProviders类            
            init_CustomConfigurationProviders(); // [5]
            //Filter的初始化参数
            init_FilterInitParameters() ; // [6]
            init_AliasStandardObjects() ; // [7]

            Container container = init_PreloadConfiguration();
            container.inject(this);
            init_CheckConfigurationReloading(container);
            init_CheckWebLogicWorkaround(container);

            if (!dispatcherListeners.isEmpty()) {
                for (DispatcherListener l : dispatcherListeners) {
                    l.dispatcherInitialized(this);
                }
            }
        } catch (Exception ex) {
            if (LOG.isErrorEnabled())
                LOG.error("Dispatcher initialization failed", ex);
            throw new StrutsException(ex);
        }
    }
初始化default.properties,具体的初始化操作在DefaultPropertiesProvider类中
 private void init_DefaultProperties() {
        configurationManager.addConfigurationProvider(new DefaultPropertiesProvider());
    }
下面我们看下DefaultPropertiesProvider类源码:
public void register(ContainerBuilder builder, LocatableProperties props)
            throws ConfigurationException {
        
        Settings defaultSettings = null;
        try {
            defaultSettings = new PropertiesSettings("org/apache/struts2/default");
        } catch (Exception e) {
            throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);
        }
        
        loadSettings(props, defaultSettings);
    }

其他的我们再次省略,大家可以浏览下各个初始化操作都加载了哪些文件
注意,很多人有问过我, Struts2的配置文件名可以不可以不叫struts.xml, 类似Struts1那种自己定义文件名.?  在此,我明确的告诉大家是不可以的.
因为 Dispatcher类中指定了个常量:
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
意思是, 我们的配置文件只能是这几个名字, Struts2才会识别, 否则不识别,  读配置的顺序就是这个常量的顺序去解读的.  下面附上加载这个配置的源码
private void init_TraditionalXmlConfigurations() {
        String configPaths = initParams.get("config");
        if (configPaths == null) {
            configPaths = DEFAULT_CONFIGURATION_PATHS;
        }
        String[] files = configPaths.split("\\s*[,]\\s*");
        for (String file : files) {
            if (file.endsWith(".xml")) {
                if ("xwork.xml".equals(file)) {
                    configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
                } else {
                    configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
                }
            } else {
                throw new IllegalArgumentException("Invalid configuration file name");
            }
        }
    }
看到了吧?  循环, 所以如果几个配置文件里面有相同的常量名或者其他一些配置, 那么后面的会覆盖掉前面的

2、doFilter()
     doFilter是过滤器的执行方法,它拦截提交的HttpServletRequest请求,HttpServletResponse响应,作为strtus2的核心拦截器,在doFilter里面到底做了哪些工作,我们将逐行解读其源码,源码如下:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        //父类向子类转:强转为http请求、响应
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        try {
            //设置编码和国际化
            prepare.setEncodingAndLocale(request, response);
             //创建Action上下文(重点)
            prepare.createActionContext(request, response);
	    //把当前Dispatcher实例放入ThreadLocal里面
            prepare.assignDispatcherToThread();
			if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
			    //如果当前路径是排斥的路径,那么此过滤器不拦截
				chain.doFilter(request, response);
			} else {
			    //包装这个Request(如果是multipart/form-data,包装成MultiPartRequestWrapper, 否则包装成StrutsRequestWrapper)
				request = prepare.wrapRequest(request);
				//寻找Action的映射
				ActionMapping mapping = prepare.findActionMapping(request, response, true);
				if (mapping == null) {
				    //如果没有映射,则需要判断是否是静态资源
					boolean handled = execute.executeStaticResourceRequest(request, response);
					if (!handled) {
						chain.doFilter(request, response);
					}
				} else {
					execute.executeAction(request, response, mapping);
				}
			}
        } finally {
            prepare.cleanupRequest(request);
        }
    }
setEncodingAndLocale调用了dispatcher方法的prepare方法:
    /**
     * Sets the request encoding and locale on the response
     */
    public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {
        dispatcher.prepare(request, response);
    }

下面我们看下prepare方法,这个方法很简单只是设置了encoding 、locale ,做的只是一些辅助的工作:
public void prepare(HttpServletRequest request, HttpServletResponse response) {
        String encoding = null;
        if (defaultEncoding != null) {
            encoding = defaultEncoding;
        }

        Locale locale = null;
        if (defaultLocale != null) {
            locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
        }

        if (encoding != null) {
            try {
                request.setCharacterEncoding(encoding);
            } catch (Exception e) {
                LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
            }
        }

        if (locale != null) {
            response.setLocale(locale);
        }

        if (paramsWorkaroundEnabled) {
            request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
        }
    }

ActionContext创建(重点中的重点)

ActionContext是一个容器,这个容易主要存储request、session、application、parameters等相关信息.ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题。其实质是一个Map,key是标示request、session、……的字符串,值是其对应的对象:
static ThreadLocal actionContext = new ThreadLocal();
Map<String, Object> context;
下面我们看下如何创建action上下文的,代码如下:
/**
*创建Action上下文,初始化thread local
*/
public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
    ActionContext ctx;
    Integer counter = 1;
    Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
    if (oldCounter != null) {
        counter = oldCounter + 1;
    }
    //注意此处是从ThreadLocal中获取此ActionContext变量
    ActionContext oldContext = ActionContext.getContext();
    if (oldContext != null) {
        // detected existing context, so we are probably in a forward
        ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
    } else {
	    //创建ValueStack(很重要的一个东东,后面会讲到)
        ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
        stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
        //stack.getContext()返回的是一个Map<String,Object>,根据此Map构造一个ActionContext
        ctx = new ActionContext(stack.getContext());
    }
    request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
    //将ActionContext存如ThreadLocal
    ActionContext.setContext(ctx);
    return ctx;
}
面试题常见问题: 为什么在Struts2中我们不用考虑线程安全? 为什么交给Spring管理的时候我们要把Bean的scope属性设置成prototype?
答: 从上述源码中我们看到了, 每一次被Struts2的这个过滤器拦截的时候, 都会调用createActionContext方法,  无论当前是否有ActionContext实例存在, 都会new一个ActionContext, 只不过是如果是action之间转发的请求,那么new的时候会把之前的那个ActionContext放进去..  而每个ActionContext都只与自己的ThreadLocal挂钩,所以是不用考虑线程安全的. 然而后面读源码也会读到, 每次请求都会产生一个Action的实例, 而Spring默认创建实例是单例的.

上面代码中dispatcher.createContextMap,如何封装相关参数:
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
            ActionMapping mapping, ServletContext context) {

        // request map wrapping the http request objects
        Map requestMap = new RequestMap(request);

        // parameters map wrapping the http parameters.  ActionMapping parameters are now handled and applied separately
        Map params = new HashMap(request.getParameterMap());

        // session map wrapping the http session
        Map session = new SessionMap(request);

        // application map wrapping the ServletContext
        Map application = new ApplicationMap(context);
	//requestMap、params、session等Map封装成为一个上下文Map,逐个调用了map.put(Map p).
        Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);

        if (mapping != null) {
            extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
        }
        return extraContext;
}

我们简单看下RequestMap,其他的省略。RequestMap类实现了抽象Map,故其本身是一个Map,主要方法实现:
//map的get实现
public Object get(Object key) {
    return request.getAttribute(key.toString());
}
//map的put实现
public Object put(Object key, Object value) {
    Object oldValue = get(key);
    entries = null;
    request.setAttribute(key.toString(), value);
    return oldValue;
}
下面是源码展示了如何执行Action控制器:
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
    dispatcher.serviceAction(request, response, servletContext, mapping);
}

    public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
                              ActionMapping mapping) throws ServletException {
	//封装执行的上下文环境,主要讲相关信息存储入map
        Map<String, Object> extraContext = createContextMap(request, response, mapping, context);

        // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
        ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
        boolean nullStack = stack == null;
	//这里再次强调下ValueStack的重要性(Struts2是获取--->有就直接put,没有就新建一个再put)
        if (nullStack) {
            ActionContext ctx = ActionContext.getContext();
            if (ctx != null) {
                stack = ctx.getValueStack();
            }
        }
        if (stack != null) {
            extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
        }

        String timerKey = "Handling request from Dispatcher";
        try {
            UtilTimerStack.push(timerKey);
            //获取命名空间
            String namespace = mapping.getNamespace();
            //获取action配置的name属性
            String name = mapping.getName();
            //获取action配置的method属性
            String method = mapping.getMethod();

            Configuration config = configurationManager.getConfiguration();
            //根据执行上下文参数,命名空间,名称等创建用户自定义Action的代理对象
            ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                    namespace, name, method, extraContext, true, false);

            request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

            // if the ActionMapping says to go straight to a result, do it!
    				//执行execute方法,并转向结果
            if (mapping.getResult() != null) {
                Result result = mapping.getResult();
                result.execute(proxy.getInvocation());
            } else {
                proxy.execute();
            }

            // If there was a previous value stack then set it back onto the request
            if (!nullStack) {
                request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
            }
        } catch (ConfigurationException e) {
        	// WW-2874 Only log error if in devMode
        	if(devMode) {
                String reqStr = request.getRequestURI();
                if (request.getQueryString() != null) {
                    reqStr = reqStr + "?" + request.getQueryString();
                }
                LOG.error("Could not find action or result\n" + reqStr, e);
            }
        	else {
        		LOG.warn("Could not find action or result", e);
        	}
            sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
        } catch (Exception e) {
            sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        } finally {
            UtilTimerStack.pop(timerKey);
        }
    }
也许有人会有疑问了, 框架是如何解析Struts.xml的?如何将URL与action映射匹配?
上文有提到struts配置文件的加载顺序, 再次拿出Dispatcher.init_TraditionalXmlConfigurations()方法源码
private void init_TraditionalXmlConfigurations() {
        String configPaths = initParams.get("config");
        if (configPaths == null) {
            configPaths = DEFAULT_CONFIGURATION_PATHS;
        }
        String[] files = configPaths.split("\\s*[,]\\s*");
        for (String file : files) {
            if (file.endsWith(".xml")) {
                if ("xwork.xml".equals(file)) {
                    configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
                } else {
                    configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
                }
            } else {
                throw new IllegalArgumentException("Invalid configuration file name");
            }
        }
    }
请大家注意两个地方  createXmlConfigurationProvider和createStrutsXmlConfigurationProvider.  根据经验判断我要去读两个类的源码

XmlConfigurationProvider和StrutsXmlConfigurationProvider,  从源码中我看到
public class StrutsXmlConfigurationProvider extends XmlConfigurationProvider
是继承的关系, 所以我们先研究StrutsXmlConfigurationProvider
我看到了register方法
public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
        if (servletContext != null && !containerBuilder.contains(ServletContext.class)) {
            containerBuilder.factory(ServletContext.class, new Factory<ServletContext>() {
                public ServletContext create(Context context) throws Exception {
                    return servletContext;
                }
            });
        }
        super.register(containerBuilder, props);
    }

这个里面调用了父类的

public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
        if (LOG.isInfoEnabled()) {
            LOG.info("Parsing configuration file [" + configFileName + "]");
        }
        Map<String, Node> loadedBeans = new HashMap<String, Node>();
        for (Document doc : documents) {
            Element rootElement = doc.getDocumentElement();
            NodeList children = rootElement.getChildNodes();
            int childSize = children.getLength();

            for (int i = 0; i < childSize; i++) {
                Node childNode = children.item(i);

                if (childNode instanceof Element) {
                    Element child = (Element) childNode;

                    final String nodeName = child.getNodeName();
                    if ("bean".equals(nodeName)) {
					    //获取bean
                        String type = child.getAttribute("type");
                        String name = child.getAttribute("name");
                        String impl = child.getAttribute("class");
                        String onlyStatic = child.getAttribute("static");
                        String scopeStr = child.getAttribute("scope");
                        boolean optional = "true".equals(child.getAttribute("optional"));
                        Scope scope = Scope.SINGLETON;
                        if ("default".equals(scopeStr)) {
                            scope = Scope.DEFAULT;
                        } else if ("request".equals(scopeStr)) {
                            scope = Scope.REQUEST;
                        } else if ("session".equals(scopeStr)) {
                            scope = Scope.SESSION;
                        } else if ("singleton".equals(scopeStr)) {
                            scope = Scope.SINGLETON;
                        } else if ("thread".equals(scopeStr)) {
                            scope = Scope.THREAD;
                        }

                        if (StringUtils.isEmpty(name)) {
                            name = Container.DEFAULT_NAME;
                        }

                        try {
                            Class cimpl = ClassLoaderUtil.loadClass(impl, getClass());
                            Class ctype = cimpl;
                            if (StringUtils.isNotEmpty(type)) {
                                ctype = ClassLoaderUtil.loadClass(type, getClass());
                            }
                            if ("true".equals(onlyStatic)) {
                                // Force loading of class to detect no class def found exceptions
                                cimpl.getDeclaredClasses();
                                containerBuilder.injectStatics(cimpl);
                            } else {
                                if (containerBuilder.contains(ctype, name)) {
                                    Location loc = LocationUtils.getLocation(loadedBeans.get(ctype.getName() + name));
                                    if (throwExceptionOnDuplicateBeans) {
                                        throw new ConfigurationException("Bean type " + ctype + " with the name " +
                                                name + " has already been loaded by " + loc, child);
                                    }
                                }

                                // Force loading of class to detect no class def found exceptions
                                cimpl.getDeclaredConstructors();

                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Loaded type:" + type + " name:" + name + " impl:" + impl);
                                }
                                containerBuilder.factory(ctype, name, new LocatableFactory(name, ctype, cimpl, scope, childNode), scope);
                            }
                            loadedBeans.put(ctype.getName() + name, child);
                        } catch (Throwable ex) {
                            if (!optional) {
                                throw new ConfigurationException("Unable to load bean: type:" + type + " class:" + impl, ex, childNode);
                            } else {
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Unable to load optional class: #0", impl);
                                }
                            }
                        }
                    } else if ("constant".equals(nodeName)) {
		        //获取常量
                        String name = child.getAttribute("name");
                        String value = child.getAttribute("value");
                        props.setProperty(name, value, childNode);
                    } else if (nodeName.equals("unknown-handler-stack")) {
                        List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<UnknownHandlerConfig>();
                        NodeList unknownHandlers = child.getElementsByTagName("unknown-handler-ref");
                        int unknownHandlersSize = unknownHandlers.getLength();

                        for (int k = 0; k < unknownHandlersSize; k++) {
                            Element unknownHandler = (Element) unknownHandlers.item(k);
                            unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler.getAttribute("name")));
                        }

                        if (!unknownHandlerStack.isEmpty())
                            configuration.setUnknownHandlerStack(unknownHandlerStack);
                    }
                }
            }
        }
    }

父类的register主要是解析bean标签和常量标签的, XmlConfigurationProvider类里面有一系列的load方法.就是解析struts.xml等配置文件的package信息,action信息.这里就不累赘的贴代码了. 大家自己下去看看就行了! 关键的我带着大家看看addAction方法
   protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
        String name = actionElement.getAttribute("name");//获取name属性
        String className = actionElement.getAttribute("class");//获取class属性
        String methodName = actionElement.getAttribute("method");//获取method属性
        Location location = DomHelper.getLocationObject(actionElement);//获取location

        if (location == null) {
            LOG.warn("location null for " + className);
        }
        //methodName should be null if it's not set
        methodName = (methodName.trim().length() > 0) ? methodName.trim() : null;

        // if there isnt a class name specified for an <action/> then try to
        // use the default-class-ref from the <package/>
	//为什么不配class默认是ActionSupport.?这里一目了然
        if (StringUtils.isEmpty(className)) {
            // if there is a package default-class-ref use that, otherwise use action support
           /* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) {
                className = packageContext.getDefaultClassRef();
            } else {
                className = ActionSupport.class.getName();
            }*/

        } else {
            if (!verifyAction(className, name, location)) {
                if (LOG.isErrorEnabled())
                    LOG.error("Unable to verify action [#0] with class [#1], from [#2]", name, className, location.toString());
                return;
            }
        }



        Map<String, ResultConfig> results;
        try {
            results = buildResults(actionElement, packageContext);
        } catch (ConfigurationException e) {
            throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
        }
        //拦截器列表
        List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
        //异常映射列表
        List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
        ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className)
                .methodName(methodName)
                .addResultConfigs(results)
                .addInterceptors(interceptorList)
                .addExceptionMappings(exceptionMappings)
                .addParams(XmlHelper.getParams(actionElement))
                .location(location)
                .build();
        packageContext.addActionConfig(name, actionConfig);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Loaded " + (StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "") + name + " in '" + packageContext.getName() + "' package:" + actionConfig);
        }
    }
strtus2-core-2.3.16.3.jar中看到struts-2.3.dtd
<!ELEMENT action ((param|result|interceptor-ref|exception-mapping)*,allowed-methods?)>
<!ATTLIST action
    name CDATA #REQUIRED
    class CDATA #IMPLIED
    method CDATA #IMPLIED
    converter CDATA #IMPLIED
>

到此为止, Struts2核心源码解读工作已经基本OK了.大家也大致了解了Struts2的工作原理.  后面有关运用Struts2做程序的时候,还会根据实际情况解读相关的源码的.敬请期待.



猜你喜欢

转载自blog.csdn.net/izard999/article/details/40143439