//struts1.1
public void init() throws ServletException {
/*
1. Initialize the framework’s internal message bundle. These messages are used to
output informational, warning, and error messages to the log files. The
org.apache.struts.action.ActionResources bundle is used to obtain
the internal messages.
调用initInternal()方法,初始化struts框架内在的消息资源,如与系统日志相关的通知、警告和错误信息。
*/
initInternal();
/*
2. Load the initialization parameters from the web.xml file that control various
behaviors of the ActionServlet. These parameters include config, debug, and
convertNull. For information on how these and other servlet
parameters affect the behavior of an application.
调用initOther(),从web.xml文件中加载ActionServlet的初始化参数,如config,debug,convertNull等,具体参数参见“ActionServlet参数设置.doc”
*/
initOther();
/*
3. Load and initialize the servlet name and servlet mapping information from the
web.xml file. These values will be used throughout the framework, mostly by tag
libraries to output correct URL destinations when submitting HTML forms. During
this initialization, the DTDs that are used by the framework are also registered. The
DTDs are used to validate the configuration file in the next step.
调用initServlet(),从web.xml文件中加载ActionServlet的URL映射信息。此外还会注册web.xml和struts配置文件所使用的DTD文件,这些DTD文件用来验证web.xml和struts配置文件的语法
*/
initServlet();
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
/*
4. Load and initialize the Struts configuration data for the default application, which is
specified by the config initialization parameter. The default Struts configuration
file is parsed and an ApplicationConfig object is created and stored in the
ServletContext. The ApplicationConfig object for the default
application is stored in the ServletContext with a key value of
“org.apache.struts.action.APPLICATION”.
调用initModuleConfig(),加载并解析默认应用模块的struts配置文件;创建ModuleConfig对象,把它存在ServletContext中
*/
// Initialize modules as needed
ModuleConfig moduleConfig = initModuleConfig("", config);
/*
5. Each message resource that is specified in the Struts configuration file for the default
application is loaded, initialized, and stored in the ServletContext at the
appropriate location based on the key attribute specified in each <messageresources>
element. If no key is specified, the message resource will be stored at
the key value “org.apache.struts.action.MESSAGE”. Obviously only one message
resource can be stored as the default, since keys have to be unique.
调用intiModuleMessageResources(),加载并初始化默认应用模块的消息资源,创建MessageResources对象,把它存在ServletContext中
*/
initModuleMessageResources(moduleConfig);
/*
6. Next, load and initialize each data source that is has been declared in the Struts
configuration file. If no data sources are specified, this step is skipped.
调用initModuleDataSources()方法,加载并初始化默认应用模块的数据源。如果没有在struts配置文件中没有定义<data-sources>元素,就忽略这一流程
*/
initModuleDataSources(moduleConfig);
/*
7. Load and initialize each plug-in that is specified in the Struts configuration file. The
init() method will be called on each and every plug-in specified.
调用initModulePlugins(),加载并初始化默认应用模块的所有插件
*/
initModulePlugIns(moduleConfig);
/*
默认应用模块配置完毕
*/
moduleConfig.freeze();
/*
8. Once the default application has been properly initialized, the servlet init()
method will determine if there are any sub-applications specified and if so, it will
repeat steps 4 through 7 for each and every sub-application. The
ServletContext key where objects are stored will include the sub-application
suffix value, so as not to overwrite the default application values.
当默认子应用模块被成功初始化后,如果应用还包括其他子应用模块,将重复流程4-7,分别对其他子应用模块进行初始化。
*/
Enumeration names = getServletConfig().getInitParameterNames();
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();
//到此,ActionServlet初始化完毕
}
init()方法流程图如下:
评论