天天看點

Struts2,Spring3,Hibernate4整合--SSH架構

Struts2,Spring3,Hibernate4整合--SSH架構(學習中)      

一、包的導入

  1、Spring包

  

Struts2,Spring3,Hibernate4整合--SSH架構

  2、Hibernate 包

Struts2,Spring3,Hibernate4整合--SSH架構

  3、struts 包 (還欠 struts2-spring-plugin-2.3.28.jar 的包)

Struts2,Spring3,Hibernate4整合--SSH架構

  4、資料庫方面的包及junt4的包

Struts2,Spring3,Hibernate4整合--SSH架構

二、配置檔案

  1、beans.xml (具體要注意的已經注釋到 xml 中了,目前整合了Spring 與 hibernate4 )

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 打開Spring 的 Annotation 支援-->
    <context:annotation-config/>
    <!-- 設定Annotation 到 哪裡 找-->
    <context:component-scan base-package="org.cs"/>
    <!-- 打開Spring 的 Aop 代理 -->
    <aop:aspectj-autoproxy/>

    <!-- 使用 DBCP 建立 dateSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 配置 hibernate -->
        <!-- 配置連接配接池的初始值 為 1-->
        <property name="initialSize" value="1"/>
        <!-- 配置最小空閑時 -->
        <property name="minIdle" value="1"/>
        <!-- 最大連接配接池 -->
        <property name="maxTotal" value="100"/>
        <!-- 配置最大空閑時-->
        <property name="maxIdle" value="20"/>
        <!-- 配置等待時間-->
        <property name="maxWaitMillis" value="1000" />
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--  整合 hibernate4 建立 SessionFactory 工廠 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 注入 資料源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 設定 Spring 在哪裡找 實體類 -->
        <property name="packagesToScan">
            <value>org.cs.Model</value>
        </property>
        <!-- 配置 hibernate -->
        <property name="hibernateProperties">
            <!--<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>-->
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

    <!-- 配置 Spring 的事務管理 -->
    <!-- 建立事務管理器 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 配置 AOP -->
    <aop:config>
        <!-- 設定 aop:pointcut 表示 哪些 包.方法 需要加入事務-->
        <aop:pointcut id="allMethods"
                      expression="execution(* org.cs.Service.*.*(..))"/>
        <!-- 設定 aop:pointcut 表示 具體 需要加入事物的 方法-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
    </aop:config>
    <!-- 配置那些方法需要加入事務處理 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 讓所有方法加入事務 -->
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

</beans>      

  2、struts.xml (實作 Spring 與 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>
    <!-- 設定 url 字尾-->
    <constant name="struts.action.extension" value="action,do"/>
    <!-- 當 struts的配置檔案修改後,系統是否自動重新加載該檔案,預設值為false(生 産環境下使用),開發階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 表示 Action 由 Spring 依賴注入來注入-->
    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

    <!-- xxxAction 使用 struts 配置 -->
    <package name="default" namespace="/" extends="struts-default">
        <!--<!– 配置攔截器 , 要在 action 配置 後才生效–>-->
        <!--<interceptors>-->
            <!--<!– 這種 方式 在 action 中需要 配置 defaultStack –>-->
            <!--<!–<interceptor name="HelloInterceptor" class="Interceptor.HelloInterceptor"/>–>-->
            <!--<!– 這種方式 隻需要在 action 中 配置 HelloStack –>-->
            <!--<interceptor name="HelloInterceptor" class="Interceptor.HelloInterceptor"/>-->
            <!--<interceptor-stack name="HelloStack">-->
                <!--<interceptor-ref name="defaultStack"/>-->
                <!--<interceptor-ref name="HelloInterceptor"/>-->
            <!--</interceptor-stack>-->
        <!--</interceptors>-->

        <!-- 全局結果集 -->
        <global-results>
            <result name="error">/WEB-INF/Exception/MyException.jsp</result>
            <result name="exception">/WEB-INF/Exception/MyException.jsp</result>
        </global-results>

        <!-- 異常抛出 配置-->
        <global-exception-mappings>
            <exception-mapping exception="Exception.MyException" result="exception"></exception-mapping>
        </global-exception-mappings>

        <!-- 基于 通配符 的 方式 , 由于整合了Spring ,class = "應該是Spring所注入的對象"-->
        <action name="*_*" class="{1}Action" method="{2}">
            <!-- 攔截器 生效 ,需要有繼承 defaultStack -->
            <!--<interceptor-ref name="HelloInterceptor"/>-->
            <!--<interceptor-ref name="defaultStack"/>-->
            <!--<interceptor-ref name="HelloStack"/>-->
            <result name="success">/WEB-INF/jsp/{1}/{2}.jsp</result>
            <!-- 重定向 type = "redirect" -->
            <result type="redirect" name="redirect">/${url}</result>
        </action>

    </package>

</struts>      

  3、web.xml (建立 Spring 的 監聽器,Struts2 的 過濾器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- 建立Spring 的監聽器-->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring 的監聽器通過這個上下文參數 擷取 beans.xml 位置 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:beans.xml</param-value>
    </context-param>

    <!-- OpenSessionInViewFilter 過濾器 (連接配接 表現層與Dao層 資料互動)-->
    <filter>
        <filter-name>openSessionInViewerFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewerFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Struts 過濾器 -->
    <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>      

三、總結:

  趕時間做出來的,還沒寫完整(目前整合了hibernate4 與 Spring ,快斷網了。。),如果有錯的地方請大家指出,友善大家一起學習。

  SSH 架構整合 完成了,有點激動。。

  這隻是基礎的SSH整合,接下來我會分享具體的項目設計,希望大家一起學習,友善指出不足之處。

  傳送門 :

 github:https://github.com/ShunC/Invest