控制器组件:
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、属性:
2、方法:
* init方法(覆盖基类的init):
* process方法
接受get或者post请求后执行这个方法
接下来总结RequestProcessor
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