The first step:
import the package ofStruts2,Hibernate andSpring
The next step:
Create (log4j.properties) or (struts.xml) under the src of your project
And ought to set the struts.xml 's constant be like that <constant name="struts.objectFactory" value="spring"/>
The next step:
Config the web.xml under WEB_INT of your project
<!-- Spring框架的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 这里可以添加多个配置文件,以‘,’隔开 -->
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
<!-- 启动Spring声明生命周期的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决web项目中session延迟加载问题的配置 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filterclass>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Struts2核心过滤器的配置 -->
<filter>
<filter-name>Struts2</filter-name>
<filterclass>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
The next step:
Create applicationContext-beans.xml,applicationContext-db.xml and applicationContext-tx.xml under the folder of WEB_INF
applicationContext-db.xml: This file was used to set dataSource
Tips: Here ought to import a package of c3p0.jar
The next step:
Config the applicationContext-tx.xml:
What needs to be modified here was: <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
There was my file's content:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 映射User.hbm.xml -->
<property name="mappingResources">
<list>
<value>com/xunpoit/entity/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
</value>
</property>
</bean>
The last step:
Config the applicationContext-beans.xml:
It was AOP of the Spring
Thank for your read
Author:lzq_blog
Date:2016-10-07