整合ssh框架的配置文件

本文详细介绍SSH(Struts+Spring+Hibernate)框架的整合过程,包括配置数据源、搭建Hibernate的SessionFactory、配置事务管理器及监听器等核心步骤。此外,还介绍了如何在Struts中进行映射配置,并展示了web.xml中的关键配置。
ssh整合
一.applicationContext.xml
        1配置数据源
            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                    <property name="location" value="classpath:db.properties"></property>
            </bean>
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                    <property name="driverClassName" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
            </bean>
        2.配置hibernate的sessionFactory工厂
            <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                    <property name="dataSource" ref="dataSource"/>//引用数据源
                    <property name="hibernateProperties">
                        <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>//配置相关数据库库的方言(为了检查hql翻译sql语句的语法)
                            <prop key="hibernate.show_sql">false</prop>//是否显示sql语句
                            <prop key="hibernate.format_sql">false</prop>//格式化sql语句
                        </props>
                    </property>
                    <!-- 直接配置注解的实体类(不用再编写配置xxx.hbm.xml文件)-->
                    <property name="annotatedClasses">
                        <list>
                            <value>cn.sxt.vo.ceshi.Module</value>
                            <value>cn.sxt.vo.ceshi.Suite</value>
                        </list>
                    </property>
            </bean>
        3.配置事务管理器(此管理器是hibernate提供的,其实spring也有事务管理器)
            <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
                    <property name="sessionFactory" ref="sessionFactory"/>
            </bean>
        4.配置事务监听器
            <tx:advice id="txAdvice" transaction-manager="txManager">
                    <tx:attributes>
                        <tx:method name="*" propagation="REQUIRED"/>//监听所有的方法
                    </tx:attributes>
            </tx:advice>
        5.配置事务提交的切入点
            <aop:config>
                    <aop:pointcut expression="execution(* cn.sxt.service..*.*(..))" id="pointcut"/>//切入点
                    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>//引用事务监听器
            </aop:config>
        6.配置注解扫描器
            <context:component-scan base-package="cn.sxt"/>//扫描sxt包的所有类里面的注解
二.strut.xml
    添加相应的映射文件就可以了
        例如:<struts>
                   <package name="yxl" extends="struts-default"></package>
                   <include file="config/yxl/struts/ceshi.xml"></include>
                   <include file="config/yxl/struts/suite.xml"></include>
                   <include file="config/yxl/struts/module.xml"></include>
             </struts>
三.web.xml
    1.配置spring容器相关的资源文件和创建容器的监听类
        <!--定位spring配置文件-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <!--web容器启动时就会触发此监听器调用相应的方法创建spring容器-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>//
        </listener>
    2.配置session的关闭的一个过滤器
      <filter>
        <filter-name>osiv</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>osiv</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    2.配置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>*.action</url-pattern>
      </filter-mapping>
    
   
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值