SSH(Spring3.0 + Struts2.1 + Hibernate3.3)整合示例

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.在浏览器中运行测试。

http://localhost:8080/SS2H/listCat.action

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章