1. 程式人生 > >SSH三大框架搭建整合struts2+spring+hibernate

SSH三大框架搭建整合struts2+spring+hibernate

SSH框架整合理論:

在SSH框架的JAVAweb專案的開發過程中,WEB層通常使用的是Struts2+jsp,service層使用的是javaBean,DAO層使用的是hibernate,而spring的使用覆蓋三層。

使用了spring框架之後,我們可以把物件交給spring來管理。在WEB層中,所有action物件的建立和管理都可以交給spring來完成,這樣Struts2就不用自己來new一個action,這一切都可以交給spring,直接向spring來要action物件。

在DAO層中hibernate框架,在這一層中hibernate–SessionFactory物件,包括session的獲得,AOP事務,都可以交給spring,這樣使用hibernate只需要做一些業務操作就可以了,

把其他的關於事務以及物件的管理都交給spring來完成。

簡單的說,三大框架整合,就是把Struts2整合到spring當中,把hibernate整合到spring當中。

下面我們來進行三大框架整合:

整合的第一步,我們首先需要做的就是導包,匯入三個框架需要用到的jar包,這是三大框架整個中的第一步,也是最重要的一步,導包完成後,我們的三大框架整合就已經完成了百分之六十。

如果三大框架所用到的jar包檔案壓縮包沒有,可以去各個框架的相關網站上下載。
具體需要用的jar包如下

hibernate包:

  1. hibernate/lib/required
  2. hibernate/lib/jpahibernate/lib/jpa
  3. 資料庫驅動包資料庫驅動包

在hibernate中的lib下的required包中jar檔案,都需要拿過來在hibernate中的lib下的required包中jar檔案,都需要拿過來。

在這裡插入圖片描述

/lib/jpa java持久化規範
在這裡插入圖片描述

Struts2包:

在這裡插入圖片描述
如果有重複,刪除版本低的那個。
在這裡插入圖片描述

Struts整合spring外掛包
在這裡插入圖片描述

如果匯入這個包,Struts在啟動的時候就會尋找spring容器,如果還沒有配置spring容器,單單是啟動Struts,專案就會丟擲異常。如果只是用Struts框架時,這個包可以不需要匯入。

spring包:

  1. 基本包:4+2
  2. 整合web需要的web包整合web需要的web包
  3. 整合aop:4個包
  4. 整合aop:4個包整合aop:4個包
  5. 整合JDBC事務:4個整合JDBC事務:4個
  6. 整合Junit4測試:1個
    基本包:4+2 core |beans |context| expression| logging| log4j
    整合web需要的web包:spring-web
    整合aop:4個包:spring-aop|spring-aspect|aop聯盟|aopweaving
    整合JDBC事務:spring-jdbc|spring-tx|c3p0|spring-orm
    整合Junit4測試:spring-test

基本包:
在這裡插入圖片描述

整合web需要的web包
在這裡插入圖片描述

整合aop:4個包
在這裡插入圖片描述

整合JDBC事務
在這裡插入圖片描述

整合Junit4測試
在這裡插入圖片描述

截止到這裡,用到的包就匯入完了,但是如果需要使用的是eclipse,標籤庫的包還是需要自己匯入一下。
在這裡插入圖片描述
導包完成

導包完成之後,就需要進行三大框架的整合步驟,首先是單獨為專案配置每一個框架

單獨配置spring容器

建立配置檔案,並匯入約束(4個)beans|context|aop|tx

首先,建立spring配置檔案applicationContext.xml

在這裡插入圖片描述

然後切換到設計檢視
在這裡插入圖片描述
然後再繼續匯入剩餘的4個約束

beans約束
在這裡插入圖片描述
context約束
在這裡插入圖片描述
然後aop,tx 這樣四個名稱空間就匯入完成了
程式碼如下:
在這裡插入圖片描述
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.springframework.org/schema/beans" 
		xmlns:context="http://www.springframework.org/schema/context"
		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-4.2.xsd 
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<bean name="userAction " class="com.test.web.action.UserAction "></bean>						
</beans>

截止到這裡,spring的約束以及名稱空間也匯入完成。

這樣spring容器就配置好了,但是我們想讓spring隨著專案的啟動而啟動,就需要在web.xml檔案中配置一個監聽器。

web.xml

    <!-- 讓spring隨web啟動而建立的監聽器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
    <!-- 配置spring配置檔案位置引數 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>

這個時候啟動專案,專案不報錯,說明spring容器隨著專案的啟動而啟動配置沒有錯誤。

單獨配置Struts2

  1. 配置Struts2主配置檔案
<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd"> 
	
<struts>
	<!-- #  struts.objectFactory = spring	將action的建立交給spring容器	
			struts.objectFactory.spring.autoWire = name spring負責裝配Action依賴屬性
			-->
	<constant name="struts.objectFactory" value="spring"></constant>

	<package name="crm" namespace="/" extends="struts-default" >
		
		<action name="UserAction_*" class="userAction" method="{1}" >
			<result name="toHome" type="redirect" >/index.htm</result>
			<result name="error" >/login.jsp</result>
		</action>
	</package>
</struts>

主配置檔案寫好之後,需要配置Struts2的核心過濾器

在web.xml檔案中配置Struts2的核心過濾器

程式碼如下:

<filter>
  	<filter-name>openSessionInView</filter-name>
  	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <!-- struts2核心過濾器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>openSessionInView</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

需要注意的是:配置到這裡,還是建議重新的啟動專案,看看是否配置正確,我們在這裡需要注意的是,每配置一步,我們就啟動專案測試一下,看看該步是否正確

防止,等最後我們配置完的時候,出現錯誤的時候,不方便進行檢查。

配置完spring到web專案,以及配置完Struts2到web專案,下面需要做的就是整合Struts2到spring中

整合Struts2與spring

  1. 需要導包,匯入spring與Struts2整合包,這個包我們在第一步已經匯入過了struts2-spring-plugin-2.3.24.jar

  2. 配置常量:

(1)struts.objectFactory = spring 將action的建立交給spring容器 (2)struts.objectFactory.spring.autoWire = name spring負責裝配Action依賴屬性

我們只需要配置第一個常量即可,因為第二個預設是開啟的,不用配置。第一個常量是將action的建立交給spring容器 ,我們需要在Struts.xml檔案中配置

<!-- #  struts.objectFactory = spring	將action的建立交給spring容器	
			struts.objectFactory.spring.autoWire = name spring負責裝配Action依賴屬性
			-->
	<constant name="struts.objectFactory" value="spring"></constant>

在這裡插入圖片描述
下面進行Struts2與spring整合

整合有兩種方案

第一種方案:class屬性上仍然配置action的完整類名,struts2仍然建立action,由spring負責組裝Action中的依賴屬性
在這裡插入圖片描述

使用這種方案,就是在配置檔案中action class依舊使用完整的類名,這樣可以看成是Struts與spring配合進行物件建立。struts2仍然建立action,由spring負責組裝Action中的依賴屬性

不過這種方法不推薦使用,不推薦理由:最好由spring完整管理action的生命週期.spring中功能才應用到Action上.

第二種方案:

class屬性上填寫spring中action物件的BeanName,BeanName是在applicationContext.xml檔案中定義的,完全由spring管理action生命週期,包括Action的建立注意:需要手動組裝依賴屬性

注意:需要手動組裝依賴屬性,因為這樣配置的話,spring就不能自動組裝,需要手動組裝依賴屬性
手動組裝依賴屬性的意思就是,在applicationContext.xml檔案中,把各個物件的依賴手動配置進去。

在這裡插入圖片描述
在Struts.xml檔案中,class屬性只需要寫beanName就可以了
在這裡插入圖片描述
注意:scope=“prototype” Action物件作用範圍一定是多例的.這樣才符合struts2架構

	<!-- action -->
	<!-- 注意:Action物件作用範圍一定是多例的.這樣才符合struts2架構 -->
	<bean name="userAction" class="com.test.web.action.UserAction" scope="prototype" >
		<property name="userService" ref="userService" ></property>
	</bean>
	<!-- service -->
	<bean name="userService" class="com.test.service.impl.UserServiceImpl" >
	</bean>

到這裡,Struts整合spring完成了

下面需要做的是。進行單獨配置hibernate,然後把hibernate與spring進行整合:

單獨配置hibernate

建立hibernate配置檔案hibernate.cfg.xml,配置相關實體以及資料庫的連線

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
		<!-- 將連線池注入到sessionFactory, hibernate會通過連線池獲得連線 -->
		<property name="dataSource" ref="dataSource" ></property>
		<!-- 配置hibernate基本資訊 -->
		<property name="hibernateProperties">
			<props>
				<!--  必選配置 -->
				<prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
				<prop key="hibernate.connection.url" >jdbc:mysql:///crm</prop>
				<prop key="hibernate.connection.username" >root</prop>
				<prop key="hibernate.connection.password" >123456</prop> 
				<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
				
				<!--  可選配置 -->
				<prop key="hibernate.show_sql" >true</prop>
				<prop key="hibernate.format_sql" >true</prop>
				<prop key="hibernate.hbm2ddl.auto" >update</prop>
			</props>
		</property>
		<!-- 引入orm元資料,指定orm元資料所在的包路徑,spring會自動讀取包中的所有配置 -->
		<property name="mappingDirectoryLocations" value="classpath:com/test/domain" ></property>
	</bean>

這一種方案是推薦方案,我們把sessionFactory交給spring容器來管理

在三大框架整合中,這些是最基本的整合操作,下面將對整合中剩餘的的細節進行補充:

使用C3P0連線池:

1.配置db.properties

jdbc.jdbcUrl=jdbc:mysql:///crm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

2.引入連線池到spring中

	<!-- 讀取db.properties檔案 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 配置c3p0連線池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
		<property name="driverClass" value="${jdbc.driverClass}" ></property>
		<property name="user" value="${jdbc.user}" ></property>
		<property name="password" value="${jdbc.password}" ></property>
	</bean>

有了連線池後,直接從連線池中讀取配置資訊,所以可以在spring容器applicationContext.xml檔案中,。就不用配置資料庫連線資訊了

3.將連線池注入給SessionFactory

<!-- 將連線池注入到sessionFactory, hibernate會通過連線池獲得連線 -->
		<property name="dataSource" ref="dataSource" ></property>

到這裡,配置資訊配置的都已經差不多了,下面需要做的是使用DAO方法操作資料庫了。