爪哇東南的自留地

          學習探討開源和web開發

          導航

          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          life

          technique

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          Struts1.1源碼解析

          Struts1.1b3部分源代碼分析.
          作者:???? 文章來源:
          訪問次數: 次??? 加入時間:2006-05-12
          ?
          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


          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的介紹參看我的另外一篇文章
          *
          Initialize the application configuration information for the
          * specified module.


          *
          * @param prefix Module prefix for this module
          * @param paths Comma-separated list of context-relative resource path(s)
          * for this modules's configuration resource(s)
          *
          * @exception ServletException if initialization cannot be performed
          * @since Struts 1.1
          */
          protected ModuleConfig initModuleConfig
          (String prefix, String paths) throws ServletException {

          if (log.isDebugEnabled()) {
          log.debug("Initializing module path '" + prefix +
          "' configuration from '" + paths + "'");
          }

          // Parse the configuration for this module
          ModuleConfig config = null;
          InputStream input = null;
          String mapping = null;
          try {
          //工廠方法創建ModuleConfig標記為:prefix
          ModuleConfigFactory factoryObject =
          ModuleConfigFactory.createFactory();
          config = factoryObject.createModuleConfig(prefix);

          // Support for module-wide ActionMapping type override
          mapping = getServletConfig().getInitParameter("mapping");
          if (mapping != null) {
          config.setActionMappingClass(mapping);
          }

          // Configure the Digester instance we will use
          //得到解析XML的digester
          Digester digester = initConfigDigester();

          // Process each specified resource path
          while (paths.length() > 0) {
          //開始解析指定路徑文件名的文件
          digester.push(config);
          String path = null;
          //文件是否為多個并且使用","分割
          int comma = paths.indexOf(',');
          //存在多個配置文件
          if (comma >= 0) {
          //先解析第一個
          path = paths.substring(0, comma).trim();
          //paths存放剩余的文件名下次再處理
          paths = paths.substring(comma + 1);
          } else {
          //當前只存在一個
          path = paths.trim();
          //沒有剩余,下次循環根據上面一句將會在唯一的循環退出點"point break"
          //退出循環
          paths = "";
          }
          //全部解析完成跳出循環
          //point break
          if (path.length() < 1) {
          break;
          }
          //根據文件名取文件
          URL url = getServletContext().getResource(path);
          InputSource is = new InputSource(url.toExternalForm());
          input = getServletContext().getResourceAsStream(path);
          is.setByteStream(input);
          digester.parse(is);
          //全局變量的形式把解析生成的ModuleConfig實例保存起來
          //如"Mark 1"處標記的"config/examples",
          //key為:"org.apache.struts.action.MODULEexamples"
          //Globals.MODULE_KEY值為:org.apache.struts.action.MODULE
          getServletContext().setAttribute
          (Globals.MODULE_KEY + prefix, config);
          input.close();
          }

          } catch (Throwable t) {
          log.error(internal.getMessage("configParse", paths), t);
          throw new UnavailableException
          (internal.getMessage("configParse", paths));
          } finally {
          if (input != null) {
          try {
          input.close();
          } catch (IOException e) {
          ;
          }
          }
          }

          // Force creation and registration of DynaActionFormClass instances
          // for all dynamic form beans we wil be using
          //根據ModuleConfig實例得到配置的FormBean的配置
          //注意:因為在Struts整個運行當中FormBean的實例要在Action的實例創建之前先創建
          //因為Action執行perform(1.1以前),execute(1.1)需要使用到FormBean
          FormBeanConfig fbs[] = config.findFormBeanConfigs();
          for (int i = 0; i < fbs.length; i++) {
          if (fbs[i].getDynamic()) {
          DynaActionFormClass.createDynaActionFormClass(fbs[i]);
          }
          }

          // Special handling for the default module (for
          // backwards compatibility only, will be removed later)
          //下面是生成一些實例
          if (prefix.length() < 1) {
          defaultControllerConfig(config);
          defaultMessageResourcesConfig(config);
          defaultFormBeansConfig(config);
          defaultForwardsConfig(config);
          defaultMappingsConfig(config);
          }

          // Return the completed configuration object
          //config.freeze(); // Now done after plugins init
          return (config);

          }

          到此初始化工作基本結束,下面將處理具體的Http請求。在Servlet協議中所有的post方法將調用
          以下方法來處理
          public void doPost(HttpServletRequest request,
          HttpServletResponse response)
          throws IOException, ServletException {}
          get方法也調用類似的方法來處理

          在Struts中post,get方法都調用同一個方法
          process(request, response);來處理具體的請求
          如下:
          /**
          * 對每一個提交過來的Action進行處理
          * Perform the standard request processing for this request, and create
          * the corresponding response.
          *
          * @param request The servlet request we are processing
          * @param response The servlet response we are creating
          *
          * @exception IOException if an input/output error occurs
          * @exception ServletException if a servlet exception is thrown
          */
          protected void process(HttpServletRequest request,
          HttpServletResponse response)
          throws IOException, ServletException {
          //設置或刪除Attribute
          RequestUtils.selectModule(request, getServletContext());
          //具體的處理交給RequestProcessor去處理HttpRequest,HttpResponse
          //這是一個很典型的設計模式
          //下面我們將詳細來分析RequestProcessor,很容易理解Struts的運行方式
          getRequestProcessor(getModuleConfig(request)).process(request, response);
          }

          三:RequestProcessor分析
          通過前面的分析我們知道Struts中對HttpRequest,HttpResponse的處理都交給RequestProcessor
          的process()方法來處理。下面我們來看看process方法
          /**
          *
          Process an HttpServletRequest and create the
          * corresponding HttpServletResponse.


          *
          * @param request The servlet request we are processing
          * @param response The servlet response we are creating
          *
          * @exception IOException if an input/output error occurs
          * @exception ServletException if a processing exception occurs
          */
          public void process(HttpServletRequest request,
          HttpServletResponse response)
          throws IOException, ServletException {

          // Wrap multipart requests with a special wrapper
          //如果是upload,返回一個MultipartRequestWrapper()
          request = processMultipart(request);

          // Identify the path component we will use to select a mapping
          //對request進行分析得到提交過來的Form Action
          String path = processPath(request, response);
          if (path == null) {
          return;
          }
          if (log.isInfoEnabled()) {
          log.info("Processing a '" + request.getMethod() +
          "' for path '" + path + "'");
          }

          // Select a Locale for the current user if requested
          //本地化處理
          processLocale(request, response);

          // Set the content type and no-caching headers if requested
          processContent(request, response);
          //設置Cache不保存
          processNoCache(request, response);

          // General purpose preprocessing hook
          if (!processPreprocess(request, response)) {
          return;
          }

          // Identify the mapping for this request
          //得到path以后,根據配置文件(struts-config.xml)的相關配置來得到一個
          //ActionMapping的實例
          //ActionMapping繼承ActionConfig
          //仔細看一下ActionMapping的代碼就能發現:
          //下面的一段將解析影射一個ActionMapping的實例
          //在ActionServlet在初始化的時候實際上已經把所有的ActionMapping的實例
          //都已經創建好了。processMapping方法實際上是從Attribute中去得到已經
          //保存好的ActionMapping的實例,可以理解為在Tomcat啟動的時候已經
          //把所有的ActionMapping保存在Attribute里面。所以在Tomcat啟動的時候
          //比較慢,如果Struts-config.xml有問題啟動就會出錯。
          /***

          type="org.apache.struts.webapp.tiles.test.TestActionTileAction">

          ?

          **/
          //****注意得到創建實例的順序
          //ActionMapping實例已經創建好了,現在從Atribute中取到
          ActionMapping mapping = processMapping(request, response, path);
          if (mapping == null) {
          return;
          }

          // Check for any role required to perform this action
          if (!processRoles(request, response, mapping)) {
          return;
          }

          // Process any ActionForm bean related to this request
          //根據mapping得到ActionForm的實例。
          //同名ActionForm在系統中只會創建一次。
          ActionForm form = processActionForm(request, response, mapping);
          //壓棧
          processPopulate(request, response, form, mapping);
          //處理校驗,調用ActionForm的validate方法
          //假如出錯將會返回到前一頁面
          //也就是說在Action還沒有創建之前就將做校驗
          if (!processValidate(request, response, form, mapping)) {
          return;
          }

          // Process a forward or include specified by this mapping
          if (!processForward(request, response, mapping)) {
          return;
          }
          if (!processInclude(request, response, mapping)) {
          return;
          }

          // Create or acquire the Action instance to process this request
          //在得到ActionMapping、ActionForm的實例后接下來得到Action實例
          //實例如果已經創建從Map里面去取如果沒有創建一個并保存在Map里面
          //對保存Action實例的Map 實現線程同步
          Action action = processActionCreate(request, response, mapping);
          if (action == null) {
          return;
          }

          // Call the Action instance itself
          //在ActionMapping、ActionForm、Action實例創建好以后
          //調用Action的execute()方法得到一個ActionForward
          //因為所有的可執行Action都必須有override Action的execute()/perform()方法
          ActionForward forward =
          processActionPerform(request, response,
          action, form, mapping);

          // Process the returned ActionForward instance
          //Action已經正常執行,執行結束后將返回到另外一個頁面
          processActionForward(request, response, forward);

          }

          仔細閱讀上面的代碼,要特別注意ActionMapping、ActionForm、Action的實例是依次創建的。
          創建完以后才去執行Action的execute()方法。為什么要依次創建ActionMapping、ActionForm
          、Action??????


          四:ModuleConfig分析
          現在我們很有必要了解一下ModuleConfig這個類,因為太多地方用到了。
          實際上ModuleConfig是一個接口有一個實現。org.apache.struts.config.impl.ModuleConfigImpl
          具體實現我就沒有不要去分析了。我們來看看這個接口。

          package org.apache.struts.config;

          /**
          *
          The collection of static configuration information that describes a
          * Struts-based module. Multiple modules are identified by
          * a prefix at the beginning of the context
          * relative portion of the request URI. If no module prefix can be
          * matched, the default configuration (with a prefix equal to a zero-length
          * string) is selected, which is elegantly backwards compatible with the
          * previous Struts behavior that only supported one module.


          *
          * @author Rob Leland
          * @version $Revision: 1.2 $ $Date: 2002/12/22 05:31:14 $
          * @since Struts 1.1
          */
          public interface ModuleConfig {
          /**
          * Has this module been completely configured yet. Once this flag
          * has been set, any attempt to modify the configuration will return an
          * IllegalStateException.
          */
          boolean getConfigured();

          /**
          * The controller configuration object for this module.
          */
          ControllerConfig getControllerConfig();
          /**
          * The controller configuration object for this module.
          * @param cc The controller configuration object for this module.
          */

          void setControllerConfig(ControllerConfig cc);

          /**
          * The prefix of the context-relative portion of the request URI, used to
          * select this configuration versus others supported by the controller
          * servlet. A configuration with a prefix of a zero-length String is the
          * default configuration for this web module.
          */
          String getPrefix();

          /**
          * The prefix of the context-relative portion of the request URI, used to
          * select this configuration versus others supported by the controller
          * servlet. A configuration with a prefix of a zero-length String is the
          * default configuration for this web module.
          */
          public void setPrefix(String prefix);
          /**
          * The default class name to be used when creating action mapping
          * instances.
          */
          String getActionMappingClass();
          /**
          * The default class name to be used when creating action mapping
          * instances.
          * @param actionMappingClass default class name to be used when creating action mapping
          * instances.
          */

          void setActionMappingClass(String actionMappingClass);

          /**
          * Add a new ActionConfig instance to the set associated
          * with this module.
          *
          * @param config The new configuration instance to be added
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void addActionConfig(ActionConfig config);

          /**
          * Add a new DataSourceConfig instance to the set associated
          * with this module.
          *
          * @param config The new configuration instance to be added
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void addDataSourceConfig(DataSourceConfig config);

          /**
          * Add a new ExceptionConfig instance to the set associated
          * with this module.
          *
          * @param config The new configuration instance to be added
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void addExceptionConfig(ExceptionConfig config);

          /**
          * Add a new FormBeanConfig instance to the set associated
          * with this module.
          *
          * @param config The new configuration instance to be added
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void addFormBeanConfig(FormBeanConfig config);

          /**
          * Add a new ForwardConfig instance to the set of global
          * forwards associated with this module.
          *
          * @param config The new configuration instance to be added
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void addForwardConfig(ForwardConfig config);

          /**
          * Add a new MessageResourcesConfig instance to the set
          * associated with this module.
          *
          * @param config The new configuration instance to be added
          *
          * @exception IllegalStateException if this module configuration
          * has been frozen
          */
          void addMessageResourcesConfig(MessageResourcesConfig config);

          /**
          * Add a newly configured {@link org.apache.struts.config.PlugInConfig} instance to the set of
          * plug-in Actions for this module.
          *
          * @param plugInConfig The new configuration instance to be added
          */
          void addPlugInConfig(PlugInConfig plugInConfig);

          /**
          * Return the action configuration for the specified path, if any;
          * otherwise return null.
          *
          * @param path Path of the action configuration to return
          */
          ActionConfig findActionConfig(String path);

          /**
          * Return the action configurations for this module. If there are
          * none, a zero-length array is returned.
          */
          ActionConfig[] findActionConfigs();

          /**
          * Return the data source configuration for the specified key, if any;
          * otherwise return null.
          *
          * @param key Key of the data source configuration to return
          */
          DataSourceConfig findDataSourceConfig(String key);

          /**
          * Return the data source configurations for this module. If there
          * are none, a zero-length array is returned.
          */
          DataSourceConfig[] findDataSourceConfigs();

          /**
          * Return the exception configuration for the specified type, if any;
          * otherwise return null.
          *
          * @param type Exception class name to find a configuration for
          */
          ExceptionConfig findExceptionConfig(String type);

          /**
          * Return the exception configurations for this module. If there
          * are none, a zero-length array is returned.
          */
          ExceptionConfig[] findExceptionConfigs();

          /**
          * Return the form bean configuration for the specified key, if any;
          * otherwise return null.
          *
          * @param name Name of the form bean configuration to return
          */
          FormBeanConfig findFormBeanConfig(String name);

          /**
          * Return the form bean configurations for this module. If there
          * are none, a zero-length array is returned.
          */
          FormBeanConfig[] findFormBeanConfigs();

          /**
          * Return the forward configuration for the specified key, if any;
          * otherwise return null.
          *
          * @param name Name of the forward configuration to return
          */
          ForwardConfig findForwardConfig(String name);

          /**
          * Return the form bean configurations for this module. If there
          * are none, a zero-length array is returned.
          */
          ForwardConfig[] findForwardConfigs();

          /**
          * Return the message resources configuration for the specified key,
          * if any; otherwise return null.
          *
          * @param key Key of the data source configuration to return
          */
          MessageResourcesConfig findMessageResourcesConfig(String key);

          /**
          * Return the message resources configurations for this module.
          * If there are none, a zero-length array is returned.
          */
          MessageResourcesConfig[] findMessageResourcesConfigs();

          /**
          * Return the configured plug-in actions for this module. If there
          * are none, a zero-length array is returned.
          */
          PlugInConfig[] findPlugInConfigs();

          /**
          * Freeze the configuration of this module. After this method
          * returns, any attempt to modify the configuration will return
          * an IllegalStateException.
          */
          void freeze();

          /**
          * Remove the specified action configuration instance.
          *
          * @param config ActionConfig instance to be removed
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void removeActionConfig(ActionConfig config);

          /**
          * Remove the specified exception configuration instance.
          *
          * @param config ActionConfig instance to be removed
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void removeExceptionConfig(ExceptionConfig config);

          /**
          * Remove the specified data source configuration instance.
          *
          * @param config DataSourceConfig instance to be removed
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void removeDataSourceConfig(DataSourceConfig config);

          /**
          * Remove the specified form bean configuration instance.
          *
          * @param config FormBeanConfig instance to be removed
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void removeFormBeanConfig(FormBeanConfig config);

          /**
          * Remove the specified forward configuration instance.
          *
          * @param config ForwardConfig instance to be removed
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void removeForwardConfig(ForwardConfig config);

          /**
          * Remove the specified message resources configuration instance.
          *
          * @param config MessageResourcesConfig instance to be removed
          *
          * @exception java.lang.IllegalStateException if this module configuration
          * has been frozen
          */
          void removeMessageResourcesConfig(MessageResourcesConfig config);
          }


          上面的注釋已經非常清晰了。我就不去浪費大家的時間了,再想仔細看就去看他的實現。
          其實主要是對HashMap()的處理。

          五:ActionMapping分析
          最后我們實現很有必要看一下ActionMapping,因為你如果想清楚的了解Struts-config.xml
          這個配置文件的作用,你應該要知道ActionMapping
          前面已經說過ActionMapping繼承ActionConfig
          以下就是ActionMapping加的四個方法。
          /**
          *
          Find and return the ExceptionConfig instance defining
          * how exceptions of the specified type should be handled. This is
          * performed by checking local and then global configurations for the
          * specified exception's class, and then looking up the superclass chain
          * (again checking local and then global configurations). If no handler
          * configuration can be found, return null.


          *
          * @param type Exception class for which to find a handler
          * @since Struts 1.1
          */
          public ExceptionConfig findException(Class type) {

          }


          /**
          *
          Find and return the ForwardConfig instance defining
          * how forwarding to the specified logical name should be handled. This is
          * performed by checking local and then global configurations for the
          * specified forwarding configuration. If no forwarding configuration
          * can be found, return null.


          *
          * @param name Logical name of the forwarding instance to be returned
          */
          public ActionForward findForward(String name) {

          }


          /**
          *
          Return the logical names of all locally defined forwards for this
          * mapping. If there are no such forwards, a zero-length array
          * is returned.
          */
          public String[] findForwards() {
          }


          /**
          *

          Create (if necessary) and return an {@link ActionForward} that
          * corresponds to the input property of this Action.
          *
          * @since Struts 1.1b2
          */
          public ActionForward getInputForward() {

          }

          還是看以下他的基類ActionConfig吧
          public class ActionConfig implements Serializable {

          /**
          * Has configuration of this component been completed?
          */
          protected boolean configured = false;

          /**
          * The set of exception handling configurations for this
          * action, if any, keyed by the type property.
          */
          protected HashMap exceptions = new HashMap();

          /**
          * The set of local forward configurations for this action, if any,
          * keyed by the name property.
          */
          protected HashMap forwards = new HashMap();

          /**
          * The module configuration with which we are associated.
          */
          protected ModuleConfig moduleConfig = null;

          /**
          * The request-scope or session-scope attribute name under which our
          * form bean is accessed, if it is different from the form bean's
          * specified name.
          */
          protected String attribute = null;

          /**
          * Context-relative path of the web application resource that will process
          * this request via RequestDispatcher.forward(), instead of instantiating
          * and calling the Action class specified by "type".
          * Exactly one of forward, include, or
          * type must be specified.
          */
          protected String forward = null;

          /**
          * Context-relative path of the web application resource that will process
          * this request via RequestDispatcher.include(), instead of instantiating
          * and calling the Action class specified by "type".
          * Exactly one of forward, include, or
          * type must be specified.
          */
          protected String include = null;

          /**
          * Context-relative path of the input form to which control should be
          * returned if a validation error is encountered. Required if "name"
          * is specified and the input bean returns validation errors.
          */
          protected String input = null;

          /**
          * Fully qualified Java class name of the
          * MultipartRequestHandler implementation class used to
          * process multi-part request data for this Action.
          */
          protected String multipartClass = null;

          /**
          * Name of the form bean, if any, associated with this Action.
          */
          protected String name = null;

          /**
          * General purpose configuration parameter that can be used to pass
          * extra iunformation to the Action instance selected by this Action.
          * Struts does not itself use this value in any way.
          */
          protected String parameter = null;

          /**
          * Context-relative path of the submitted request, starting with a
          * slash ("/") character, and omitting any filename extension if
          * extension mapping is being used.
          */
          protected String path = null;

          /**
          * Prefix used to match request parameter names to form ben property
          * names, if any.
          */
          protected String prefix = null;

          /**
          * Comma-delimited list of security role names allowed to request
          * this Action.
          */
          protected String roles = null;

          /**
          * Identifier of the scope ("request" or "session") within which
          * our form bean is accessed, if any.
          */
          protected String scope = "session";

          /**
          * Suffix used to match request parameter names to form bean property
          * names, if any.
          */
          protected String suffix = null;

          /**
          * Fully qualified Java class name of the Action class
          * to be used to process requests for this mapping if the
          * forward and include properties are not set.
          * Exactly one of forward, include, or
          * type must be specified.
          */
          protected String type = null;

          /**
          * Should the validate() method of the form bean associated
          * with this action be called?
          */
          protected boolean validate = true;
          }

          其實ActionConfig是一個很典型的ValueObject.所以其他的get/set方法我就不寫出來了。
          看這個代碼一定要和struts-config.xml一起來看,根據struts-config.xml去找找
          每一段配置文件最終要生成一個ActionConfig,他們之間的對應關系。
          如果你想擴展Struts,ActionMapping估計你一定要修改。還有ActionServlet你也要修改。

          六:結束語
          分析了一些代碼下面做一些概述。先來整體的了解一下Struts的工作流程.
          在實現一個基于Struts的運用之前我們首先是做環境設置,Struts正常工作需要至少兩個
          配置文件web.xml,struts-config.xml.
          web.xml告訴App Server所有以.do結尾的請求最終提交給ActionServlet去處理。
          2就規定ActionServlet是在App Server啟動的時候
          創建的并且一直存在。
          ActionServlet在創建的時候會做如下的工作:
          保存一些后面需要使用的實例在Attribute(內存)里面。
          根據web.xml的配置解析struts-config.xml文件。
          根據struts-config.xml的配置生成ActionMapping實例并且保存。

          ActionServlet在生命周期就一直等待Http 請求
          每一個.do結尾的Http 請求都由ActionServlet先截獲然后根據請求路徑得到具體調用那
          一個Action去處理,在這之前生成、處理ActionForm。具體知道那一個Action去處理請求
          后調用Action的execute()/perform()處理完成,返回。


          ?

          posted on 2006-09-13 20:23 ericli 閱讀(1396) 評論(0)  編輯  收藏 所屬分類: Struts


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 荣成市| 津南区| 新竹市| 于田县| 礼泉县| 黄石市| 凉山| 罗山县| 新竹市| 肥东县| 图们市| 云龙县| 阿克陶县| 田阳县| 清苑县| 东丰县| 麻栗坡县| 大兴区| 上蔡县| 确山县| 虎林市| 筠连县| 江陵县| 庄河市| 定兴县| 张掖市| 墨玉县| 新建县| 石楼县| 贵州省| 淅川县| 拜城县| 新泰市| 临夏市| 永丰县| 上虞市| 舞钢市| 波密县| 嘉峪关市| 浦城县| 通江县|