一:添加Struts2+Spring4+Hibernate4所需的jar文件:
1、将下载好的Struts2 jar,Spring4 jar ,Hibernate4 jar 放到MyEclipse-->WEBROOT/WEB-INF/lib下。如图:
添加好Jar后,接下来就是配置文件了。
二:配置Struts2+Spring4+Hibernate4文件:
1、配置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>HelloZw</display-name> <!-- spring4配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring/*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts2配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2_1</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2_1</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- hibernate4配置 --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>mySessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </web-app>
2、配置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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="globalMessages" /> <!-- Add packages here --> <package name="blog" namespace="/" extends="struts-default"> <action name="placeOrder"> <result>/neworder.jsp</result> </action> <action name="saveNewOrder" class="saveNewOrderAction"> <result name="input">/neworder.jsp</result> <result>/vieworder.jsp</result> </action> <action name="findOrder"> <result>/queryorder.jsp</result> </action> <action name="findOrderID" class="findOrderAction"> <result name="input">/queryorder.jsp</result> <result>/vieworder.jsp</result> </action> </package> </struts>
3、配置Spring4和Hibernate4(注:我把hibernate放到Spring中管理,所以没有hibernate-cfg.xml),文件在WEB-INF\config\spring\applicationContext.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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/hellozw?useUnicode=true&characterEncoding=utf8&autoReconnect=true&autoReconnectForPools=true</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>zhangwei</value> </property> </bean> <!-- 配置SessionFactory --> <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="mappingResources"> <list> <value>us/zhwe/hellozw/model/Order.hbm.xml</value> <value>us/zhwe/hellozw/model/OrderLineItem.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="current_session_context_class">thread</prop> <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </prop> <prop key="connection.pool_size">10</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- 配置TransactionManager --> <bean id="myTxManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> <!-- 配置事务策略 --> <tx:advice id="txAdvice" transaction-manager="myTxManager"> <tx:attributes> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* us.zhwe.hellozw.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config> <bean id="orderDAO" class="us.zhwe.hellozw.hibernate.OrderHibernateDAO"> <property name="sessionFactory"> <ref local="mySessionFactory" /> </property> </bean> <bean id="orderService" class="us.zhwe.hellozw.service.spring.OrderServiceSpringImpl"> <property name="orderDAO"> <ref local="orderDAO" /> </property> </bean> <bean id="saveNewOrderAction" class="us.zhwe.hellozw.action.SaveOrderAction" scope="prototype"> <property name="orderService"> <ref local="orderService" /> </property> </bean> <bean id="findOrderAction" class="us.zhwe.hellozw.action.FindOrderAction" scope="prototype"> <property name="orderService"> <ref local="orderService" /> </property> </bean> </beans>
注意:里面有我测试的文件,你们添加的时候要按照自己的需求来添加。
项目结构图: