Struts2 Spring Hibernate 的简单整合

   首先,加载Spring框架,如图:

 

    为了便于以后添加新的应用,这里把spring所有的Jar包都添加了,下一步,要将Spring的配置文件创建在

WEB-INF目录下,或许不理它,到项目中去移动也可以。单击Finish, 对Spring的添加到此结束。

 

    接着我们再添加Hibernate框架,如图:

接着选择将Hibernate的配置文件交给Spring来进行管理,如图:

再为Hibernate创建一个sessionFactory,如图:

接着再选择数据源,

接着是提示你是否建立sessionFactory,因为已经将sessionFactory交给Spring管理了,所以在这里不用创建了

   单击Finish,并将Spring中与Hibernate中一样的Jar包全部替换,这样就完成了对Hibernate框架的加载了。

接着再加载struts2框架,这里就不再详细说明了,详情参考Struts2 + Spring 整合简单例子。呵呵~~~

接着就是配置ApplicationContext.xml 文件。代码如图:


 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


    class="org.apache.commons.dbcp.BasicDataSource">
      value="com.mysql.jdbc.Driver">
  

  
  
  
 

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  
   
  

  
   
    
     org.hibernate.dialect.MySQLDialect
    

    
    
    true
    true
        
    
   

  

 

 
 
 
    class="org.springframework.orm.hibernate3.HibernateTemplate">
  
   
  

 

 
 

这里我们要注意添加注释的一段,这些是对我们接下去的程序很有帮助的。

再编写struts.xml 文件,具体代码如下(因现在只是配置三大框架的环境,所以很简单,空空如也):


        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
       
       

接着就是最重要的web.xml配置文件了,在Struts2整合Spring的例子已经说明了,这里也不详说了,代码如下:


 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
  org.springframework.web.context.ContextLoaderListener
 

 
  struts2
  org.apache.struts2.dispatcher.FilterDispatcher  
 

  
  struts2
  /*
 

 
    index.jsp
 

这时,还不能进行编写详细的代码,应该要先测试下这个环境出错了没。部署项目到 tomcat 里,运行没有

出现错误,如出现错误,请详细检查上面每一步的操作是否一致。

到此为止,开发环境三大框架整合的准备工作就完成了。

你可能感兴趣的:(hibernate,spring,struts,encoding,框架,bean)