Struts2.3.15.3+hibernate-core-4.2.7+Spring3.2.5整合

提示:所需要的jar包,很多不过不需要记忆,所有类库放在一起就行了,谁要我发给他。921353419@qq.com  

jar 包拷入webRoot根目录下的lib目录下 

   

柿子还是软的捏,因为hibernate和Spring都不依赖与Web容器所以很轻松就可以整合   

1.在源码包下,也就是Src目录下新建名称为applicationContext.xml为文件如下图所示 

   

2.在新建的applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!--组件查找基本包 -->
    <context:component-scan base-package="com.bjtst">
        <!-- 告诉Spring组件使用了注解 -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!-- 使用alibaba数据源 -->
    <!-- 配置数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/bjtst" />
        <property name="username" value="root" />
        <property name="password" value="root" />

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="0" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="20" />
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="0" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />

        <!-- <property name="poolPreparedStatements" value="true" /> <property
            name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />

        <!-- 监控数据库 -->
        <!-- <property name="filters" value="mergeStat" /> -->
        <property name="filters" value="stat" />
    </bean>



    <!-- 使用c3po数据源产生SessionFactory -->
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" -->
    <!-- destroy-method="close"> -->
    <!-- <property name="driverClass" value="com.mysql.jdbc.Driver" /> -->
    <!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bjtst"
        /> -->
    <!-- <property name="user" value="root" /> -->
    <!-- <property name="password" value="root" /> -->
    <!-- </bean> -->

    <!-- 注解使用的seesionFactory 里面必须引用一个数据源对象 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 引用上面配置的数据源,id:dataSource -->
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.bjtst.model"><!-- PO从这个包里面扫描 -->
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- 对象关系映射到数据库使用更新模式,最常用 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 使用MySQL的方言 -->
                <!-- hibernate4新出的属性 表明将Session的产生和消亡托管给Spring管理 -->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
                </prop>
                <prop key="hibernate.jdbc.fatch_size">30</prop>
                <prop key="current_session_context_class">thread</prop>

            </props>
        </property>
    </bean>
    <!-- 开启注解事务驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 配置事务管理器里面引用了sessionFactory -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- Spring 事务管理切面,意思是对下面正则匹配的方法名称的执行配置事务隔离界别(propagation) -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- <tx:method name="create*" propagation="REQUIRED" /> -->
            <!-- <tx:method name="delete*" propagation="REQUIRED" /> -->
            <!-- <tx:method name="save*" propagation="REQUIRED" /> -->
            <!-- <tx:method name="*" propagation="REQUIRED" read-only="true"/> -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="merge*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="put*" propagation="REQUIRED" />
            <tx:method name="use*" propagation="REQUIRED" />
            <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 上面配置的事务管理切面的应用范围,哪些包中哪些类的具有哪些返回值的哪些具有哪些参数的哪些方法进行正则匹配 -->
    <!-- execution 表示执行条件 * 表示所有返回值包括void .. 表示所有子孙包 *Impl* 表示匹配该正则表达式的类名 .*
        表示匹配所有方法 (..) 表示匹配所有参数 -->
    <aop:config>
    <!-- 上面配置的事务管理切面的应用范围,哪些包中哪些类的具有哪些返回值的哪些具有哪些参数的哪些方法进行正则匹配 -->
    <!--
        execution    表示执行条件
            *        表示所有返回值包括void
            ..        表示所有子孙包
            *Impl*    表示匹配该正则表达式的类名
            .*        表示匹配所有方法
            (..)    表示匹配所有参数
     -->
        <aop:pointcut id="serviceMethods"
            expression="execution(* com.bjtst.service.*Service.*(..))" />
        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
    </aop:config>
</beans>

3.在web项目中引入Struts2并将其与Spring结合

在web.xml中添加Struts2、Spring的拦截器并增加一些常用的基础过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
    version="3.0">

    <!-- Welcome File List -->
    <welcome-file-list>
        <welcome-file>/jsplib/index.jsp</welcome-file>
    </welcome-file-list>

    <!-- WebApp Root -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>webapp.root</param-value>
    </context-param>
    
    
    <!-- Spring OpenSessionInView -->
     <filter>
         <filter-name>openSessionInView</filter-name>
         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        
           <init-param>
             <param-name>flushMode</param-name>
             <param-value>AUTO</param-value>
         </init-param>
         <init-param>
             <param-name>singleSession</param-name>
             <param-value>true</param-value>
         </init-param>
     </filter>
     <filter-mapping>
         <filter-name>openSessionInView</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

    <!-- Spring Encoding Filter -->
    <filter>
        <filter-name>encodingFilter</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>

    <!-- Spring Encoding Filter Mapping -->
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>





    <!-- Struts2 Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <!-- Struts2 Filter Mapping -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Log4j ConfigurationFile Location -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

    <!-- Spring Log4j Listener -->
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>

    <!-- Spring ConfigurationFile Location -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Spring Context Listener -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- Spring Web Request Listener -->
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>

    <!-- Spring Introspector Cleanup Listener -->
    <listener>
        <listener-class>
            org.springframework.web.util.IntrospectorCleanupListener
        </listener-class>
    </listener>

</web-app>

4.在WebRoot下新建 struts.xml 配置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>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!-- 指定由spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 是否启用开发模式 -->
    <constant name="struts.devMode" value="false" />
    <!-- struts配置文件改动后,是否重新加载 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置浏览器是否缓存静态内容 -->
    <constant name="struts.serve.static.browserCache" value="true" />
    <!-- 请求参数的编码方式 -->
    <constant name="struts.i18n.encoding" value="utf-8" />
    <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
    <constant name="struts.i18n.reload" value="false" />
    <!-- 文件上传最大值 -->
    <constant name="struts.multipart.maxSize" value="104857600" />
    <!-- 让struts2支持动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- Action名称中是否可使用斜线 -->
    <constant name="struts.enable.SlashesInActionNames" value="false" />
    <!-- 允许标签中使用表达式语法 -->
    <constant name="struts.tag.altSyntax" value="true" />
    <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
    <constant name="struts.dispatcher.parametersWorkaround" value="false" />
    <!-- 设置Convention插件是否从jar包中搜索Action类 [可选] 默认值为true -->
    <constant name="struts.convention.action.disableJarScanning" value="true" />
    <!-- 设置Convention插件文件协议类型 -->
    <constant name="struts.convention.action.fileProtocols" value="jar,wsjar" />
    <!-- 设置Convention插件需要搜索的jar包 -->
    <constant name="struts.convention.action.includeJars" value=".*?/sshe*.*?jar(!/)?" />
    
    <package name="default" namespace="/" extends="struts-default">
        <action name="*_*">
            <result>/jsplib/{1}/{2}.jsp</result>
        </action>
    </package>

</struts>

 


此时Spring3+hibernate4+Struts2已经紧紧的结合在一起了。

spring就像一个讨人喜欢的孩子,左手攥着Hibernate右手拉着Struts2

 

 

posted @ 2014-04-17 10:45  王艺博  阅读(298)  评论(0编辑  收藏  举报