1.1 protected MessageResources internal:
加载内部属性文件,org.apache.struts.action.ActionResources.properties;
1.2 protected String config:
表示struts配置文件的字符串,用逗号隔开的多个struts-config.xml配置文件的字符串,默认为"/WEB-INF/struts-config.xml"
1.3 protected String servletName = null;
ActionServlet的名字,从web.xml文件中读取
1.4 protected String servletMapping = null;
ActionServlet在web.xml文件中配置的url_pattern,一般设置为“*.do”
1.5 protected Digester configDigester = null;
用来基于struts-config.xml配置文件初始化ModuleConfigImpl对象;
1.6 protected FastHashMap dataSources = new FastHashMap();
用来存放模块的数据源,键值为data-source标记的key属性值
2.方法:
2.1public void init() throws ServletException:
2.1.1 p rotected void initInternal() throws ServletException:
初始化成员变量internal
2.1.2 p rotected void initOther() throws ServletException:
初始化成员变量config;
2.1.3 p rotected void initServlet() throws ServletException:
初始化成员变量servletName、servletMapping,一般为”*.do”,同时设置容器全局变量,方法为:getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);
2.1.4 (原子语句):
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this),即,将当前ActionServlet放进容器全局变量;
2.1.5 p rotected ModuleConfig initModuleConfig(String prefix, String paths):
该方法初始化模块的配置,其中paths为配置文件路径的字符串,如"/WEB-INF/struts-config.xml",而主模块的prefix默认为空字符串””;ModuleConfigFactory的默认实现类为org.apache.struts.config.impl.DefaultModuleConfigFactory,ModuleConfig的默认实现类为ModuleConfigImpl,该类主要用来描述一个模块的一个或多个struts-config.xml文件;通过调用方法initConfigDigester()初始化成员变量configDigester,通过调用configDigester的parse方法,初始化ModuleConfigImpl对象,并将ModuleConfigImpl对象填入Servlet上下文属性,如下:
getServletContext().setAttribute(Globals.MODULE_KEY + prefix, config);
2.1.6 p rotected void initModuleMessageResources(ModuleConfig config):
初始化资源文件,将读取配置文件的message-resources标记的资源,并放入ServletContext的属性如下:
getServletContext().setAttribute(mrcs[i].getKey() + config.getPrefix(),resources);
此时注意,如果声明了多个message-resources标记,则必须声明不同的key。
2.1.7 p rotected void initModuleDataSources(ModuleConfig config) throws ServletException:
初始化模块数据源,并放入ServletContext属性和ActionServlet成员变量dataSources中,如下:
getServletContext().setAttribute(dscs[i].getKey() + config.getPrefix(), ds);
dataSources.put(dscs[i].getKey(), ds);
struts1.1提供了默认的数据源类为org.apache.struts.util.GenericDataSource。
2.1.8 p rotected void initModulePlugIns (ModuleConfig config) throws ServletException:
初始化模块的Plugin,并将Plugin数组存放到ServletContext属性中,如下:
getServletContext().setAttribute(Globals.PLUG_INS_KEY + config.getPrefix(), plugIns);
2.1.9 moduleConfig.freeze();
提示模块初始化完毕。
[注释]:如果当前web应用包含了多个Module,主Module的前缀为””,其它Module则可以通过在ActionServlet的初始化参数中配置,初始参数名为”config/”开头,如”config/firstSubModule”,而值则为子模块的struts配置文件路径,如”/WEB-INF/conf/firstSub-struts-config.xml”。则此Module的prefix为/firstSubModule,并通过解析配置文件初始化该ModuleConfigImpl对象。同理,可以配置多个子Module,而初始化完主Module(前缀为””)后会初始化所有子Module。
初始化过程中ServletContext设置的相关的属性列表:
1. 设置ServletContext的Globals.SERVLET_KEY属性为web.xml中ActionServlet中设置的<url-pattern>标记值,一般为“*.do”。
getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);
2.设置ServletContext的Globals.ACTION_SERVLET_KEY属性为当前ActionServlet。
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
3.设置ServletContext的Globals.MODULE_KEY + prefix属性为当前ModuleConfigImpl对象;
getServletContext().setAttribute(Globals.MODULE_KEY + prefix, config);
其中,主ModuleConfigImpl的prefix值为空字符串””,其它子ModuleConfigImpl的prefix的定义参见上述的[注释]。
4.根据MessageResourcesConfig的key属性值和config的prefix属性值设置ServletContext的属性值为当前的MessageResourcesConfig对象。
getServletContext().setAttribute(
mrcs[i].getKey() + config.getPrefix(),
resources);
5.根据DataSourceConfig对象的key属性和config的prefix属性设置为ServletContext的相关属性值设置为数据源对象。
getServletContext().setAttribute
(dscs[i].getKey() + config.getPrefix(), ds);
6.将配置的plugIns添加到ServletContext属性集中。
getServletContext().setAttribute(Globals.PLUG_INS_KEY + config.getPrefix(), plugIns);