Struts2 Spring Hibernate 的简单整合

[程序开发]Struts2 Spring Hibernate 的简单整合

创建工程,选择java EE5
首先,加载Spring框架,如图:
       
        [程序开发]Struts2 Spring Hibernate 的简单整合
       
       
 为了便于以后添加新的应用,这里把spring所有的Jar包都添加了,下一步,要将Spring的配置文件创建在
       
        WEB-INF目录下,或许不理它,到项目中去移动也可以。单击Finish, 对Spring的添加到此结束。
       
(也可以导入基础的包 1)Spring 2.0 AOP.  2)Spring 2.0 Core . 3)Spring 2.0 Persistence Core.  4)Spring 2.0 Persistence JDBC.)
        接着我们再添加Hibernate框架,(可以就导入一个包 Hibernate 3.0 Core)如图:
       
        [程序开发]Struts2 Spring Hibernate 的简单整合
       
        接着选择将Hibernate的配置文件交给Spring来进行管理,如图:
       
        [程序开发]Struts2 Spring Hibernate 的简单整合
       
        再为Hibernate创建一个sessionFactory,如图:
       
        [程序开发]Struts2 Spring Hibernate 的简单整合

点击查看大图


       
        接着再选择数据源,
       
        [程序开发]Struts2 Spring Hibernate 的简单整合

点击查看大图


       
        接着是提示你是否建立sessionFactory,因为已经将sessionFactory交给Spring管理了,所以在这里不用创建了
       
        [程序开发]Struts2 Spring Hibernate 的简单整合

点击查看大图


       
        单击Finish,并将Spring中与Hibernate中一样的Jar包全部替换,这样就完成了对Hibernate框架的加载了。
       
        接着再加载struts2框架的jar
这里注意的是必须加入了1)Struts2-spring-plugin-2.0.11.jar。
                                 2)freemarker-2.3.8.jar
                                 3)ognl-2.6.11.jar
                                 4)struts2-core-2.0.11.1.jar
                                 5)xwork-2.0.4.jar
注意struts.xml必须建在src目录下
     

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 ">

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="oracle.jdbc.driver.OracleDriver">
  </property>
  <property name="url"
   value="jdbc:oracle:thin:@192.168.0.109:1521:orcl">
  </property>
  <property name="username" value="platform"></property>
  <property name="password" value="platform"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.Oracle9Dialect
    </prop>
    <!-- 以下是添加的,不是自动生成的 -->
    <prop key="hibernate.connection.autocommit">true</prop>
    <prop key="hibernate.show_sql">true</prop>
    <!--上面是方便我们对程序的调试,和操作  -->

   </props>
  </property>
 </bean>
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*" propagation="REQUIRED" />
   <tx:method name="update*" propagation="REQUIRED" />
   <tx:method name="remover*" propagation="REQUIRED" />
   <tx:method name="*" read-only="true" />
  </tx:attributes>
 </tx:advice>

 <aop:config>
  <aop:pointcut id="allManagerMethod"
   expression="execution (* org.agilecc.sd.service.*.*(..))" />
  <aop:advisor advice-ref="txAdvice"
   pointcut-ref="allManagerMethod" />
 </aop:config>
</beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
        <struts>
        </struts>

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
 <!--
  <context-param> <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
  </context-param>
 -->
 <filter>
  <filter-name>lazyLoadingFilter</filter-name>
  <filter-class>
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
 </filter>

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

 <filter-mapping>
  <filter-name>lazyLoadingFilter</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

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

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值