1. 新建WEB工程;
2. 导入struts2开发包,和资源配置文件
① globalMessages.properties
② struts.properties
3. 导入Spring开发jar包
4. 在web.xml中配置spring容器,让服务器启动时就加载
如下:
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5. Struts2清除缓存,上传时用到
<!-- 清楚缓存,上传时必须 -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</fil ter-class>
</filter>
6. 如果需要让能在struts2中使用Forward功能则作如下配置,web.xml文件【红色字体哪】
<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>*.do</url-pattern>
<span style="color:#ff0000;"><dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher></span>
</filter-mapping>
7. 在spring容器中把对象都配置到applicationContext.xml中
这里需要注意的是:配置action的时候:如下例子
applicationContext.xml中配置如下:
<!-- 配置Action对象 -->
<bean id="loginAction"class="com.sx.web.actions.LoginAction" scope="prototype">
<property name="usrBiz" ref="usrBiz"></property>
</bean>
那么在struts.xml中action这要这样配置:
把spring交由spring管理的配置,当然也可以通过属性配置文件配置struts.objectFactory属性值。在struts.properties中设置struts.objectFactory属性值:struts.objectFactory= spring
也可以直接在struts.xml中作如下配置:
<!-- 把struts2交给spring管理 -->
<constant name="struts.objectFactory" value="spring"></constant>
也就是说如果struts2如果与spring整合了后,struts2的action配置中的class属性直接引用spring容器中action bean的id值即可;
<package name="usrBiz" namespace="/"extends="struts-default">
<action name="usr_*" class="loginAction" method="{1}">
<result >index.jsp</result>
</action>
</package>
8. 解决懒加载的问题:
<!--避免hibernate延迟加载 的"著名的过滤器" -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
9. Spring中的事务配置
方法①:
首先在容器中启用事务管理器
<!-- 启用注解 -->
<context:annotation-config/>
<!-- 配置统一管理事务-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
这时候则在对应需要事务处理的类文件中做如下注解(@Transactional):
这样spring则会自动帮我们管理事务,而无需我们自己去管理;
方法②:
①事物管理器
②advice通知(这里可以指定把事物应用到那个方法,使用哪种级别传播方式)
③aop切面
<!-- 事务配置方法② -->
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.s2sha.web.biz.impls.*.*(..))"id="usrBizOperaction"/>
<!-- 织入通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="usrBizOperaction"/>
</aop:config>