网上有N多个版本的整合方案,其实都是大同小异,记录我的整合过程,主要想记录下整合时遇到的问题,方便将来查找,有其他人遇到我同样的问题,可以一起探讨。不扯闲话了!

整合环境: jdk6.0 + myeclipse7.0 + struts-2.1.18 + Spring2.5 + Tomcat6.0

整合方案:struts2.1+spring2.5+tomcat6.0连接池

1、引入jar文件,这些应该是整合所需的最基本Jar包,如图:

Struts2.1+Spring2.5+tomcat6.0连接池整合过程详解_spring2.5 

2、修改web.xml配置文件

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
  7.       
  8.     <!-- Spring配置  start --> 
  9.       <context-param>   
  10.         <param-name>contextConfigLocation</param-name>   
  11.         <param-value>/WEB-INF/conf/am/dataAccessContext-AM.xml,  
  12.                     /WEB-INF/conf/am/applicationContext-AM.xml,  
  13.                     /WEB-INF/conf/am/actionContext-AM.xml  
  14.         </param-value>   
  15.         <!--可载入多个配置文件用逗号做分隔符   -->   
  16.       </context-param>   
  17.       <listener>   
  18.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  19.       </listener>   
  20.     <!-- Spring配置  end-->   
  21.     <!-- Struts 2 配置 --> 
  22.     <filter> 
  23.        <filter-name>struts2</filter-name> 
  24.        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  25.         <init-param> 
  26.             <param-name>config</param-name> 
  27.             <param-value>struts-default.xml,struts-plugin.xml,../conf/struts.xml</param-value> 
  28.         </init-param> 
  29.     </filter> 
  30.       
  31.  
  32.     <!-- Struts 2 配置 -->      
  33.       
  34.     <!-- 字符编码过滤器 --> 
  35.     <filter>   
  36.         <filter-name>encodingFilter</filter-name>   
  37.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
  38.         <init-param>   
  39.           <param-name>encoding</param-name>   
  40.           <param-value>UTF-8</param-value>   
  41.         </init-param>   
  42.       </filter>   
  43.       
  44.     <filter-mapping>   
  45.         <filter-name>encodingFilter</filter-name>   
  46.         <url-pattern>/*</url-pattern>   
  47.       </filter-mapping>   
  48.       
  49.     <filter-mapping> 
  50.        <filter-name>struts2</filter-name> 
  51.        <url-pattern>/*</url-pattern> 
  52.     </filter-mapping> 
  53.       
  54.     <resource-ref> 
  55.        <description>test dataSource</description> 
  56.         <res-ref-name>jdbc/webwork</res-ref-name> 
  57.        <res-type>javax.sql.DataSource</res-type> 
  58.        <res-auth>Container</res-auth> 
  59.     </resource-ref> 
  60.  
  61.  
  62. </web-app> 

这个配置文件非常重要!为了将struts的配置文件移动到自定义目录下,费了一番功夫,路径必须为“../conf/struts.xml”,网上有说“/WEB-INF/conf/struts.xml”的,但经过我在本机环境的反复测试发现未能成功,纳闷。。。为啥spring的配置文件能够找到呢?

3、增加struts2.1配置文件。这里我以struts.xml作为基本配置文件,在其中引入各模块配置文件。如:<include file="../conf/am/struts-am.xml" /> 在struts-am.xml中配置实际的action信息。

struts.xml配置信息:

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. <struts> 
  6.  
  7.     <!-- 指定由spring负责action对象的创建 --> 
  8.     <constant name="struts.objectFactory" value="spring"/> 
  9.     <!-- 默认编码集 -->   
  10.     <constant name="struts.i18n.encoding" value="UTF-8" /> 
  11.     <!-- 配置文件修改后自动加载 --> 
  12.     <constant name="struts.configuration.xml" value="true" />     
  13.       
  14.     <include file="../conf/am/struts-am.xml" />   
  15.       
  16. </struts> 

配置文件中同时增加了spring管理struts2的action。

struts-am.xml配置信息:

 

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. <struts> 
  6.  
  7.     <package name="am" namespace="/am" extends="struts-default"> 
  8.       
  9.         <action name="loginAction_*" class="loginAction" 
  10.             method="{1}" > 
  11.             <result name ="success">/am/login/main.jsp</result> 
  12.         </action> 
  13.     </package> 
  14. </struts> 

3、增加spring配置文件。打算各个模块,不同类(action,BO,DAO)有各自的配置文件。如下:

 

 

 

Struts2.1+Spring2.5+tomcat6.0连接池整合过程详解_struts2.1_02

 

这里只演示dataAccessContext.xml的配置信息。如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.   <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
  7.     <property name="jndiName"> 
  8.     <value>java:comp/env/jdbc/webwork</value> 
  9.     </property>    
  10.   </bean> 
  11.   <bean id="uuidGenerator" class="com.tsun.utils.IdGenerator"></bean> 
  12.     
  13.   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
  14.     <property name="dataSource"> 
  15.          <ref bean = "dataSource" />    
  16.     </property> 
  17.   </bean> 
  18.   <bean id="loginDAO" class="com.tsun.am.login.dao.LoginDAOImpl"> 
  19.     <property name="jdbcTemplate"> 
  20.         <ref bean="jdbcTemplate"/> 
  21.     </property> 
  22.   </bean> 
  23. </beans> 

4、配置tomcat连接池

     经测试,本方法使用本机tomcat5.5和tomcat6.0

     在WebRoot/META-INF/中创建context.xml

     这个为了方便大家copy,直接贴纯文本了啊!!!
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
 
   <!-- removeAbandonedTimeout多久以后被回收 单位为秒-->
   <!-- removeAbandoned回收没释放的链接-->
   <!-- logAbandoned日志打印没有回收的链接栈信息-->
          
 <Resource
      name="jdbc/webwork"
      type="javax.sql.DataSource"
      maxActive="4"
      maxIdle="2"
      username="work"
      maxWait="5000"
      driverClassName="com.mysql.jdbc.Driver"
      password="admin"
      url="jdbc:mysql://localhost:3306/webwork"
      removeAbandoned="true"
      removeAbandonedTimeout="300"
      logAbandoned="true"/>
</Context>

    配置内容就不说了,不明白的google一下!

    将对应数据库jar包拷贝到tomcat的lib文件夹中!

    基本就这样了啊!可以测试了!