https://github.com/panying0424/SSH.git
Struts2X+Spring5X+Hibernate5X 整合Demo
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Bill</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 定义Struts 2的核心Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<!-- 让Struts 2的核心Filter拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder
location="classpath:DB.properties" />
<!-- C3P0 数据源 -->
<bean id="dataSourse"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSourse"></property>
<!-- 配置Hibernate属性 -->
<property name="configLocation"
value="classpath:hibernate.cfg.xml"></property>
<!-- 加载Hibernate配置文件 -->
<property name="mappingLocations"
value="classpath:com/bill/entity/*.hbm.xml"></property>
</bean>
<!-- spring 声明事务 -->
<!-- 事务 管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务属性 -->
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="check*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 事务属性 与切入点关联 -->
<aop:config>
<aop:pointcut
expression="execution(* com.bill.service.*.*(..))" id="txPointcut" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="txPointcut" />
</aop:config>
<bean id="userDao" class="com.bill.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="accountDao" class="com.bill.dao.AccountDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="billDao" class="com.bill.dao.BillDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.bill.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="accountService" class="com.bill.service.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="billService" class="com.bill.service.BillService">
<property name="billDao" ref="billDao"></property>
</bean>
<!-- action -->
<bean id="loginUserAction" class="com.bill.user.action.LoginUser"
scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="addAccountAction"
class="com.bill.account.action.AddAccount" scope="prototype">
<property name="accountService" ref="accountService"></property>
</bean>
<bean id="accountJSONAction"
class="com.bill.account.action.AccountJSON" scope="prototype">
<property name="accountService" ref="accountService"></property>
</bean>
<bean id="accountJSONMoneyAction"
class="com.bill.account.action.AccountJSONMoney" scope="prototype">
<property name="accountService" ref="accountService"></property>
</bean>
<bean id="displayBillsAction"
class="com.bill.bills.action.DisplayBills" scope="prototype">
<property name="billService" ref="billService"></property>
</bean>
</beans>
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Struts2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<!-- 配置了系列常量 动态方法调用关闭 开发者模式 -->
<constant name="struts.enable.DynamicMethodInvocation"
value="false"></constant>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default" namespace="/">
<action name="loginUser" class="loginUserAction">
<result name="success" type="redirectAction">displayBills</result>
<result name="input">/index.jsp</result>
</action>
<action name="addAccount" class="addAccountAction"></action>
<action name="displayBills" class="displayBillsAction">
<result name="success" type="dispatcher">/bills.jsp</result>
</action>
</package>
<package name="json" namespace="/" extends="json-default">
<action name="accountJSON" class="accountJSONAction">
<result name="success" type="json">
<param name="noCache">true</param>
<param name="contentType">application/json</param>
</result>
</action>
<action name="accountJSONMoney" class="accountJSONMoneyAction">
<result name="success" type="json">
<param name="noCache">true</param>
<param name="contentType">application/json</param>
</result>
</action>
</package>
</struts>