struts2中应用Spring 插件(一)

struts2中使用Spring 插件(一)

 

1、 整合 Spring 的准备工作:

1.1    spring.jar commons-logging.jar struts2-spring-plugin-xxx.jar 复制到 lib 中;

1.2    、加载 Spring 框架的配置文件: applicationContext.xml 文件,有以下两种加载方式:

1.2.1 、对于 Servlet2.3 以后的版本,可以通过 Listener 的方式加载,方法如下:

  1 )、对于只有一个 appliactionContext.xml 文件的应用来说:

   <listener>

                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

 

  2 )、若应用中有多个 applicationContext.xml 文件需要加载,方法如下:

  <!— 先确定多个配置文件 à

  <context-param>

     <!— 参数名为 contextConfigLocation à

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/AContext.xml,/WEB-INF/BContext.xml </param-value>

  </context-param>

  <!--- 采用 Listener 完成 Spring 容器的初始化 ->

<listener>

         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

1.2.2 、对于 Servlet2.3 之前的版本,使用 load-on-startup Servlet 进行加载

  同样,有两种情况:

  1 )、一个配置文件的:

         <servlet>

              <servlet-name>context</servlet-name> 

              <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

              <load-on-startup>1</load-on-startup>

         </servlet>

  2 )、多个配置文件的情况:

  <context-param>

     <!— 参数名为 contextConfigLocation à

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/AContext.xml,/WEB-INF/BContext.xml </param-value>

  </context-param>

  <!--- 采用 load-on-startup Servlet 完成 Spring 容器的初始化 ->

<servlet>

       <servlet-name>context</servlet-name> 

       <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

  </servlet>