Struts2+Spring+Hibernate的整合

 

Struts2 –Spring -Hibernate的整合:

Struts2 –Spring -Hibernate的整合过程:

1)        添加hibernate3.1支持,

       *选中项目名àMyEclipseàadd Hibernate capabilitiesà(①创建hibernate配置文件——hibernate.cfg.xml;②在添加支持的过程中创建HibernateSessionFactory类(需要指定包,例如可以创建包名为com.softeem.s2sh.utils));

       *配置hibernate.cfg.xml(数据库连接、添加需要的properties设置,例如show_sql);

       *导入Hibernate连接数据库所需要的驱动jar包。

 

2)        添加Spring2.0的支持

       *选中项目名àMyEclipseàadd Spring capabilitiesà(①勾选Spring的WEB支持(即勾选Spring2.0 Web library)和aop支持(即勾选Spring2.0 AOP library);②添加spring bean configuration配置文件——applicationContext.xml;③在配置文件中创建sessionfactory的bean;

      

3)        Spring和Hibernate的整合

*添加Spring对Hibernate支持的library:项目名àBuild PathàConfigurae Build PathàAdd Libraryà MyEclipse Libraries à勾选Spring 2.0 ORM/DAO/Hibernate3 Library(deprecated)

 

4)        手动添加Struts2的支持

       *必须的7个jar包中的3个(因为那4个在Spring和Hibernate中已经有了)外加一个支持spring的jar包struts2-spring-plugin-2.1.8.jar(可以创建一个user library,把4个包放进去),这四个包分别是:struts2-core-2.1.8.jar、xwork-core-2.1.6.jar、ognl-2.7.3.jar、struts2-spring-plugin-2.1.8.jar,

5)               *添加struts2的配置文件:struts.xml(参考Struts2-core-2.1.6.jar-->struts-2.0.dtd文件的第30-32行),并配置struts2的配置文件struts.xml,例如添加struts2对Spring的依赖(这里要注意,不能使用开发模式,开发模式只能在properties中配置)

 

       *在web.xml中添加struts2的过滤器(建议:2.0版本的配置)

 

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

这时所有的library入下:


6)     Spring和Struts2的整合

*在web.xml中配置加载Spring的配置文件的位置(放在web.xml的最上面)

 

    <context-param>

       <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

    </context-param>

当然,如果将applicationContext.xml直接拷贝到了/WEB-INF下的话,就不用配置以上内容了

 

*在web.xml中添加Spring上下文的监听器(建议放在web.xml的最下面)

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

 

 

 

 

*在struts.xml中添加Struts2对Spring的依赖(Struts2需要依赖于Spring创建Action的对象)

 

    <constant name="struts.objectFactory"

    value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

    <constant name="struts.devMode" value="true"></constant>

 

7)        在HibernateSessionFactory类的目录下创建类CreateTables

package com.softeem.s2sh.utils;

import org.hibernate.cfg.Configuration;

import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CreateTables {

    public static void main(String[] args) {

        Configuration cfg = new Configuration().configure();

        SchemaExport se = new SchemaExport(cfg) ;

        se.create(true, true);

    }

}

以上为基本的步骤

 

8)        在包com.softeem.s2sh.pojos中构建java实例(例如User.java),创建映射文件User.hbm.xml,将映射文件添加到hibernate.cfg.xml中

9)        创建DAO,并在applicationContext.xml中添加生成该DAO对象的bean(这样做是为了采用spring生成对象,而不是用new),例如某个DAO调用了SessionFactory,则applicationContext.xml配置文件中这样写:

    <bean id="userDAO" class="com.softeem.s2sh.dao.impl.UserDAO">

        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

 

10)    创建jsp页面、

11)    创建Action等等等

…………

 

 

 

spring的配置文件applicationContext.xml:

…………

    <bean id="UserDAO" class="com.softeem.dao.UserDAO"></bean>

 

    <bean id="LoginAction" class="com.softeem.actions.LoginAction">

此处需要注意:

在spring的配置文件中,bean标签的id属性要与struts2的配置文件中的action标签class属性相对应,因为现在创建action的对象原本由struts来完成的,现在交给了spring来完成,我们可以理解为“struts找到了class之后跳转到spring中,spring创建了action的对象”

       <property name="agent" ref="UserDAO"></property>

    </bean>

    …………

 

struts2的配置文件struts.xml:

<struts>

<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

 

   

 

Constant标签的内容固定添加,表示:

         将struts中完成的创建action对象的功能交给spring的来做;

    要添加在package标签之前。

<package name="default" namespace="/" extends="struts-default">

        ……

        <action name="login" class="LoginAction">

           <result name="success" type="redirect">/success.jsp</result>

        </action>

       ……

</package>

</struts>

…………

加载spring的配置文件:

applicationContext.xml

项目配置文件web.xml:

         …………

<context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

    </context-param>

 

加载配置文件监听器

 

 

 

 

 


    …………

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

 

 

 

 

 


…………

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值