SSH框架的集成步骤

SSH框架的集成步骤
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>

你可能感兴趣的:(SSH框架的集成步骤)