`
aben6448
  • 浏览: 25398 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts - ActionServlet

阅读更多
控制器组件:



      ActionServlet:负责处理接受用户请求,是一个Servlet,负责初始化和清理struts使用的资源,初始化的时候,先根据 servlet的init-param载入application的config,ActionServlet会去枚举全部的名称为config的 init-param,去搜素以config/开始的init-param,struts会根据这些init-pram去载入配置文件,并且根据 init-param的config/后面的字符串去指定相应ModuleConfig实例的prefix字段.这非常重要,因为 ActionServlet怎么样去决定那一个module将会去处理请求.每一个request都会调用process方法,ActionServlet中的process方法很简单,仅仅决定哪个Module应该处理request,然后调用相应模块的 RequestProcessor's process方法,会将参数request和response传递给此方法.



      ActionServlet的javadoc:



    * The user interface will generally be created with server pages, which will not themselves contain any business logic. These pages represent the "view" component of an MVC architecture.//用户界面不包含任何业务,这些页面 View

    * Forms and hyperlinks in the user interface that require business logic to be executed will be submitted to a request URI that is mapped to this servlet.//表单和超链接 请求被相应的URI映射的servlet 处理业务

    * There can be one instance of this servlet class, which receives and processes all requests that change the state of a user's interaction with the application. The servlet delegates the handling of a request to a RequestProcessor object. This component represents the "controller" component of an MVC architecture.

    * The RequestProcessor selects and invokes an Action class to perform the requested business logic, or delegates the response to another resource...RequestProcessor选择和调用Action去执行用户的业务逻辑,委托相应给另一个资源。

    * The Action classes can manipulate the state of the application's interaction with the user, typically by creating or modifying JavaBeans that are stored as request or session attributes (depending on how long they need to be available). Such JavaBeans represent the "model" component of an MVC architecture.Action可以轻松的和程序的状态交互,很典型的,创建和访问存储在request或者seseion范围内的 JavaBeans,有那次JavaBeans在MVC中扮演Model

    * Instead of producing the next page of the user interface directly, Action classes generally return an ActionForward to indicate which resource should handle the response. If the Action does not return null, the RequestProcessor forwards or redirects to the specified resource (by utilizing RequestDispatcher.forward or Response.sendRedirect ) so as to produce the next page of the user interface。



The standard version of RequestsProcessor implements the following logic for each incoming HTTP request. You can override some or all of this functionality by subclassing this object and implementing your own version of the processing.



    * Identify, from the incoming request URI, the substring that will be used to select an action procedure.

    * Use this substring to map to the Java class name of the corresponding action class (an implementation of the Action interface).

    * If this is the first request for a particular Action class, instantiate an instance of that class and cache it for future use.

    * Optionally populate the properties of an ActionForm bean associated with this mapping.

    * Call the execute method of this Action class, passing on a reference to the mapping that was used, the relevant form-bean (if any), and the request and the response that were passed to the controller by the servlet container (thereby providing access to any specialized properties of the mapping itself as well as to the ServletContext).



The standard version of ActionServlet is configured based on the following servlet initialization parameters, which you will specify in the web application deployment descriptor (/WEB-INF/web.xml ) for your application. Subclasses that specialize this servlet are free to define additional initialization parameters.



    * config - Comma-separated list of context-relative path(s) to the XML resource(s) containing the configuration information for the default module. (Multiple files support since Struts 1.1) [/WEB-INF/struts-config.xml].

    * config/${module} - Comma-separated list of Context-relative path(s) to the XML resource(s) containing the configuration information for the module that will use the specified prefix (/${module}). This can be repeated as many times as required for multiple modules. (Since Struts 1.1)

    * configFactory - The Java class name of the ModuleConfigFactory used to create the implementation of the ModuleConfig interface. [org.apache.struts.config.impl.DefaultModuleConfigFactory]

    * convertNull - Force simulation of the Struts 1.0 behavior when populating forms. If set to true, the numeric Java wrapper class types (like java.lang.Integer ) will default to null (rather than 0). (Since Struts 1.1) [false]

    * rulesets - Comma-delimited list of fully qualified classnames of additional org.apache.commons.digester.RuleSet instances that should be added to the Digester that will be processing struts-config.xml files. By default, only the RuleSet for the standard configuration elements is loaded. (Since Struts 1.1)

    * validating - Should we use a validating XML parser to process the configuration file (strongly recommended)? [true]





ActionServlet的源码分析:



1、属性:







    
//       默认配置文件
       protected String config = "/WEB-INF/struts-config.xml";


//       指定根据配置文件生成ModuleConfig对象的Digester(apache解析Xml文档的组件,在Commons包)
       protected Digester configDigester = null;


//      是否转换为java的包装类型,如int转换为Integer类 null
      protected boolean convertNull = false;


//     在相应module存储的jdbc数据源,如果有,存储在application范围内。
     protected FastHashMap dataSources = new FastHashMap();


//     存储struts内部的消息资源
     protected MessageResources internal = null;


//     处理所有请求的RequestProcessor实例
     protected RequestProcessor processor = null;
//     在web.xml中映射的URI 模式
     protected String servletMapping = null;

     //  在web.xml中映射的servlet名称
     protected String servletName = null;








2、方法:



    *     init方法(覆盖基类的init):





 //初始化内部资源(org.apache.struts.action.ActionResources.properties)存储在属性//internal中
             initInternal();
             //根据Servlet的配置信息 config文件和convetnull
            initOther();
            //解析web.xml 把servletName存储在application scope内。
            initServlet();
             
            //将ActionServlet存储在application scope内
            getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
            
            //初始化ModuleConfig's factory
            initModuleConfigFactory();
            // Initialize modules as needed,加载默认模块的struts-config.xml
            ModuleConfig moduleConfig = initModuleConfig("", config);
            initModuleMessageResources(moduleConfig);
            initModuleDataSources(moduleConfig);
            initModulePlugIns(moduleConfig);
            moduleConfig.freeze();
            
            //读取init-param config 加载其他moduleConfig
            //所以的moduleConfig对象均存入applications scope
            //存储的key为Globals.MODULE_KEY + prefix
            Enumeration names = getServletConfig().getInitParameterNames();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                if (!name.startsWith("config/")) {
                    continue;
                }
                String prefix = name.substring(6);
                //根据prefix初始化moduleConfig
                moduleConfig = initModuleConfig
                    (prefix, getServletConfig().getInitParameter(name));
                //根据moduleConfig初始化资源,并存入application scope
                initModuleMessageResources(moduleConfig);
                //初始化dataSource
                initModuleDataSources(moduleConfig);
                //初始化plugins
                initModulePlugIns(moduleConfig);
                moduleConfig.freeze();
            }
    
            //存储所有的prefix的List到application scope中
            this.initModulePrefixes(this.getServletContext());
            //清理digester
            this.destroyConfigDigester();






    *  process方法



      接受get或者post请求后执行这个方法




 
protected void process(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
      //将相应的moduleConfig对象和资源对象放入request scope
        ModuleUtils.getInstance().selectModule(request, getServletContext());
        //获取相应module的ModuleConfig对象
        ModuleConfig config = getModuleConfig(request);
       //根据moduleConfig对象获取此module的RequestProcessor
        RequestProcessor processor = getProcessorForModule(config);
        //如果此moduleConfig不在application scope则新创建一个
        if (processor == null) {
           processor = getRequestProcessor(config);
        }

        //进入RequestProcessor 的process方法
        processor.process(request, response);
 

    }








接下来总结RequestProcessor













分享到:
评论

相关推荐

    struts-1.3.8-all.zip

    使用Struts 1.3.8时,开发者需要配置web.xml和struts-config.xml,定义ActionServlet、Action、ActionForm等元素。此外,还需要编写ActionForm类,实现业务逻辑的Action类,以及使用JSP和Struts标签创建用户界面。 ...

    jakarta-struts-1.1.zip_jakarta-Struts-1_jakarta-struts 1_jakarta

    `struts-documentation.war`文件是一个预打包的Web应用,通常用于部署到Servlet容器,如Tomcat或Jetty。这个WAR文件内含有Struts框架的API文档,开发者可以通过解压并部署这个WAR文件,在Web服务器上访问到Struts的...

    struts-1.2.9-lib.zip和struts-1.2.9-src.zip

    5. **Servlet和JSP API**:由于Struts是建立在Servlet和JSP之上的,所以这个库包中也会包含相应的API库,如`servlet-api.jar`和`jsp-api.jar`。 "struts-1.2.9-src.zip"则是Struts 1.2.9的源代码包,对开发者来说...

    struts-1.3.8-lib(new)

    1. **struts-core.jar**:这是Struts的核心库,包含了Action类、Form Beans、RequestProcessor以及Dispatcher等核心组件。 2. **struts-bean.jar**:提供Bean相关的标签库,如logic和html标签。 3. **struts-...

    Struts-Action核心代码

    在Struts中,`ActionServlet`是关键组件之一,它是Struts框架与Servlet容器之间的桥梁。 **ActionServlet** 是Struts框架的核心控制器,它是一个定制的Servlet,负责处理所有的HTTP请求。当用户发起一个请求时,...

    org.springframework.web.struts-sources-3.0.4.RELEASE.jar

    Spring提供了一个名为`SpringModuleServlet`的类,它是`ActionServlet`的子类,负责初始化Spring的ApplicationContext,并将Struts的ActionForm对象与Spring的Bean进行关联。 2. **依赖注入(DI)** `org.spring...

    struts-1.3.8-all&1.3.10

    Struts 1.x是Struts的第一个主要版本,它的核心组件包括ActionServlet、ActionForm、ActionMapping和Tiles等。以下是对这两个版本及其相关知识点的详细解释: 1. **ActionServlet**:这是Struts的核心控制器,它是...

    struts-1.3.8-all.jar

    1. **ActionServlet**:这是 Struts 框架的入口点,它是一个实现了 `Servlet` 接口的控制器,负责处理 HTTP 请求,将请求转发到相应的业务逻辑组件。 2. **ActionForm**:这个类用于封装表单数据,从客户端提交的...

    struts-1.2.9-bin 及 struts-1.2.9-src

    1. **struts.jar**:核心库,实现了Struts框架的基本功能,如ActionServlet、Form Beans、ActionMapping等。 2. **commons-logging.jar**:Apache Commons Logging,提供日志服务,允许开发者选择不同的日志实现。 3...

    struts-2.3.32-all

    2. **Dispatcher Servlet**: Struts 2的核心组件,负责接收HTTP请求,解析请求参数,并调用Action来处理请求。 3. **Action Mapping**: 配置文件(通常为struts.xml)中定义了Action与URL的映射关系,以及Action...

    struts-template.rar_.war文件_struts-templa

    6. **Dispatcher**:Struts 使用ActionServlet作为控制器,它是一个前端控制器,负责调度请求到相应的Action。 7. **Tiles**:Struts 还提供了Tiles框架,它允许开发者创建可重用的页面布局,增强了视图的复用性和...

    struts-servlet关于管理员注册和登录的整合

    Servlet或Struts的Action可以负责这一过程。先从表单中获取用户名和密码,然后通过查询数据库来验证这些信息。如果匹配成功,设置session属性表示用户已登录,否则返回错误消息。 在Struts配置文件(struts.xml)中...

    struts-1.3.8.rar

    1. **ActionServlet**:这是一个定制的 Servlet,负责处理 HTTP 请求并调用相应的 Action 类。 2. **Action**:Action 类是业务逻辑的核心,接收请求,处理数据,并通过 ActionForward 指定响应的视图。 3. **...

    struts-2.3.15.3源码

    关于删除的lib库,Struts 2依赖于一系列的第三方库,包括Apache Commons、XWork、Freemarker、Java Servlet API等。这些库提供了诸如依赖注入、AOP(面向切面编程)、I18N(国际化)、数据校验等功能。在开发环境中...

    jakarta-struts-1.2.4-src.zip_jakarta struts 1_jakarta struts-1.1

    1. **ActionServlet**:作为Struts框架的核心组件,ActionServlet负责处理HTTP请求,并将它们转发给相应的Action对象进行业务逻辑处理。 2. **ActionForm**:用于在Controller层和View层之间传递数据。用户填写表单...

    struts-core-1.3.10.jar.zip

    1. **ActionServlet**:这是Struts框架的入口点,它是一个Servlet,负责接收HTTP请求并调度到相应的Action,从而执行业务逻辑。 2. **ActionForm**:表单Bean,用于收集用户输入的数据,与JSP页面中的表单元素相...

    Struts-in-Action代码

    10. **Struts与Servlets的关系**:Struts是建立在Servlet和JSP之上的框架,它对Servlet API进行了抽象,简化了Web应用的开发。通过FilterDispatcher(Struts 1)或FrontController(Struts 2)实现请求的分发。 ...

    struts-1.3.9-lib.zip

    - ActionServlet拦截请求,根据struts-config.xml中的配置找到对应的ActionMapping。 - 根据ActionMapping,调用相应的ActionForm实例,填充表单数据。 - Action执行业务逻辑,可能涉及数据库操作或其他服务调用...

    Struts 源码学习之ActionServlet

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml, /WEB-INF/struts-config-Wildcard.xml ...

Global site tag (gtag.js) - Google Analytics