1.建立Web Project工程(File-New-Web Project-选择JavaEE 5.0) 2.在Web Project工程下添加Struts框架(选中工程-MyEclipse-Project Capabilities-Add Struts Capabilities..-选择Struts1.3) 3.在Web Project工程下添加Spring框架(选中工程-MyEclipse-Proje
2.在Web Project工程下添加Struts框架(选中工程->MyEclipse->Project Capabilities->Add Struts Capabilities..->选择Struts1.3)
3.在Web Project工程下添加Spring框架(选中工程->MyEclipse->Project Capabilities->Add Spring Capabilities..->选中除(Hibernate 3.2 Annotations & Entity Manager-<MyEclipse-Libraries>、Hibernate 3.2 Advanced Support Libraries -<MyEclipse-Libraries>)、Toplink Essentials-<MyEclipse-Libraries>和OpenJPA -<MyEclipse-Libraries>的包)
自动生成ApplicationContext.xml
4.在Web Project工程下添加Hibernate 框架(选中工程->MyEclipse->Project Capabilities->Add Hibernate Capabilities.. ->清除(Hibernate 3.1 Core Libraries -<MyEclipse-Libraries>)选中包->在SessionFactory id中输入sessionFactory)
5.将数据表反转到工程:(选中表->右键选中Hibernate Reverse Engineering..
6.在Struts配置文件Struts-config.xml中加入plugin插件
作用:<!-- web app 启动, 加载spring -->
<plug-in ClassName=”Org.Springframework.web.struts.ContextLoaderPlugIn” >
<set-property property="contextConfigLocation"
value="classpath:applicationContext.xml"/>
</plug-in>
7 在Struts-config.xml中:把action中的type设置为:
type="org.springframework.web.struts.DelegatingActionProxy"
8在生成好的单个实体DAO上生成接口,方法如下:
(右键
à refactor
à Extract Interface……)
并且在其对应的action上生成接口对象,同时必须生成接口对象的get、set方法并且应用在excute方法中;
9在applicationContext.xml配置文件中:
<bean name=" " class=" ">
<property name="" ref=" ">
</property>
</bean>
其中,bean中的name必须同struts-config.xml内的Action中的path保持一致!class是对应其action的路径。在property中的name是对应DAO所生成的接口在action中创建的对象名称。ref是对应DAO的名称。
10.在ApplicationContext.xml配置文件:
<1>手动创建事务管理器;
<!-- 事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<2><!-- 事务策略通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="find*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="search*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<3>创建事务代理;
<!-- 事务代理 -->
<aop:config>
<aop:pointcut id="txDAO"
expression="execution (* com.accp.entity.*.*(..))" />
<aop:advisor pointcut-ref="txDAO" advice-ref="txAdvice" />
</aop:config>