Struts+Hibernate+Spring結合

來源:互聯網
上載者:User

===============================================struts和spring架構結合配置=============================================
要將Spring作為Struts的一個外掛程式來使用,在struts-config.xml檔案中加入如下配置:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>

在Web.xml中通過下面的方式載入Spring
<context-param> 
 <param-name>
  contextConfigLocation
 </param-name>
 <param-value>
  /WEB-INF/applicationContext.xml
 </param-value>
</context-param>
 <listener>       
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

===============================================struts和spring結合三種方式============================================
1)。繼承org.springframework.web.struts.ActionSupport類
將Action類的繼承方式更改為org.springframework.web.struts.ActionSupport或org.springframework.web.struts.DispatchActionSupport
在Action類中需要得到Spring Context ApplicationContext context = this.getWebApplicationContext();

2)。控制器方式
需要在struts-config.xml檔案中加入:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>

在applicationContext.xml檔案中不僅需要注入DAO實作類別,還要通過Action類的path路徑注入Action類

3)。委託代理
在struts-config.xml檔案中更改Action標籤裡的type屬性為: org.springframework.web.struts.DelegatinActionProxy

在applicationContext.xml檔案中與控制器方式一樣,注入DAO實作類別與Action。
<!– 注入實作類別 -->
<bean name="threeifc" class="com.soft.test.TestIMP">
 <property name="template" ref="template"></property>
</bean>
<!– 注入Action -->
<bean name="/path路徑" class="com.soft.three.ThreeAction">
 <property name="threeifc" ref="threeifc"></property>
</bean>

===============================================hibernate架構與spring架構結合========================================
//hibernate設定檔存放在src下面
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
 <value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

=================================================jdbc資料來源配置=====================================================
<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
 <value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
 <value>jdbc:oracle:thin:@127.0.0.1:1521:name</value>
</property>
<property name="username">
 <value>username</value>
</property>
<property name="password">
 <value>password</value>
</property>
</bean>

==================================================JNDI資料來源配置====================================================
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
 <value>java:comp/env/jdbc/oracle</value>
</property>
</bean>

<!– Hibernate的一些參數的配置 -->
<property name="hibernateProperties">
<props>
 <prop key=“hibernate.dialect”><!– 資料庫方言 -->
  org.hibernate.dialect.Oracle9Dialect
 </prop>
 <prop key=“hibernate.show_sql”><!– 在控制台列印sql語句 -->
  true
 </prop>
</props>
</property>
<!– 表映射 -->
<property name="mappingResources">
<list>
 <value>com/webssh/TableName.hbm.xml</value>
</list>
</property>

=====================================================hibernate模板注入==============================================
<!-- HibernateTemplate -->
<bean name=" hibernateTemplate " class="org.springframework.orm.hibernate3.HibernateTemplate">
 <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

使用HibernateTemplate的方式有兩種:
方式一:
<!-- 將模板注入給介面,需要在實作類別中寫屬性 -->
<bean id="classIFC" class="com.webssh.classinfo.ClassIMP2">
 <property name="template" ref="hibernateTemplate"></property>
</bean>
方式二:
<!-- 將模板注入給介面,不需要在實作類別中寫屬性 -->
介面要繼承org.springframework.orm.hibernate3.support.HibernateDaoSupport類
<bean id="classIFC" class="com.webssh.classinfo.ClassIMP2">
 <property name="hibernateTemplate" ref="hibernateTemplate">
 </property>
</bean>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.