因爲剛開始學習SSH,其中的配置比較多,爲了下次可以快速的進行配置,將SSH整合的過程記錄下來,以便下次查閱。html
軟件環境:MyEclipse 9.0、Struts2.二、Spring三、Hibernate三、tomcat 6x。java
具體的步驟以下:mysql
1. 新建一個web項目;web
2. 在tomcat服務器配置新建的web項目spring
a) 在conf下的 server.xml中增長Context節,以下:sql
<Context path="/ssh" docBase="D: \ssh\WebRoot" reloadable="true"/>數據庫
b) 或者直接在MyEclipse中對新建的項目進行部署;apache
c) 開啓tomcat服務器,測試是否配置完成。tomcat
3. 配置struts2服務器
a) 添加struts2所需的基本jar到lib目錄下,尤爲注意不要漏掉struts2-spring-plugin-2.2.1.1.jar包,一共9個,以下:
l commons-fileupload-1.2.1.jar
l commons-io-1.3.2.jar
l commons-logging-1.0.4.jar
l freemarker-2.3.16.jar
l javassist-3.7.ga.jar 這個包能夠從實例項目中獲取,lib中並無
l ognl-3.0.jar
l struts2-core-2.2.1.1.jar
l struts2-spring-plugin-2.2.1.1.jar
l xwork-core-2.2.1.1.jar
b) 在web.xml中增長struts2的過濾器,配置以下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
c) 在src目錄下建立struts的配置文件,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> <package name="struts2" extends="struts-default"> </package> </struts> |
4. 配置Spring
a) 使用MyEclipse的Add Spring Capabilities功能導入所需的包,包括AOP、Core、Persistence Core 和Web,注意要選擇將導入的包拷貝到lib目錄下;
b) 取消Enable AOP Builder功能,並選擇將配置文件生成到WEB-INF目錄下;
c) 在web.xml中增長Spring的監聽器,配置以下:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> |
5. 配置Hibernate
a) 使用MyEclipse的Add Hibernate Capabilities功能導入所需的包,導入默認選擇的兩個包便可,注意要選擇將導入的包拷貝到lib目錄下;
b) 將項目中所需的數據庫驅動包和鏈接池相關的包導入到lib目錄下,這裏包括mySql和DBCP相關的三個包;
c) 在Spring配置文件中配置datasource和SessionFactory,以下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingResources"> <list> <!--<value>com/test/bean/User.hbm.xml</value>--> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> |
6. 啓動tomcat服務器,進入默認頁面,看是否配置成功。