上次说道开启声明式事务,大家如果多少有点了解的话, 就会知道,其实用Spring来对Hibernate的整合还有Struts2的整合,一大部分就是他控制了配置文件和SessionFactory 来干涉Hibernate和Struts2的。那么现在就开始介绍框架整合的第二部分:
第一步就是导入专门为此准备的配置文件:tr.xml
<!-- 导入事务的配置文件 -->
<import resource="tr.xml"/>
这个文件可以参照 Spring.xml 来写,然后在里面添加关于事务的描述:
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="datagrid*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
然后是对想要使用事务的类和对象进行捕获:<!-- 定义给那些位置做事务 -->
<aop:config>
<aop:pointcut id="transactionPoint" expression="execution(* com.kismet.service.impl..Impl*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPoint"/>
</aop:config>
这样对于事务的声明就完成了,之后就是在项目中的测试和使用了, 到此Spring和Hibernate的集合彻底完事了,剩下的就是和Struts2的集合了。
整合Struts2 之前,还是老规矩:导入相应的Jar包:
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
ognl-3.0.5.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
struts2-spring-plugin-2.3.4.1.jar
我们要想使用Struts2 就要在web.xml中对他进行配置,配置文件如下:
<!-- struts启动 -->
<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>*.action</url-pattern>
</filter-mapping>
刚刚学习Struts2的时候,我们知道需要引入一个Struts2 的配置文件,我们一般叫他:<Struts2.xml> 要把它放到SRC根目录下面,然后为了 书写规范,我们一把都要新建一个配置文件 叫做Action.xml 来专门放置Action的。这样项目看起来就是比较整齐了,下一篇就是介绍如何对框架整合之后进行测试了,毕竟这么纠结的整合完成之后要是不好使 ,那就是天灾人祸啦。谢谢大家观看。