Struts1.1中ActionServlet的初始化

1.  属性:

  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;

    ActionServletweb.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

  初始化成员变量servletNameservletMapping,一般为”*.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,通过调用configDigesterparse方法,初始化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”。则此Moduleprefix/firstSubModule,并通过解析配置文件初始化该ModuleConfigImpl对象。同理,可以配置多个子Module,而初始化完主Module(前缀为””)后会初始化所有子Module

 

 

初始化过程中ServletContext设置的相关的属性列表:

1.  设置ServletContextGlobals.SERVLET_KEY属性为web.xmlActionServlet中设置的<url-pattern>标记值,一般为“*.do”。

       getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);

2.设置ServletContextGlobals.ACTION_SERVLET_KEY属性为当前ActionServlet

       getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);

3.设置ServletContextGlobals.MODULE_KEY + prefix属性为当前ModuleConfigImpl对象;

       getServletContext().setAttribute(Globals.MODULE_KEY + prefix, config);

       其中,主ModuleConfigImplprefix值为空字符串””,其它子ModuleConfigImplprefix的定义参见上述的[注释]

4.根据MessageResourcesConfigkey属性值和configprefix属性值设置ServletContext的属性值为当前的MessageResourcesConfig对象。

       getServletContext().setAttribute(

                mrcs[i].getKey() + config.getPrefix(),

                resources);

5.根据DataSourceConfig对象的key属性和configprefix属性设置为ServletContext的相关属性值设置为数据源对象。

       getServletContext().setAttribute

                (dscs[i].getKey() + config.getPrefix(), ds);

6.将配置的plugIns添加到ServletContext属性集中。

       getServletContext().setAttribute(Globals.PLUG_INS_KEY + config.getPrefix(), plugIns);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值