1.新建Web Project,建包。
2.新建Cat(bean),加入@Entity配置。
3.加入Springjar包。
4.加入Hibernatejar包。
5.編寫dao數據操作類。
)1.編寫dao接口
)2.編寫dao實現類(繼承HibernateDaoSupport,實現dao接口)
6.編寫service業務操作類。
)1.編寫service接口
)2.編寫service(繼承service接口,將dao接口作爲其的私有屬性,提供get、set方法)
7.添加Struts2jar包。
8.編寫struts2的Action。(繼承xwork2下的ActionSupport,將service接口作爲該類的私有屬性,get、set方法略)
9.Spring配置文件applicationContext.xml的配置,配置Hibernate相關配置(datasource、sessionFactory)。
)1.修改sessionFactory的class爲org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
)2.在sessionFactory中配置實體類;
)3.配置dao,設置sessionFactory屬性(因爲dao使用的是HibernateDaoSupport);
)4.爲service配置事務(定義事務管理類-定義事務管理規則-用事務工廠代理類管理service),service使用代理類,設置dao屬性;
a.定義事務管理類(org.springframework.orm.hibernate3.HibernateTransactionManager),設置sessionFactory屬性;
b.定義事務管理規則,設置properties屬性,添加一條規則(PROPAGATION_REQUIRED);
c.定義事務工廠代理類(org.springframework.orm.hibernate3.HibernateTransactionManager),設置事務管理屬性、設置被管理對象屬性(被管理對象service設置dao屬性)、設置事務管理規則屬性;
)5.配置action,設置service屬性
10.Struts2配置文件struts.xml的配置。設置constant標籤struts.objectFactory的值爲spring。
11.在web.xml中配置Spring(listener)、struts2(filter)。
)1.配置Spring,Spring文件配置,用listener作爲Spring加載器(org.springframework.web.context.ContextLoaderListener);
)2.配置struts2,使用filter作爲struts2的分發器(org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter);
12.編寫JSP頁面。
)1.exception.jsp
)2.list.jsp
13.部署到Tomcat中
14.在瀏覽器中運行測試。