首页 > 学院 > 开发设计 > 正文

ssh框架整合步骤

2019-11-08 03:23:46
字体:
来源:转载
供稿:网友
一.Struts2与Hibernate整合直接按各自的方法进行(添加jar包和配置文件)。二.SPRing与Struts2整合核心是让Spring管理Struts2的action,方法如下:1.分别搭建各自的框架(添加jar包和配置文件)2.添加"struts2-spring-plugin-x.x.x.jar"包3.在web.xml中配置启动监听器和全局参数(struts2参考文档里复制):<listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value></context-param>4.配置action对象到spring容器中(@component或在xml中配置)三.Spring与Hibernate的整合核心是使用Spring的orm模块管理Hibernate的sessionFactory并使用spring的aop思想使用声明式事务管理,方法如下:1.搭建好各自的框架(添加jar包和配置文件)2.删除Hibernate.cfg.xml,在applicationContext.xml中配置SessionFactory(使用spring的会话工厂)。       <bean id="mySessionFactory"                class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactory<wbr>Bean">                <property name="dataSource" ref="myDataSource" />                <property name="packagesToScan">                        <list>                                <value>com.model</value>                        </list>                </property>                <property name="hibernateProperties">                        <value>                                hibernate.dialect=org.hibernate.dialect.MySQLDialect                                hibernate.show_sql=true                        </value>                </property>        </bean>3.在applicationContext.xml配置HibernateTemplate      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">        <property name="sessionFactory" ref="mySessionFactory"></property>      </bean>4.在applicationContext.xml中添加声名式事务处理的配置        <bean id="txManager"                class="org.springframework.orm.hibernate3.HibernateTransactionMana<wbr>ger">                <property name="sessionFactory" ref="mySessionFactory" />        </bean>        <tx:annotation-driven transaction-manager="txManager" />5.Dao层的设计:方案一:在DAO类中加入HiberanteTemplate的引用,并使用spring容器注入实例,使用:this.hibernateTemplate。方案二:DAO类继承HibernateDaoSupport,dao类和其继承的属性(hibernateTemplate)需要使用xml的注入方式。使用时:this.getHibernateTemplate()。方案三:不使用HibernateTemplate,直接在dao类中加入SessionFactory的引用,声明式事务使用this.sessionFactory.getCurrentSession(),不使用声明式事务时需要手动开启和关闭会话(不推荐)。四.Spring、Struts2、Hibernate三者整合核心思想是让Spring管理Struts2的Action和Hibernate的SessionFactory,以上已经总结。大体操作步骤跟上面相似,总结如下:操作步骤:1.添加各自的jar包和整合有关的jar包(宁可少加不可多加,防止冲突)2.在web.xml配置struts2的过滤器和Spring的启动监听器、Spring配置文件路径(全局参数)。3.添加struts2的配置文件struts.xml。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表