隨筆-10  評論-11  文章-20  trackbacks-0
          S2SH框架集成步驟:

          1、框架依賴jar文件
          (這些jar包可以去官網自己下載,地址不在連接)

          1.1 sping     所依賴工程 spring-framework-2.5.6
               dist\spring.jar          
               lib\cglib\cglib-nodep-2.1_3.jar        
               lib\jakarta-commons\commons-logging.jar        
               lib\aspectj\aspectjweaver.jar和aspectjrt.jar      lib\j2ee\common-annotations.jar

          1.2 hibernate   所依賴工程 hibernate-distribution-3.3.2.GA
               hibernate3.jar
               lib\required\jta-1.1.jar        javassist-3.9.0.GA.jar        dom4j-1.6.1.jar        commons-collections-3.1.jar        antlr-2.7.6.jar        slf4j-api-1.5.8.jar
               lib\bytecode\cglib\cglib-2.2.jar
               [二級緩存可選]lib\optional\oscache\oscache-2.1.jar        同時需要把\project\etc\oscache.properties 拷貝到src 下
               [二級緩存可選]lib\optional\ehcache\ehcache-1.2.3.jar  同時需要把\project\etc\ehcache.xml
               [二級緩存可選]lib\optional\c3p0\    配置c3p0數據庫連接池的使用 作用等同于apache的dbcp

               *使用hibernate注解:hibernate-annotations-3.4.0.GA\
               hibernate-annotations.jar
               lib\hibernate-commons-annotations.jar
               lib\ejb3-persistence.jar

               *若使用slf的日志還需要:slf4j-1.5.8
               slf4j-nop-1.5.8.jar

          1.3 struts2  所依賴工程  struts-2.1.8.1
               lib目錄下的:
                  struts2-core-2.1.8.1.jar
                  xwork-core-2.1.6.jar
                  ognl-2.7.3.jar
                      freemarker-2.3.15.jar
                  commons-fileupload-1.2.1.jar
                  commons-io-1.3.2.jar
                  struts2-spring-plugin-2.1.8.1.jar
                       aopalliance-1.0.jar
                  classworlds-1.1.jar
                  commons-beanutils-1.7.0.jar
                  commons-chain-1.2.jar
                  commons-collections-3.2.jar  在hibernate中已經引用          
                  commons-digester-2.0.jar        
                  commons-lang-2.3.jar
                  commons-logging-1.0.4.jar  此文件在spring中已存在
                  commons-logging-api-1.1.jar
                  commons-validator-1.3.1.jar
                  ezmorph-1.0.3.jar
                  json-lib-2.1.jar 若使用json可選
                  oro-2.0.8.jar
                  oval-1.31.jar


          2、框架的配置文件

          2.1 spring框架配置文件及集成hibernate、二級緩存
                  $WEB_ROOT/WEB-INF/applicationContext.xml
                      src/ehcache.xml

          <?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: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-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/aop> <http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
                     <http://www.springframework.org/schema/tx> <http://www.springframework.org/schema/tx/spring-tx-2.5.xsd>">
              
              <!-- 配置數據源 -->
                  <context:property-placeholder location="classpath:jdbc4mysql.properties" />
                  <bean id="dataSource"
                          class="org.apache.commons.dbcp.BasicDataSource"
                          destroy-method="close">
                          <property name="driverClassName" value="${driverClassName}" />
                          <property name="url" value="${url}" />
                          <property name="username" value="${username}" />
                          <property name="password" value="${password}" />
                          <!-- 連接池啟動時的初始值 -->
                          <property name="initialSize" value="${initialSize}" />
                          <!-- 連接池的最大值 -->
                          <property name="maxActive" value="${maxActive}" />
                          <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
                          <property name="maxIdle" value="${maxIdle}" />
                          <!--  最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以保證應急 -->
                          <property name="minIdle" value="${minIdle}" />
                  </bean>
                  
                   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                   <property name="dataSource" ref="dataSource"/>
                   <property name="mappingResources">
                      <list>
                        <value>cn/tsp2c/sshdemo/domain/User.hbm.xml</value>
                      </list>
                   </property>
                   <property name="hibernateProperties">
                      <value>
                          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                          hibernate.hbm2ddl.auto=update
                          hibernate.show_sql=false
                          hibernate.format_sql=false
                          hibernate.cache.use_second_level_cache=true
                          hibernate.cache.use_query_cache=false
                          hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                        </value>
                   </property>
                   </bean>
                  
              <!-- 配置事務管理器 -->
                  <bean id="txManager"
                          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                          <property name="dataSource" ref="dataSource" />
                  </bean>
                  
                  <!-- spring支持兩種事務聲明方式:注解和xml配置,建議使用注解方式 -->
                  <!-- 采用@Transactional注解方式使用事務,在dao類及方法需用注解方式標明事務方式 -->
                  <tx:annotation-driven transaction-manager="txManager" />   

               <!-- 用戶DAO實現,實現方式:JDBC
              <bean id="userDao" class="cn.tsp2c.sshdemo.dao.impl.UserDaoImpl">
                  <property name="dataSource" ref="dataSource"/>
              </bean>
              -->
              <!-- 用戶DAO實現,實現方式:hibernte  -->    
              <bean id="userDao" class="cn.tsp2c.sshdemo.dao.impl.hibernate.UserDaoHibernateImpl">
                  <property name="sessionFactory" ref="sessionFactory"/>
              </bean>
             
               
                  <!-- 用戶服務bean -->
                  <bean id="userService" class="cn.tsp2c.sshdemo.service.impl.UserServiceImpl" >
                     <property name="userDao" ref="userDao"/>
                  </bean>
                  
                  <!-- 用戶Action bean,scope=prototype符合struts2的action生成機制 -->
              <bean id="userAction" class="cn.tsp2c.sshdemo.web.action.UserAction" scope="prototype">
                  <property name="userService" ref="userService"/>
              </bean>
          </beans>

          其中集成hibernate需要屬性文件:jdbc4mysql.properties

          driverClassName=com.mysql.jdbc.Driver
          url=jdbc\:mysql\://localhost\:3306/sshdemodb?useUnicode\=true&characterEncoding\=utf-8
          username=root
          password=1234
          initialSize=1
          maxActive=500
          maxIdle=2
          minIdle=1

          需要配置hibernate的二級緩存,如使用ehcache:在src路徑下加入ehcache.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!--
               defaultCache 設置節點為缺省的緩存策略
               maxElementsInMemory 設置內存中最大允許存在的對象數量
               eternal 設置緩存中的對象是否永遠不過期
               overflowToDisk 把超出內存設定的溢出的對象存放到硬盤上
               timeToIdleSeconds 設置緩存對象空閑多長時間就過期,過期的對象會被清除掉
               timeToLiveSeconds 設置緩存對象總的存活時間
               diskPersistent 當jvm結束時是否把內存中的對象持久化到磁盤
               diskExpiryThreadIntervalSeconds 設置用于清除過期對象的監聽線程的輪詢時間
          -->
          <ehcache>
              <diskStore path="e:\cache"/>
              <defaultCache  maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
                  timeToIdleSeconds="120"
                  timeToLiveSeconds="180"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="60"/>
                  <cache name="cn.tsp2c.sshdemo.domain.User" maxElementsInMemory="100" eternal="false"
              overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="500" diskPersistent="false"/>
          </ehcache>

          2.2 struts2的配置文件: src/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>
              <!-- 指定web應用的默認編碼為UTF-8,功能等同于request.setCharacterEncoding() -->
              <constant name="struts.i18n.encoding" value="UTF-8"/>
              <!-- 指定struts2的請求處理后綴,匹配*.action的所有請求 -->
              <constant name="struts.action.extension" value="action"/>
              <!-- 關閉struts2的!動態方法調用,建議使用通配符匹配方式實現動態方法調用 -->
              <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
              <!-- 設置瀏覽器是否緩存靜態頁面,默認為true,建議:開發階段關閉,生產環境打開 -->
              <constant name="struts.serve.static.browserCache" value="false" />
              <!-- 當struts.xml修改時自動重新加載,默認為false。建議:開發階段打開,生產環境關閉 -->
              <constant name="struts.configuration.xml.reload" value="true"/>
              <!-- 開發模式下打開,便于打印詳細日志,生產環境下關閉 -->
              <constant name="struts.devMode" value="true" />
              <!-- 設置視圖主題為css_xhtml -->
              <constant name="struts.ui.theme" value="simple" />
              <!-- 指定struts中action交由spring創建 -->
              <constant name="struts.objectFactory" value="spring"/>
              
              <package name="base" extends="struts-default">
                  <global-results>
                         <result name="message">/WEB-INF/page/message.jsp</result>
                         <result name="error">/WEB-INF/page/error.jsp</result>
                  </global-results>
              </package>
              
              <package name="user" namespace="/user" extends="base">
                  <action name="login" class="cn.tsp2c.sshdemo.web.action.LoginAction" method="execute">
                      <result name="success">/index.jsp</result>
                      <result name="input">/login.jsp</result>
                  </action>
                  <action name="user_*" class="cn.tsp2c.sshdemo.web.action.UserAction" method="{1}">
                      <result name="list">/userlist.jsp</result>
                      <result name="add" type="redirect">/useradd.jsp</result>
                  </action>
              </package>
          </struts>


          3、需要把spring、strusts2框架注入到web容器(hibernate框架被spring集成,和web容器沒有關系。所以不需要在web.xml中配置)

          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>">
              <!-- 配置spring的xml文件,若配置文件有多個,可用,或空格分隔 -->
                  <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>
                                  /WEB-INF/applicationContext.xml
                          </param-value>
                  </context-param>

                  <!-- web容器加載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>
                  </filter-mapping>

                  <!-- web容器加載spring配置 -->
                  <listener>
                          <listener-class>
                                  org.springframework.web.context.ContextLoaderListener
                          </listener-class>
                  </listener>

                  <!-- web容器增加字符集轉換的過濾器,由于struts2框架解決了字符集轉碼,此配置可以注釋掉
                  <filter>
                          <filter-name>encoding</filter-name>
                          <filter-class>
                                  org.springframework.web.filter.CharacterEncodingFilter
                          </filter-class>
                          <init-param>
                                  <param-name>encoding</param-name>
                                  <param-value>UTF-8</param-value>
                          </init-param>
                  </filter>
                  <filter-mapping>
                          <filter-name>encoding</filter-name>
                          <url-pattern>/*</url-pattern>
                  </filter-mapping>
              -->
                  <!-- 解決hibernate的session關閉導致延遲加載異常的問題 -->
                  <!--  
                          <filter>
                          <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
                          <filter-class>
                            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
                          </filter-class>
                          </filter>
                          <filter-mapping>
                            <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
                            <url-pattern>/*</url-pattern>
                          </filter-mapping>
                  -->

                  <welcome-file-list>
                          <welcome-file>index.jsp</welcome-file>
                  </welcome-file-list>
          </web-app>

          3.1 web容器集成spring

                  <!-- 配置spring的xml文件,若配置文件有多個,可用,或空格分隔 -->
                  <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>
                                  /WEB-INF/applicationContext.xml
                          </param-value>
                  </context-param>

                  <!-- web容器加載spring配置 -->
                  <listener>
                          <listener-class>
                                  org.springframework.web.context.ContextLoaderListener
                          </listener-class>
                  </listener>


          3.2 web容器集成struts2
                  <!-- web容器加載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>
                  </filter-mapping>

          3.3 關于在session范圍內解決hibernate的延遲加載問題

          <!-- 解決hibernate的session關閉導致延遲加載異常的問題 -->
          <filter>
              <filter-name>HibernateSessionInViewFilter</filter-name>
              <filter-class>
                org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
              </filter-class>
          </filter>
          <filter-mapping>
              <filter-name>HibernateSessionInViewFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>


          4、如何使用s2sh框架實現MVC

          4.1 action

          struts.xml:
              <!-- 指定struts中action交由spring創建 -->
              <constant name="struts.objectFactory" value="spring"/>

          <action name="user_*" class="cn.tsp2c.sshdemo.web.action.UserAction" method="{1}">
              <result name="list">/userlist.jsp</result>
              <result name="add" type="redirect">/useradd.jsp</result>
          </action>

          在spring中配置:
          <!-- 用戶Action bean,scope=prototype符合struts2的action生成機制 -->
              <bean id="userAction" class="cn.tsp2c.sshdemo.web.action.UserAction" scope="prototype">
                  <property name="userService" ref="userService"/>
              </bean>

          4.2 service
          <!-- 用戶服務bean -->
          <bean id="userService" class="cn.tsp2c.sshdemo.service.impl.UserServiceImpl" >
             <property name="userDao" ref="userDao"/>
          </bean>

          4.3 dao
          <!-- 用戶DAO實現,實現方式:JDBC -->
          <bean id="userDao" class="cn.tsp2c.sshdemo.dao.impl.UserDaoImpl">
              <property name="dataSource" ref="dataSource"/>
          </bean>
          posted on 2011-03-25 20:18 Soap MacTavish 閱讀(3305) 評論(1)  編輯  收藏

          評論:
          # re: SSH框架的集成步驟 2014-04-24 14:03 | 標桿
          靶標城  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 远安县| 乌兰县| 新田县| 共和县| 新蔡县| 将乐县| 荆门市| 崇礼县| 滦平县| 集贤县| 邹城市| 开封市| 临朐县| 闸北区| 屯留县| 乌拉特前旗| 同江市| 泽州县| 东方市| 石狮市| 右玉县| 贡嘎县| 周口市| 辽阳市| 四川省| 壶关县| 神农架林区| 仙桃市| 新河县| 绥德县| 永嘉县| 剑阁县| 静宁县| 宁海县| 溆浦县| 汶川县| 彭州市| 中山市| 宾川县| 怀安县| 富裕县|