网上有N多个版本的整合方案,其实都是大同小异,记录我的整合过程,主要想记录下整合时遇到的问题,方便将来查找,有其他人遇到我同样的问题,可以一起探讨。不扯闲话了!
整合环境: jdk6.0 + myeclipse7.0 + struts-2.1.18 + Spring2.5 + Tomcat6.0
整合方案:struts2.1+spring2.5+tomcat6.0连接池
1、引入jar文件,这些应该是整合所需的最基本Jar包,如图:
2、修改web.xml配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- Spring配置 start -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/conf/am/dataAccessContext-AM.xml,
- /WEB-INF/conf/am/applicationContext-AM.xml,
- /WEB-INF/conf/am/actionContext-AM.xml
- </param-value>
- <!--可载入多个配置文件用逗号做分隔符 -->
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- Spring配置 end-->
- <!-- Struts 2 配置 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>struts-default.xml,struts-plugin.xml,../conf/struts.xml</param-value>
- </init-param>
- </filter>
- <!-- Struts 2 配置 -->
- <!-- 字符编码过滤器 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <resource-ref>
- <description>test dataSource</description>
- <res-ref-name>jdbc/webwork</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- </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配置信息:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- 指定由spring负责action对象的创建 -->
- <constant name="struts.objectFactory" value="spring"/>
- <!-- 默认编码集 -->
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <!-- 配置文件修改后自动加载 -->
- <constant name="struts.configuration.xml" value="true" />
- <include file="../conf/am/struts-am.xml" />
- </struts>
配置文件中同时增加了spring管理struts2的action。
struts-am.xml配置信息:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="am" namespace="/am" extends="struts-default">
- <action name="loginAction_*" class="loginAction"
- method="{1}" >
- <result name ="success">/am/login/main.jsp</result>
- </action>
- </package>
- </struts>
3、增加spring配置文件。打算各个模块,不同类(action,BO,DAO)有各自的配置文件。如下:
这里只演示dataAccessContext.xml的配置信息。如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName">
- <value>java:comp/env/jdbc/webwork</value>
- </property>
- </bean>
- <bean id="uuidGenerator" class="com.tsun.utils.IdGenerator"></bean>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource">
- <ref bean = "dataSource" />
- </property>
- </bean>
- <bean id="loginDAO" class="com.tsun.am.login.dao.LoginDAOImpl">
- <property name="jdbcTemplate">
- <ref bean="jdbcTemplate"/>
- </property>
- </bean>
- </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文件夹中!
基本就这样了啊!可以测试了!