DWR2学习笔记(二)

三.    务器启动DWR初始化,并引出dwr.xml文件多种配置方式

dwr.xml的引入方式,这个要看DwrServlet是如何加载我们的配置文件的,

DwrServlet.init()à

ContainerUtil.configureContainerFully(container, servletConfig)à

configureFromInitParams(container, servletConfig)à

    public static boolean configureFromInitParams(…, ServletConfig servletConfig)

    {

        Enumeration en = servletConfig.getInitParameterNames();

        boolean foundConfig = false;

        while (en.hasMoreElements())

        {

            String name = (String) en.nextElement();

            String value = servletConfig.getInitParameter(name);

            // if the init param starts with "config" then try to load it

            if (name.startsWith(INIT_CONFIG))

            {

                foundConfig = true;

                StringTokenizer st = new StringTokenizer(value, "\n,");

                while (st.hasMoreTokens())

                {

                    String fileName = st.nextToken().trim();

                    DwrXmlConfigurator local = new DwrXmlConfigurator();

                    local.setServletResourceName(fileName);

                    local.configure(container);

                }

            }………

      }………

      Return foundConfig;

}

1.   说明:此方法表示根据web.xml中所描述的dwr.xml位置加载,而不是按照WEB-INF/dwr.xml加载,

(1)如果此方法返回true,表示已经成功从 web.xml中指定的地方加载到dwr.xml,这个方法之后,就不会再从WEB-INF下加载dwr.xml了,

(2)如果此方法返回false,表示web.xml中没有指定dwr.xml位置,此时就会从WEB-INF目录下加载dwr.xml了。

2.   根据上面的源码可以分析出,会读取DwrServlet的配置参数,然后依次分析每个参数名称,如果参数名称是以config开头的,则表示找到了配置,则会加载该参数名所表示的dwr.xml文件位置,该参数可以配置多个dwr.xml文件,以’,’分隔即可

如:

<servlet>

       <servlet-name>dwr-invoker</servlet-name>

       <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

       <init-param>

           <param-name>debug</param-name>

           <param-value>true</param-value>

       </init-param>

       <!-- 指定DWR配置文件 -->

       <!-- 方式1DWR文件配置成局部参数,多个dwr.xml文件用,分隔 -->

       <init-param>

           <param-name>config-dwr</param-name>

           <param-value>

              /WEB-INF/dwr/dwr-hello1.xml,

              /WEB-INF/dwr/dwr-hello2.xml

           </param-value>

       </init-param>

       <!--  方式2也可以写成:

       <init-param>

           <param-name>config-dwr-1</param-name>

           <param-value>

              /WEB-INF/dwr/dwr-hello1.xml

           </param-value>

       </init-param>

       <init-param>

           <param-name>config-dwr-2</param-name>

           <param-value>

              /WEB-INF/dwr/dwr-hello2.xml

           </param-value>

       </init-param>

       -->

       <load-on-startup>9</load-on-startup>

    </servlet>

posted on 2009-12-20 22:21  TroyZ  阅读(502)  评论(0编辑  收藏  举报