SSH整合步驟

來源:互聯網
上載者:User

Spring+Struts

 

1. 在web.xml中註冊接聽程式:

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

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/beans.xml</param-value>
 </context-param>

 

2. 將Spring的jar包添加到項目中, 除了Spring的jar包以外,還需要struts2-spring-plugin-2.2.1.jar, 將log4j.properties放入src下

 

3. 在strtus.xml中添加常量:<constant name="struts.objectFactory" value="spring"/>意思是對象工廠由spring來充當

 

4. 修改Action的代碼,將Action中獲得Dao的方式由原來直接new改成:將Dao作為屬性放入Action

 

5. 在Spring的設定檔中對項目中用到的類進行配置, 主要是Action和DaoDao配置成單例的

     Action配置成原型(每個請求是新的執行個體進行處理,避免安全執行緒的問題)

 

6. 修改struts.xml設定檔,將原來在class中配置的action的完全限定名 修改成 spring設定檔中配置的id屬性

 

 

Spring和Hibernate整合

7. 自訂的Dao繼承與Spring提供的類HibernateDaoSupport()類

     同時實現自己的介面直接調用HibernateTemplate()實現持久化操作。

 

8. 在Spring的設定檔中,添加如下配置:
 
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/stuinfo" />
  <property name="username" value="root" />
  <property name="password" value="123" />
 </bean>

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
 </bean>

 將sessionFactory注入到自訂的Dao裡。例如:

 <bean id="commDao" class="com.etp.dao.CommDaoImpl">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </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.