龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

Struts1.1b3部分源代码分析.

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
Struts1.1部分源代码分析 一:说明 本文针对Struts1.1b3做分析,主要希望通过对源代码的分析阐述Struts1.1的工作方式。 本文不适合初学者参考,适合具有一定基于Struts开发的程序员参考。

  Struts1.1部分源代码分析
  一:说明
   本文针对Struts1.1b3做分析,主要希望通过对源代码的分析阐述Struts1.1的工作方式。
  本文不适合初学者参考,适合具有一定基于Struts开发的程序员参考。
  下面的描述;里面将会对ActionServlet,RequestProcessor,ModuleConfig等几个类做一些
  说明。以注释源代码的方式,说明取工作流程。
  非凡申明:Struts1.1代码版权属于Apache遵循The Apache Software License, Version 1.1.
  本文版权属于孤魂一笑个人所有,任何个人或组织希望转载,请与我联系。并获得我的授权
  方可转载。
  
  二:ActionServlet分析
   我们先来看一下使用Struts的配置文件。
  
  
   action
   org.apache.struts.action.ActionServlet
   org.apache.struts.tiles.ActionComponentServlet -->
  
   definitions-config
   /WEB-INF/tiles-defs.XML,/WEB-INF/tiles-tests-defs.xml,/WEB-INF/tiles-tutorial-defs.xml,
   /WEB-INF/tiles-examples-defs.xml

  

  
   definitions-debug
   0
  

  
   definitions-parser-details
   0
  

  
   definitions-parser-validate
   true
  

  
  
   config
   /WEB-INF/struts-config.xml
  

  
  
   config/examples
   /WEB-INF/struts-examples-config.xml
  

  
  
   config/test
   /WEB-INF/struts-tests-config.xml
  

  
  
   config/tutorial
   /WEB-INF/struts-tutorial-config.xml
  

  
   validate
   true
  

  
   debug
   2
  

  
   detail
   2
  

  
  
   application
   org.apache.struts.webapp.tiles.dev1-1.ApplicationResources
  

  
   2
  

  
  
  
   action
   *.do
  

  
   接下来我们来看一下ActionServlet的具体使用
   Javax.servlet.http.HttpServlet
  
   -->org.apache.struts.action.ActionServlet
   所以本质上ActionServlet是一个普通的servlet,负责处理.do为后缀的Http请求.
   servlet在执行doGet(),doPost(),之前先调用init(),
   以下我们先分析一下init()方法
   /**
   * Initialize this servlet. Most of the processing has been factored into
   * support methods so that you can override particular functionality at a
   * fairly granular level.
  
   * servlet初始化操作,注重初始化顺序
   * @exception ServletException if we cannot configure ourselves correctly
   */
   public void init() throws ServletException {
   //注重初始化的顺序
   //Initialize our internal MessageResources bundle
   initInternal();
   //Initialize other global characteristics of the controller servlet
   //处理一些全局变量的设置如:debug,detail等
   initOther();
   //Initialize the servlet mapping under which our controller servlet
   //is being Accessed. This will be used in the &Html:form>
   //tag to generate correct destination URLs for form submissions
   //主要是注册DTD文件以及解析web.xml关于ActionServlet的配置。如后缀名等.
   // Configure the processing rules that we need
   // digester.addCallMethod("web-app/servlet-mapping",
   // "addServletMapping", 2);
   // digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
   // digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);
   //initServlet()的上面一段将把Struts默认的后缀名从web.xml中解析得到
   //也就是web.xml中的如下配置:
   //
   //action
   //*.do
   //默认以.do结尾的请求都将由Struts来处理,你可以自己修改
   //

   initServlet();
  
   // Initialize modules as needed
   //在Attribute中保存类实例
   getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
   //根据配置文件生成ModuleConfig,这是很重要的一步.下面会专门分析
   //在tiles的配置中先解析注释为"Mark 0"的一个配置文件:/WEB-INF/struts-config.xml
   //使用initModuleConfig方法解析XML文件.
   //参数为prefix:"",paths:"/WEB-INF/struts-config.xml"
   ModuleConfig moduleConfig = initModuleConfig("", config);
   //初始化Message
   initModuleMessageResources(moduleConfig);
   //初始化JDBC DataSource
   initModuleDataSources(moduleConfig);
   //初始化PlunIn
   initModulePlugIns(moduleConfig);
  
   moduleConfig.freeze();
   //在Struts1.1以后可以使用多个配置文件,在解析完默认的配置文件也就是上面提到的
   //注释为"Mark 0"的一个配置文件:/WEB-INF/struts-config.xml后解析其他的配置文件
   Enumeration names = getServletConfig().getInitParameterNames();
   //依次解析注释为"Mark 1"、"Mark 2"、"Mark 3"对应配置文件
   while (names.hasMoreElements()) {
   //每一个配置文件的文件名
   String name = (String) names.nextElement();
   if (!name.startsWith("config/")) {
   continue;
   }
   //
   String prefix = name.substring(6);
   moduleConfig = initModuleConfig
   (prefix, getServletConfig().getInitParameter(name));
  
   initModuleMessageResources(moduleConfig);
   initModuleDataSources(moduleConfig);
   initModulePlugIns(moduleConfig);
   moduleConfig.freeze();
   }
   destroyConfigDigester();
  
   }
   /**
   * 此方法使用Digester解析XML,关于使用Digester的介绍参看我的另外一篇文章
   *


  

精彩图集

赞助商链接