struts2 处理请求流程分析(结合源码)1

struts2 源码版本2.0.11.1

本文是综合网上部分人的分析成果,然后再自己结合源码进行的,分析中如有错误,请指正。

   从struts2 中的web.xml的启动配置可以看出,首先分析的是FilterDispatcher 这个过滤器类。

 

1、过滤器的初始化方法 void init(FilterConfig filterConfig)

//初始化方法
public void init(FilterConfig filterConfig) throws ServletException {
    	 this.filterConfig = filterConfig;
    	//获得默认的参数,创建dispathcher 对象
        dispatcher = createDispatcher(filterConfig);
        dispatcher.init();
       
        String param = filterConfig.getInitParameter("packages");
        String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging";
        if (param != null) {
            packages = param + " " + packages;
        }
        this.pathPrefixes = parse(packages);
    }

 

    1.1、createDispatcher(filterConfig);方法,该方法的目的是创建Dispathcher 对象

 

  protected Dispatcher createDispatcher(FilterConfig filterConfig) {
    	//读取相应过滤器的web.xml 配置
        Map<String,String> params = new HashMap<String,String>();
        for (Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements(); ) {
            String name = (String) e.nextElement();
            String value = filterConfig.getInitParameter(name);
            params.put(name, value);
        }
        //可以看出Dispatcher 类包装了ServletContext 和过滤器的web.xml 配置
        return new Dispatcher(filterConfig.getServletContext(), params);
    }

 

 

  1.2、dispatcher.init();方法,该方法对dispatcher进行了一系列的初始化工作,这个工作很重要也有点复杂,具体每个初始化的工作的流程怎样,待有空闲的时候再继续分析,网上也有人已经分析过了,如果有兴趣可参照:http://zddava.iteye.com/blog/211795

 

public void init() {
    	if (configurationManager == null) {
    		configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
    	}
    	//读取properties信息,默认的default.properties
    	init_DefaultProperties(); // [1]
    	//读取xml配置文件,默认的struts-default.xml,struts-plugin.xml,struts.xml
        init_TraditionalXmlConfigurations(); // [2]
      //读取用户自定义的struts.properties  
        init_LegacyStrutsProperties(); // [3]
       //读取FilterDispatcher的配置中所定义的actionPackages属性,传说中的Struts 2 零配置所谓的零配置
        init_ZeroConfiguration(); // [4]
      //自定义的configProviders 
        init_CustomConfigurationProviders(); // [5]
      //该功能全面被注释
        init_MethodConfigurationProvider();
      //载入FilterDispatcher传进来的initParams   
        init_FilterInitParameters() ; // [6]
      //将配置文件中的bean与具体的类映射 
        init_AliasStandardObjects() ; // [7]
      //构建一个用于依赖注射的Container对象   
      //在这里面会循环调用上面七个ConfigurationProvider的register方法   
      //其中的重点就是DefaultConfiguration的#reload()方法  
        Container container = init_PreloadConfiguration();
        init_CheckConfigurationReloading(container);
        init_CheckWebLogicWorkaround(container);
    }

 

   1.3、String param = filterConfig.getInitParameter("packages"); 以下的代码。这个步骤载入了packages标签下定义的静态资源。 读取web.xml中 的下面的配置路径还有org.apache.struts2.static,template,org.apache.struts2.interceptor.debugging这三个包空间下边的资源也会作为静态资源载入。

    <filter>
       <filter-name>struts2</filter-name>
       <filter-class>
           org.apache.struts2.dispatcher.FilterDispatcher
       </filter-class>
       <init-param>
           <param-name>packages</param-name>
           <param-value>cn.static.resource</param-value>
       </init-param>
    </filter>

 

    1.4、this.pathPrefixes = parse(packages);这个步骤是对packages 进行解析的。

  protected String[] parse(String packages) {
        if (packages == null) {
            return null;
        }
        List<String> pathPrefixes = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(packages, ", \n\t");
        while (st.hasMoreTokens()) {
            String pathPrefix = st.nextToken().replace('.', '/');
            if (!pathPrefix.endsWith("/")) {
                pathPrefix += "/";
            }
            pathPrefixes.add(pathPrefix);
        }
        return pathPrefixes.toArray(new String[pathPrefixes.size()]);
    }

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章