创建maven+spring+struts1
一、 建立 maven 项目
二、项目结构
三、配置 pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- spring end -->
<!-- struts start -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<!-- struts end -->
<!-- spring + spring start -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<!-- spring + spring end -->
<!-- C3P0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<!-- End C3P0 -->
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- End jstl -->
</dependencies>
四、配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- End Spring配置 -->
<!-- 默认i18n资源文件 -->
<context-param>
<param-name>
javax.servlet.jsp.jstl.fmt.localizationContext
</param-name>
<param-value>i18n/messages</param-value>
</context-param>
<!--End 默认i18n资源文件 -->
<!-- Spring 编码过滤 -->
<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>
<!--
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
-->
</filter>
<!--
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.mvc</url-pattern>
</filter-mapping>
-->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
-->
<!-- End Spring 编码过滤 -->
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- End Spring 刷新Introspector防止内存泄露 -->
<!-- 后台登录验证过滤 -->
<filter>
<filter-name>userLoginFilter</filter-name>
<filter-class>
com.npsesa.web.filter.UserLoginFilter
</filter-class>
</filter>
<!-- End 后台登录验证过滤 -->
<!-- Struts2配置 -->
<filter>
<filter-name>struts2-config</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2-config</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2-config</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- End Struts2配置 -->
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- End session超时定义,单位为分钟 -->
<!-- 默认首页定义 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- End 默认首页定义 -->
<!-- Error Page定义 -->
<error-page>
<error-code>500</error-code>
<location>/commons/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/commons/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/commons/403.jsp</location>
</error-page>
<!-- End Error Page定义 -->
</web-app>
五、新建文件夹,将项目结构变成如下
六、配置jdbc.properties
# Hsqldb
#jdbc.url=jdbc:hsqldb:./hsqldb/se
#jdbc.url=jdbc:hsqldb:hsql://localhost/se
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
# Oracle
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:se
#hibernate.dialect=org.hibernate.dialect.Oracle9iDialect
# DB2
#jdbc.driverClassName=com.ibm.db2.jcc.DB2Driver
#jdbc.driverClassName=COM.ibm.db2.jdbc.app.DB2Driver
#jdbc.url=jdbc:db2://localhost:50000/se
#hibernate.dialect=org.hibernate.dialect.DB2Dialect
# MySQL
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/npsesa?useUnicode=true&characterEncoding=utf8
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# PostgreSQL
#jdbc.driverClassName=org.postgresql.Driver
#jdbc.url=jdbc:postgresql:se
#hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
# MS SQL Server
jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver
#jdbc.url=jdbc:jtds:sqlserver://192.168.3.221:1433;DatabaseName=npsesa
jdbc.url=jdbc:jtds:sqlserver://localhost:1433;instance=SQL2012;DatabaseName=npsesa
# Username & Password
jdbc.username=sa
jdbc.password=sas
#jdbc.password=sa
七、配置applicationContext-jdbc.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 拦截事务的方法设置 -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="txManager" />
<property name="transactionAttributes">
<props>
<prop key="find*"> PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*"> PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*"> PROPAGATION_REQUIRED</prop>
<prop key="save*"> PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="txProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- dbcp数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"></property> <!--mysql数据库驱动 -->
<property name="jdbcUrl" value="${jdbc.url}"></property> <!-- mysql数据库名称 -->
<property name="user" value="${jdbc.username}"></property> <!-- 数据库的登陆用户名 -->
<property name="password" value="${jdbc.password}"></property> <!-- 数据库的登陆密码 -->
<!-- 其他配置 -->
<property name="initialPoolSize" value="3"></property> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="minPoolSize" value="3"></property> <!--连接池中保留的最小连接数。Default: 3 -->
<property name="maxPoolSize" value="5"></property> <!--连接池中保留的最大连接数。Default: 15 -->
<property name="acquireIncrement" value="3"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="maxStatements" value="8"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxIdleTime" value="1800"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
</bean>
<!-- Spring jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="baseDAOAdapter" class="com.npsesa.dao.impl.BaseDAOAdapter">
<property name="jdbcTemplate" ref ="jdbcTemplate" />
</bean>
</beans>
八、配置 applicationContext-action.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 3.0//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>
九、配置 applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 3.0//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="clientDao" class="com.npsesa.dao.impl.ClientDaoImpl" parent="baseDAOAdapter" />
<bean id="moduleDao" class="com.npsesa.dao.impl.ModuleDaoImpl" parent="baseDAOAdapter" />
</beans>
十、配置 struts2-config.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">
<struts2-config>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:spring/applicationContext-action.xml" />
</plug-in>
</struts2-config>