SHH框架整合詳細步驟

OA項目: SHH框架的整合

1.       數據庫(mysql)的創建: create databases myoa default character set utf8;

2.       查看創建的數據 show create database myoa;

 

Contr+shift+t: 查找方言

Alt+shit+a快速去除重複的字段

Spring :

 

SHH框架整合:

springstruts整合:

 

1.創建數據庫

 

       C:\Documents and Settings\Administrator>mysql -uroot -proot

      

       mysql> create database myoa default character set utf8;

       Query OK, 1 row affected (0.03 sec)

 

       mysql> show create database myoa;

       +----------+---------------------------------------------------------------+

       | Database | Create Database                                               |

       +----------+---------------------------------------------------------------+

       | myoa     | CREATE DATABASE `myoa` /*!40100 DEFAULT CHARACTER SET utf8 */ |

       +----------+---------------------------------------------------------------+

       1 row in set (0.00 sec)

 

 

2.創建web項目

       創建Web項目名爲: MyOA

       設置項目的編碼方式爲utf-8  . 設置後默認項目下所有文件與utf-8編碼方式

       項目名右擊-->properties->Resource-->Text file encoding->Other-->UTF-8

 

3.配置環境

       3.1 Struts2環境搭建(該環境適用於javaEE5)

              3.1.1導入jar struts

                     struts-2.3.1.1\apps\struts2-blank-2.x.war \WEB-INF\lib\下所有jar

                     commons-fileupload-1.2.1.jar

                 commons-lang-2.5.jar

                 commons-io-1.3.2.jar

                 freemarker-2.3.15.jar

                 javassist-3.12.0.GA.jar

                  mysql-connector-java-5.1.5-bin.jar

                 ognl-2.7.3.jar

                 struts2-core-2.1.8.1.jar

                 xwork-core-2.1.6.jar

 

              3.1.2 配置 web.xml

                     <?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">

                     <!--配置Struts2主過濾器 -->

                            <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> //* :表示過濾所有的url請求

                            </filter-mapping>

                     </web-app>

 

              3.1.3 配置 struts.xml

                     window-->Preferences-->xml --> xml Catalog--> 導入struts-2.0.dtd 或者2.5.*.dtd都可以.

                     url: http://struts.apache.org/dtds/struts-2.0.dtd

                     <?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>

                            <!-- 設置爲開發模式作用當配置文件改變時無需重新部署 -->

                         <constant name="struts.devMode" value="true" />

                            <!-- Action的擴展名設爲action -->

                            <constant name="struts.action.extension" value="action" />

                            <name:包名,可以隨便寫,最好使用該類當前的包名, namespace:命名空間,

                         <package name="xxx" namespace="/" extends="struts-default">

                            <action name="自定義" class="action類所在的路徑">

                                   <result>/WEB-INF/success.jsp</result>//返回到指定的頁面

                            </action>

<result input=”錯誤的/返回頁面”>/WEB-INF/success.jsp</result>                     </package>

                     </struts>

3.1.3: 測試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>

    <!-- 配置 struts開發模式-->

    <constant name="struts.devMode" value="true"/>

    <!-- 配置擴展名 -->

    <constant name="struts.action.extension" value="action"/>

    <!-- 聲明唯一的包名,主要用與裝配acitonnamespace爲包名的命名空間-->

    <package name="mydefault" namespace="/" extends="struts-default">

           <!-- 配置總頁面,TotalPageAction 用戶管理-->

       <action name="total_*" class="totalPageAction" method="{1}">

           <result name="{1}">/WEB-INF/totalpage/{1}.jsp</result>

       </action>

</struts>

 

1. Struts2未整合之前測試(在只是導入如strutsjar包時測試如下)

src下創建包例: com.tu.myoa.test , 編寫 TestAction 測試類

       publicclass TestAction extends ActionSupport{

    public String execute() throws Exception {

       System.out.println("TestAction.execute()");

       return"success";

    }

}                  

2 簡單測試使用的jsp頁面 test.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

      <body>

          Struts2測試成功

      </body>

</html>

 

3. 部屬,開啓tomcat      服務器

打開IE 輸入: http://127.0.0.1:8080/MyOA/test.action

網頁中打印出:  Struts2測試成功, 後臺Console打印 : TestAction.execute()... ,則表示Struts正常工作 , 可以開始進行整合

 

3.2 Hibernate環境搭建

       3.2.1 hibernate框架需要導入的jar

       hibernate-distribution-3.5.6-Final\ 目錄

       Hibernate3.jar   --->hibernate核心包  3.5.6版本

 

       其它爲hiberante所依賴的jar

       hibernate-distribution-3.5.6-Final\lib\required目錄

       antlr-2.7.6.jar

       commons-collections-3.1.jar

       dom4j-1.6.1.jar

       javassist-3.9.0.GA.jar

       jta-1.1.jar

       slf4j-api-1.5.8.jar

 

       hibernate-distribution-3.5.6-Final\lib\jpa目錄

                 hibernate-jpa-2.0-api-1.0.0.Final.jar    --不加會報錯              

       hibernate-distribution-3.5.6-Final\lib\optional\c3p0目錄

                c3p0-0.9.1.jar

 

3.2.2 編寫配置文件 hibernate.cfg.xml

       樣板可以在hibernate-distribution-3.5.6-Final\project\etc目錄下可以找到

       配置  http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd

                     <?xml version='1.0' encoding='utf-8'?>

                     <!DOCTYPE hibernate-configuration PUBLIC

                            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

                            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

                     <hibernate-configuration>

                         <session-factory>

                            <!-- 連接數據庫屬性-->

                            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

                            <property name="connection.url">jdbc:mysql://127.0.0.1:3306/創建的數據庫名</property>

                            <property name="connection.username">root</property>

                            <property name="connection.password">root</property>

                            <!-- 是否顯示SQL語句 -->

                            <property name="show_sql">true</property>

                            <!-- SQL 語句方言 -->

                            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

                            <!-- 自動創建表 ,如果表已經存在,不在新建表,沒有存在,則新建表-->

                            <property name="hibernate.hbm2ddl.auto">update</property>

                            <!-- 加載域對象和表的映射文件類路徑, *:表示實體類名,即對象/映射關聯,:該文件的類名必須和創建的類名相同.

                            <mapping resource=" xxx.hbm.xml"/>-->

                         </session-factory>

                     </hibernate-configuration>

                    

              3.2.3 編寫映射文件 xxx.hbm.xml

                     配置  http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd

                     編寫樣板:

                     <?xml version="1.0"?>

                     <!DOCTYPE hibernate-mapping PUBLIC

                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

                     <hibernate-mapping>

                            <class name="類名" table="對應的數據庫中創建的表名">

                                   <id name="類中屬性名">

                                          //navite表示按照數據底層自動增長id的值

                                          <generator class="navite"/>

                                   </id>

                                   <property name="類中屬性名"/>

                            </class>

                     </hibernate-mapping>

 

       3.3 Spring環境搭建

              3.3.1 導入Spring核心包

                     spring-framework-2.5.5\dist目錄下

                            spring.jar

 

                     導入Spring所依賴的jar

                            spring-framework-2.5.5\lib\aspectj目錄下  ---> SpringAOP切面編程技術

                            aspectjrt.jar

                            aspectjweaver.jar

 

                     AOP切面編程所依賴的技術  動態代理和cglib

                            spring-framework-2.5.5\lib\cglib目錄下

                                   cglib-nodep-2.1_3.jar 

 

                     spring-framework-2.5.5\lib\log4j目錄下

                            log4j-1.2.15.jar 

                     spring-framework-2.5.5\lib\jakarta-commons 日誌包,顯示操作出現的不正確信息

                            commons-logging.jar

                    

              3.3.2 配置文件 applicationContext.xml

                     配置  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd          --beans配置

                            http://www.springframework.org/schema/context/spring-context-2.5.xsd --context配置

                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd          --事務配置

 

                     <?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:context="http://www.springframework.org/schema/context"

                                   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/context

                                                 http://www.springframework.org/schema/context/spring-context-2.5.xsd

                                                 http://www.springframework.org/schema/tx

                                                 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

                           

                     <!-- 自動掃描指定的目錄下的所有類並裝配bean(實體)到容器中 -->

                     <context:component-scan base-package="com.tu.myoa"></context:component-scan>

                     </beans>

 

      

3.3.3.配置Springweb.xml中的監聽器

       配置用於創建容器對象的監聽器 , 配置方法在幫助文檔中有:

              打開 spring-framework-2.5.5\docs\reference\html_single\index.html152  -- 15.2. Common configuration

       web.xml中增加如下代碼:                     

    <!-- 配置spring用於創建容器的監聽器 -->

    <listener>

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

    </listener>

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext*.xml</param-value>

    </context-param>

: 以上是在src目錄下配置,如果是WEB-INF需要增加路徑即: /WEB-INF/ applicationContext*.xml

                   

3.4單元測試環境搭建

              工程下右擊-->Build Path--> Add Libraries... --> JUnit --> Junit4 --> Finish ,也可以直接使用註解在方法上寫@Test, 主要作用是:在不用寫main()函數時,可以直接運行測試代碼.非常實用

             

4.SSH整合

              4.1.1 整合Struts2Spring

                     4.1.1.1                 

                            所謂整合是在4.1.1.3 配置 struts.xml文件的<action class="xxx"/> 中的 xxx

                            <!-- 配置測試用的Action

                                   Struts2Spring爲整合前,class屬性寫類的全名

                                   Struts2Spring爲整合後,class屬性寫bean的名稱(或類的全名).

                                   爲了查看是否Spring是否注入成功則將class設置成bean的名稱

                                   因爲在applicationContext.xml文件中設置了自動掃描包下註解-->

                            <!-- 配置測試用的Action -->

                            <action name="test" class="testAction">

                                   <result name="success">/test.jsp</result>

                            </action>

 

                     4.1.1.2 整合需要導入的包  Spring容器去拿bean對象

                            SpringStruts2整合需要使用到的jar

                            struts-2.3.1.1\lib目錄下

                            struts2-spring-plugin-2.3.1.1.jar

 

                     4.1.1.3 配置Springweb.xml中的監聽器

                            配置用於創建容器對象的監聽器,  配置方法在幫助文檔中有:

                                   打開 spring-framework-2.5.5\docs\reference\html_single\index.html

                                   152  -- 15.2. Common configuration

                            web.xml中增加如下代碼:

                            <!-- 配置Spring的用於創建容器對象的監聽器 -->

                            <listener>

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

                            </listener>

                            <context-param>

                              <param-name>contextConfigLocation</param-name>

                              <param-value>applicationContext*.xml</param-value> // src目錄下配置,如果是WEB-INF需要增加路徑即: /WEB-INF/ applicationContext*.xml

                            </context-param>

 

                     4.1.1.4 成功頁面修改test.jsp成如下:

                              <body>

                                Struts2測試成功<br/>

                                Struts2Spring環境整合成功

                              </body>

 

4.1.2 Struts2Spring整合後測試

                     4.1.2.1 在需要測試的bean類中增加註解

                             TestAction 測試類

                            // @Controller("testAction")  默認bean名稱爲類名的頭字母小寫,也可以不寫

                            @Controller    //添加註解,Spring自動注入

                            public class TestAction extends ActionSupport{

                                   public String execute() throws Exception {

                                          System.out.println("TestAction.execute()...");

                                          return this.SUCCESS;

                                   }

                            }

                           

                     4.1.2.2 創建com.tu.myoa.test包編寫 SpringTest.java 測試類

                           import org.springframework.context.ApplicationContext;

                            import org.springframework.context.support.ClassPathXmlApplicationContext;

                            public class SpringTest {

                                   //加載配置文件,並初始化bean

                            private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

                                   @Test     //使用 @Test 單元測試

                                   public void testSpring() throws Exception {

                                          //bean名稱相同,使用自動注入,名稱爲類名頭字母改小寫

                                          TestAction testAction = (TestAction) ac.getBean("testAction"); 

                                          System.out.println(testAction); //打印出哈希值表示Spring通過

                                   }

                            }

                    

                  結果 :進行測試後出現:  com.tu.myoa.test.TestAction@175d6ab 結果表示Spring環境配置成功可以進行整合

                    

                     4.1.1.3

                            部屬,開啓tomcat   服務器

                            打開IE 輸入: http://127.0.0.1:8080/MyOA/test.action 或刷新此頁面

                            網頁中打印出:   Struts2測試成功

                            Struts2Spring環境整合成功

                            後臺Console打印 : TestAction.execute()... 

                            則表示StrutsSpring整合成功

 

       4.2 HibernateSpring整合  SpringIOC容器管理SessionFactory.管理事務

              4.2.1 配置一個beanSpring管理: SessionFactory

                     4.2.1.1 去除hibernate.xml中的數據連接信息.

                            因爲這些數據都教給Spring來進行管理配置,不然會出現重複.

                            <!-- 連接數據庫屬性-->

                            <!-- 將數據刪除或註釋 

                            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

                            <property name="connection.url">jdbc:mysql://127.0.0.1:3306/myoa</property>

                            <property name="connection.username">root</property>

                            <property name="connection.password">root</property>-->

 

                            配置屬性文件:db.properties

                            url=jdbc:mysql:///myoa //連接數據庫的url

                            driverClass=com.mysql.jdbc.Driver//連接驅動

                            username=root//登錄數據庫的用戶名

                            password=root //密碼

 

                     4.2.1.2 編寫 applicationContext.xml

                            編寫增加以下內容:

                            <!-- 自動掃描包下所有的bean並裝配bean -->

                            <context:component-scan base-package="cn.itcast.oa"/>

                           

                            <!-- 加載外部的properties配置文件 ,用於取得連接數據庫信息-->            

                            <context:property-placeholder location="classpath:db.properties"/>

 

                            <!-- 配置SessionFactory Spring管理Hibernate SessionFactory 對象

                                   但因爲SessionFactory是一個接口,不能生成實現類,所以需要編寫實現類

                                   hibernate3中有一個實現類專門是做整合使用的類:LocalSessionFactoryBean

                            --><!-- org.springframework.orm.hibernate3.LocalSessionFactoryBean -->

                            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                                   <!-- 配置hibernate.cfg.xml主配置文件路徑 -->

                                   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

                                   <!-- 配數據源 -->

                                   <property name="dataSource">

                                          <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> 

                                     <!-- 配置數據庫連接信息 ,使用${}EL表達式的形式從屬性文件中取值,注意配置以上 db.properties 屬性文件,注意要換行寫,每行行尾不能有空格-->

                                                 <property name="jdbcUrl" value="${jdbcUrl}"></property>      

                                                 <property name="driverClass" value="${driverClass}"></property>

                                                 <property name="user" value="${username}"></property>

                                                 <property name="password" value="${password}"></property>

                                                 <!-- 配置C3p0時的配置信息詳情查看c3p0幫助方檔

                                                 打開: c3p0-0.9.1.2\doc\index.html

                                                 在附錄Appendix A: Configuration Properties 配置相中有所有配置信息-->

                                                  <!-- 其他和管理配置 -->

                                                 <!--初始化時獲取三個連接,取值應在minPoolSizemaxPoolSize之間。Default: 3 -->

                                                 <property name="initialPoolSize" value="3"></property>

                                                 <!--連接池中保留的最小連接數。Default: 3 -->

                                                 <property name="minPoolSize" value="3"></property>

                                                 <!--連接池中保留的最大連接數。Default: 15 -->

                                                 <property name="maxPoolSize" value="5"></property>

                                                 <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->

                                                 <property name="acquireIncrement" value="3"></property>

                                                 <!-- 控制數據源內加載的PreparedStatements數量。如果maxStatementsmaxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->

                                                 <property name="maxStatements" value="8"></property>

                                                 <!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->

                                                 <property name="maxStatementsPerConnection" value="5"></property>

                                                 <!--最大空閒時間,1800秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->

                                                 <property name="maxIdleTime" value="1800"></property>

                                          </bean>

                                   </property>

                            </bean>

                           

                     4.2.1.3 測試 SessionFactory

                            SpringTest.java中編寫如下測試代碼:

                            //測試sessionFactory是否注入到容器中,dao層做準備

                            @Test

                            public void testSessionFactory() throws Exception {

                                   SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");

                                   System.out.println(sessionFactory);

                            }

 

               4.2.2 配置Spring管理事務

                     4.2.2.1 配置編寫 applicationContext.xml

                            編寫增加以下內容:

                            <!-- Spring管理事務配置聲明式事務管理(採用基於註解的配置) -->

                            <bean id="ttransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

                                   <property name="sessionFactory" ref="sessionFactory"></property>

                            </bean>

                            <tx:annotation-driven transaction-manager="transactionManager"/>

                    

                     4.2.2.2 編寫測試準備創建事務測試類

                            編寫一個Service做測試準備com.tu.myoa.test包中TestService.java模擬向數據庫增加數據

                            @Service  //注入註解默認bean名稱:testService

                            public class TestService {

                                   @Resource  //注入sessionFactory

                                   private SessionFactory sessionFactory;

                                   @Transactional      // 事務註解

                                   public void addUsers() {

                                          //獲取Spring幫我們管理的Session  就一定要調用getCurrentSession();

                                          Session session =sessionFactory.getCurrentSession();

                                          session.save(new User());

                                          //int a = 1 / 0;               //異常測試是否會回滾

                                          session.save(new User());

                                   }

                            }

                    

                     4.2.2.3 編寫測試準備  創建實體

                            4.2.2.3.1 編寫 domain User

                                   com.tu.myoa.domain包中創建一個實體.

                                   package com.tu.myoa.domain;

                                   public class User {

                                          private Long id;

                                          private String name;

                                          public Long getId() {

                                                 return id;

                                          }

                                          public void setId(Long id) {

                                                 this.id = id;

                                          }

                                          public String getName() {

                                                 return name;

                                          }

                                          public void setName(String name) {

                                                 this.name = name;

                                          }

                                   }

                            4.2.2.3.2 配置User.hbm.xml文件如下:

                                          配置代碼提示: hibernate-mapping-3.0.dtd

                                          <?xml version="1.0"?>

                                          <!DOCTYPE hibernate-mapping PUBLIC

                                                 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

                                                 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

                                          <hibernate-mapping package="com.tu.myoa.domain">

                                                 <class name="User" table="myoa_user">

                                                        <id name="id">

                                                               <generator class="native"/>

                                                        </id>

                                                        <property name="name" />

                                                 </class>

                                          </hibernate-mapping>

 

                            4.2.2.3.3 編寫配置文件 hibernate.cfg.xml (對象/關係映射文件)

                                          :3.2.2 <!-- 加載域對象和表的映射文件類路徑

                                                 <mapping resource="xxx.hbm.xml"/>-->

                                          跟改爲如下:

                                   <mapping resource="com/tu/myoa/domain/User.hbm.xml" />

 

                            4.2.2.3.4  SpringTest.java中增加如下測試方法

                                   //測試事務

                                   @Test

                                   public void testServiceTransaction() throws Exception {

                                          TestService testService = (TestService) ac.getBean("testService");

                                          testService.addUsers(); //增加用戶

                                   }

 

                            4.2.2.3.5 測試整合SSH

                                   運行以上代碼:SpringTesttestServiceTransaction()方法

                                   控制平臺打印:則表示成功,整合SSH成功

                                          com.tu.myoa.test.TestAction@ee6ad6

                                          org.hibernate.impl.SessionFactoryImpl@10d0eae

                                          Hibernate: insert into myoa_user (name) values (?)

                                          Hibernate: insert into myoa_user (name) values (?)

                                   mysql> select * from myoa_user;

                                   +----+------+

                                   | id | name |

                                   +----+------+

                                   |  1 | NULL |

                                   |  2 | NULL |

                                   +----+------+

                                   2 rows in set (0.00 sec)

                                   查詢數據庫中的數據如果有兩條數據則表示成功.

 

                            4.2.2.3.6 測試事務

                                   修改 TestService類中的 addUsers 方法爲如下:

                                   @Transactional      // 事務註解

                                   public void addUsers() {

                                          //獲取Spring幫我們管理的Session  就一定要調用getCurrentSession();

                                          Session session =sessionFactory.getCurrentSession();

                                          session.save(new User());

                                          int a = 1 / 0;          //異常測試是否會回滾  <--<---

                                          session.save(new User());

                                   }

                                   重新運行測試方法: SpringTest. testServiceTransaction方法;

                                   出現異常:

                                   mysql> select * from myoa_user;

                                   +----+------+

                                   | id | name |

                                   +----+------+

                                   |  1 | NULL |

                                   |  2 | NULL |

                                   |  4 | NULL |

                                   |  5 | NULL |

                                   +----+------+

                                   4 rows in set (0.00 sec)

                                   當出現少有一個id,則表示事務回滾成功.

 

                           

       4.3 測試SSH整合

              4.3.1 編寫測試代碼

                     @Controller("testAction")

                     public class TestAction extends ActionSupport {

                            //SSH最近整合測試.使用Spring注入

                            @Resource

                            private TestService ts ;          //<-----

                            public String execute() throws Exception {

                                   System.out.println("TestAction.execute()...");

                                   //SSH最近整合測試.

                                   //TestService ts = new TestService();   //當使用時Spring後就不需要new

                                   ts.addUsers();               

                                   return this.SUCCESS;

                            }

                     }

              4.3.2 編寫 test.jsp 測試整合

                       <body>

                         Struts2環境測試成功<br/>

                         Struts2Spring環境整合成功<br/>

                             hibernateSpring環境整合成功<br/>

                         SSH整合成功

                       </body>

 

                     部署開啓 tomcat服務器

                            打開IE 輸入地址: http://127.0.0.1:8080/MyOA/test.action

                            內頁打印:

                                          Struts2測試成功

                                          Struts2Spring環境整合成功

                                          SSH整合成功

                     並控制檯打印如下內容:

                            TestAction.execute()...

                            Hibernate: insert into myoa_user (name) values (?)

                            Hibernate: insert into myoa_user (name) values (?)

 

                     查詢MySQL數據庫:

                            mysql> select * from myoa_user;

                     出現如下則成功:

                            +----+------+

                            | id | name |

                            +----+------+

                            |  1 | NULL |

                            |  2 | NULL |

                            |  4 | NULL |

                            |  5 | NULL |

                           +----+------+

 

                     至此表示三大框架SSH整合完必.

 

 

:以上版本適用於javaEE5,javaEE6需要使用3.0版本的其他包,如果使用javaEE6的則會出現sessionFactory不能注入錯誤信息

shh所需要的jar,即配置文件如下:javaEE5環境總共有24jar

 

antlr-2.7.6.jar

aspectjrt.jar

aspectjweaver.jar

c3p0-0.9.1.jar

cglib-nodep-2.1_3.jar

commons-collections-3.1.jar

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

commons-logging.jar

dom4j-1.6.1.jar

freemarker-2.3.15.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

hibernate3.jar

javassist-3.12.0.GA.jar

jta-1.1.jar

log4j-1.2.15.jar

mysql-connector-java-5.1.5-bin.jar

ognl-2.7.3.jar

slf4j-api-1.5.0.jar

slf4j-log4j12-1.5.0.jar

spring.jar

struts2-core-2.1.8.1.jar

struts2-spring-plugin-2.1.8.1.jar

xwork-core-2.1.6.jar

 

需要配置的文件:

 

applicationContext.xml //主要用於加包下面的實體bean,直接在壓縮包搜索即可,文件名不能修改

db.properties //屬相文件,後綴必須爲properties,主要存一些經常變換的屬性,鍵值對存儲形式,key=value

User.hbm.xml //實體/關係映射文件,主要是實體對象的屬性和表之間的對應,文件名和類名一直,類名.hbm.xml,該文件與實體bean放在一起

hibernate.cfg.xml //固定文件,不可修改文件名,主要配置數據連接信息

log4j.properties//日誌記錄文件,該文件需要設置,error級別,減少不必要顯示的信息,配置log4j.rootLogger=error, stdout,其他帶有日誌級別(info,warn,debug)的全部使用#註釋

struts.xml //用於配置action,文件名不可改變,只能放在src目錄下.其他文件任意

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章