ssh整合

  • 导包
  • struts.xml
<?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>
    <!-- #  struts.objectFactory = spring   将action的创建交给spring容器    
            struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
            (必须)
            -->
    <constant name="struts.objectFactory" value="spring"></constant>

    <package name="crm" namespace="/" extends="struts-default" >
        <global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
        </global-exception-mappings>
         <!--
             整合方案1:class依然是类全名(不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.)
            整合方案2:class属性上填写spring中action对象的BeanName
                完全由spring管理action生命周期,包括Action的创建
                注意:需要手动组装依赖属性
          -->                            (spring中bean的name名)
        <action name="UserAction_*" class="userAction" method="{1}" >
            <result name="toHome" type="redirect" >/index.htm</result>
            <result name="error" >/login.jsp</result>
        </action>
    </package>
</struts>
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" 
        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-4.2.xsd 
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!-- 读取db.properties文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置c3p0连接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
        <property name="driverClass" value="${jdbc.driverClass}" ></property>
        <property name="user" value="${jdbc.user}" ></property>
        <property name="password" value="${jdbc.password}" ></property>
    </bean>

    <!-- 核心事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 将SessionFactory配置到spring容器中 -->
    <!-- 加载配置方案1:仍然使用外部的hibernate.cfg.xml配置信息 -->
    <!-- <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <property name="configLocation" value="classpath:hibernate.cfg.xml" ></property>
    </bean> -->
    <!-- 加载配置方案2:在spring配置中放置hibernate配置信息 -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
        <property name="dataSource" ref="dataSource" ></property>
        <!-- 配置hibernate基本信息 -->
        <property name="hibernateProperties">
            <props>
                <!--  必选配置 -->
            <!--    <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop>
                <prop key="hibernate.connection.username" >root</prop>
                <prop key="hibernate.connection.password" >1234</prop> -->
                <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>

                <!--  可选配置 -->
                <prop key="hibernate.show_sql" >true</prop>
                <prop key="hibernate.format_sql" >true</prop>
                <prop key="hibernate.hbm2ddl.auto" >update</prop>
            </props>
        </property>
        <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
        <property name="mappingDirectoryLocations" value="classpath:cn/itcast/domain" ></property>
    </bean>

    <!-- action -->
    <!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
    <bean name="userAction" class="cn.itcast.web.action.UserAction" scope="prototype" >
        <property name="userService" ref="userService" ></property>
    </bean>
    <!-- service -->
    <bean name="userService" class="cn.itcast.service.impl.UserServiceImpl" >
        <property name="ud" ref="userDao" ></property>
    </bean>
    <!-- dao -->
    <bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl" >
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>
</beans>
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssh_crm</display-name>


  <!-- 让spring随web启动而创建的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置spring配置文件位置参数 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 扩大session作用范围
    注意: 任何filter一定要在struts的filter之前调用
   -->
   <filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <!-- struts2核心过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>openSessionInView</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

web.xml 一个spring监听器一个Struts2的过滤器一个hibernate过滤器 Struts2的过滤器要在最后

对于ssh的整合来说,在包齐全的情况下,web.xml配置让spring随web启动而启动的监听器ContextLoaderListener,并且配置spring配置文件路径,hibernate因为有懒加载要扩大session作用范围,配置OpenSessionInViewFilter过滤器,Struts的核心过滤器StrutsPrepareAndExecuteFilter。

struts.xml开启Struts2的spring常量,配置action时将class属性改变为spring中bean的name名。

spring与hibernate整合原理:
将sessionFactory对象交给spring容器管理
hibernate.cfg.xml里的配置改到spring中

<!-- 加载配置方案2:在spring配置中放置hibernate配置信息 -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
        <property name="dataSource" ref="dataSource" ></property>
        <!-- 配置hibernate基本信息 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
                <!--  可选配置 -->
                <prop key="hibernate.show_sql" >true</prop>
                <prop key="hibernate.format_sql" >true</prop>
                <prop key="hibernate.hbm2ddl.auto" >update</prop>
            </props>
        </property>
        <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
        <property name="mappingDirectoryLocations"  value="classpath:cn/wutest/domain" ></property>
    </bean>

Dao类创建:继承HibernateDaoSupport

struts2后加载文件中的配置会将先加载文件中的配置覆盖。
加载文件的顺序
1.default.properties文件
作用:定义了struts2框架中所有常量
位置: org/apache/struts2/default.properties

2.struts-default.xml
作用:配置了bean,interceptor,result等。
位置:在struts的core核心jar包.

struts-plugin.xml
它是struts2框架中所使用的插件的配置文件。
struts.xml
我们使struts2所使用的配置文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值