5.2 SSH整合2

纯XML的整合,保留spring和struts2的主配置文件,hibernate的主配置文件内容配到spring的配置文件中。

第一个版本存在的问题
测试类无法运行
配置杂,分开写
去掉hibernate主配置文件
hibernateTemplate写多了也是重复代码

  1. 删除hibernate.cfg.xml文件
  2. 将配置转移到applicationContext.xml中
    原hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!-- 配置SessionFactory-->
    <session-factory>
        <!-- 第一部分:连接数据库的信息 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/day67_ssh</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">suntong</property>
        <!-- 数据库的方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 第二部分:hibernate的可选配置 -->
        <!-- 是否显示hibernate生成的SQL语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 是否使用格式化输出sql语句到控制台 -->
        <property name="hibernate.format_sql">false</property>
        <!-- 配置hibernate采用何种方式生成DDL语句 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 设置hibernate的连接池提供商 -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <!-- 把session和线程绑定,从而实现一个线程只有一个Session 
        <property name="hibernate.current_session_context_class">thread</property>-->
        <property name="hibernate.current_session_context_class">
            org.springframework.orm.hibernate5.SpringSessionContext
        </property>
        <!-- 第三部分:映射配置文件的位置 -->
        <mapping resource="com/itheima/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 配置service-->
    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
    </bean>
    
    <!-- 配置dao -->
    <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    
    <!-- 配置一个hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <!-- 看源码,需要sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- spring接管sessionFactory的创建,配置sessionFactory
        需要用spring提供的sessionFactory:LocalSessionFactoryBean
        创建SessionFactory有三部分必不可少信息,再hibernate主配置文件中都有,把hibernate主配置文件的位置注入进来,加上classpath说明是根路径
     -->
    
    
    <!-- ----------------------------------------------------------- -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 连接数据库 用连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- hibernate的可选配置 -->
        <property name="hibernateProperties">
            <props>
                <!-- 数据库的方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 是否显示hibernate生成的SQL语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 是否使用格式化输出sql语句到控制台 -->
                <prop key="hibernate.format_sql">false</prop>
                <!-- 配置hibernate采用何种方式生成DDL语句 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- 把session和线程绑定,从而实现一个线程只有一个Session-->
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate5.SpringSessionContext
                </prop>
            </props>
        </property>
        <!-- 映射文件的位置 
            
            mappingResources:它是一个注入String数组类型的数据,提供的是映射文件的位置,有几个映射文件,就需要写几个
            mappingDirectoryLocations:它是注入一个Resource类型的数组,提供的是映射文件所在的目录,此属性一般多用于一个项目有多个地方存放映射配置
                //服务端
                    server_domain
                //移动端
                    mobile_domain
            mappingLocations:注入一个Resource类型的数组,提供的是映射文件的位置,它可以使用通配符
            
        -->
        <property name="mappingLocations">
            <array>
                <value>classpath:com/itheima/domain/*.hbm.xml</value>
            </array>
        </property>
    </bean>
    
    
    
    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day67_ssh"></property>
        <property name="user" value="root"></property>
        <property name="password" value="suntong"></property>
    </bean>
    
    
    <!-- -------------------------------------------------------------------------------- -->
    
    
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 配置事务的通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置aop -->
    <aop:config>
        <!-- 配置切入点表达式 -->
        <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
        <!-- 建立切入点表达式和事务通知的关联 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
    </aop:config>
    
</beans>

实验时eclipse崩溃,tomcat又不好使了
这次我们通过tomcat bin目录下关闭和启动



成功运行

优化

将applicationContext.xml移到根路径下
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>day67_01S2S4H5</display-name>
  
  <!-- 配置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>
  <!-- 配置spring提供的监听器,用于监听servletContext对象创建,同时为我们创建spring的容器 
        默认情况下:它只能加载位置是在WEB-INF目录中的spring配置文件,同时文件名称必须是applicationContext.xml
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
    
    
    
    
    
    <!-- 手动指定spring的配置文件位置,需要使用ServletContext的初始化参数 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring/application.xml</param-value>
    </context-param>
    
  
  
  
  
  
  
  <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>

依旧可以


不如把struts.xml也挪一下吧!!!

struts.xml由web.xml文件中的核心控制器加载
需要修改

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>day67_01S2S4H5</display-name>
  
  <!-- 配置struts2的核心过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    
    <!-- 手动指定struts2配置文件的位置,此处的配置绝大多数固定 
        必须写、用了插件必须写,自己文件的位置
    -->
    <init-param>
        <param-name>config</param-name>
        <param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xml</param-value>
    </init-param> 
  </filter>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置spring提供的监听器,用于监听servletContext对象创建,同时为我们创建spring的容器 
        默认情况下:它只能加载位置是在WEB-INF目录中的spring配置文件,同时文件名称必须是applicationContext.xml
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 手动指定spring的配置文件位置,需要使用ServletContext的初始化参数 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring/application.xml</param-value>
    </context-param>
    
  <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>

成功

拆解配置文件applicationContext.xml

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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 引入其他spring配置文件 -->
    <import resource="applicationContext-customer.xml"/>
    <import resource="applicationContext-jdbc.xml"/>
    <import resource="applicationContext-tx.xml"/>
</beans>

applicationContext-customer.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 配置和customer模块相关的bean:三层 -->
    <!-- 配置service-->
    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
    </bean>
    
    <!-- 配置dao -->
    <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    
    
</beans>


applicationContext-jdbc.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 和jdbc配置相关的 -->
    
    
    <!-- 配置一个hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <!-- 看源码,需要sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- spring接管sessionFactory的创建,配置sessionFactory
        需要用spring提供的sessionFactory:LocalSessionFactoryBean
        创建SessionFactory有三部分必不可少信息,再hibernate主配置文件中都有,把hibernate主配置文件的位置注入进来,加上classpath说明是根路径
     -->
    
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 连接数据库 用连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- hibernate的可选配置 -->
        <property name="hibernateProperties">
            <props>
                <!-- 数据库的方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 是否显示hibernate生成的SQL语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 是否使用格式化输出sql语句到控制台 -->
                <prop key="hibernate.format_sql">false</prop>
                <!-- 配置hibernate采用何种方式生成DDL语句 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- 把session和线程绑定,从而实现一个线程只有一个Session-->
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate5.SpringSessionContext
                </prop>
            </props>
        </property>
        <!-- 映射文件的位置 
            
            mappingResources:它是一个注入String数组类型的数据,提供的是映射文件的位置,有几个映射文件,就需要写几个
            mappingDirectoryLocations:它是注入一个Resource类型的数组,提供的是映射文件所在的目录,此属性一般多用于一个项目有多个地方存放映射配置
                //服务端
                    server_domain
                //移动端
                    mobile_domain
            mappingLocations:注入一个Resource类型的数组,提供的是映射文件的位置,它可以使用通配符
            
        -->
        <property name="mappingLocations">
            <array>
                <value>classpath:com/itheima/domain/*.hbm.xml</value>
            </array>
        </property>
    </bean>
    
    
    
    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day67_ssh"></property>
        <property name="user" value="root"></property>
        <property name="password" value="suntong"></property>
    </bean>

</beans>



applicationContext-tx.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 和事务相关的配置 -->
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 配置事务的通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置aop -->
    <aop:config>
        <!-- 配置切入点表达式 -->
        <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
        <!-- 建立切入点表达式和事务通知的关联 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
    </aop:config>
    
</beans>

拆解struts.xml

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>
    
    <constant name="struts.devMode" value="true"></constant>

    <!-- 配置公共包,有公共的配置就写在此处 -->
    <package name="myDefault" extends="struts-default" abstract="true">
        
    </package>
    
    <!-- 引入其他struts2配置文件 -->
    <include file="config/struts/struts-customer.xml"></include>
</struts>

struts-customer.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>
    
    <!-- 配置和客户管理相关动作 -->
    <package name="customer" extends="myDefault" namespace="/customer">
        <action name="addUICustomer" class="com.itheima.web.action.CustomerAction" method="addUICustomer">
            <result name="addUICustomer">/jsp/customer/add.jsp</result>
        </action>
        
        <action name="findAllCustomer" class="com.itheima.web.action.CustomerAction" method="findAllCustomer">
            <result name="findAllCustomer">/jsp/customer/list.jsp</result>
        </action>
    </package>
</struts>

运行


取出CustomerDaoImpl中的重复代码

CustomerDaoImpl.java

package com.itheima.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.itheima.dao.ICustomerDao;
import com.itheima.domain.Customer;

/*
 * 客户的持久层实现
 * */
public class CustomerDaoImpl extends HibernateDaoSupport implements ICustomerDao {
    


    @Override
    public List<Customer> findAllCustomer() {
        //return session.createQuery("from Customer").list();
        return (List<Customer>) getHibernateTemplate().find("from Customer");
    }

    @Override
    public void saveCustomer(Customer customer) {
        getHibernateTemplate().save(customer);
    }

}

applicationContext-jdbc.xml

去除hibernateTemplate的注入配置

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 和jdbc配置相关的 -->
    
    
    <!-- 配置一个hibernateTemplate 
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        看源码,需要sessionFactory
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    -->
    
    <!-- spring接管sessionFactory的创建,配置sessionFactory
        需要用spring提供的sessionFactory:LocalSessionFactoryBean
        创建SessionFactory有三部分必不可少信息,再hibernate主配置文件中都有,把hibernate主配置文件的位置注入进来,加上classpath说明是根路径
     -->
    
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 连接数据库 用连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- hibernate的可选配置 -->
        <property name="hibernateProperties">
            <props>
                <!-- 数据库的方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 是否显示hibernate生成的SQL语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 是否使用格式化输出sql语句到控制台 -->
                <prop key="hibernate.format_sql">false</prop>
                <!-- 配置hibernate采用何种方式生成DDL语句 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- 把session和线程绑定,从而实现一个线程只有一个Session-->
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate5.SpringSessionContext
                </prop>
            </props>
        </property>
        <!-- 映射文件的位置 
            
            mappingResources:它是一个注入String数组类型的数据,提供的是映射文件的位置,有几个映射文件,就需要写几个
            mappingDirectoryLocations:它是注入一个Resource类型的数组,提供的是映射文件所在的目录,此属性一般多用于一个项目有多个地方存放映射配置
                //服务端
                    server_domain
                //移动端
                    mobile_domain
            mappingLocations:注入一个Resource类型的数组,提供的是映射文件的位置,它可以使用通配符
            
        -->
        <property name="mappingLocations">
            <array>
                <value>classpath:com/itheima/domain/*.hbm.xml</value>
            </array>
        </property>
    </bean>
    
    
    
    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day67_ssh"></property>
        <property name="user" value="root"></property>
        <property name="password" value="suntong"></property>
    </bean>

</beans>



在applicationContext-customr.xml中对应的位子注入sessionFactory
applicationContext-customer.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 配置和customer模块相关的bean:三层 -->
    <!-- 配置service-->
    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
    </bean>
    
    <!-- 配置dao -->
    <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    
</beans>

spring接管action层

applicationContext-customer.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 配置和customer模块相关的bean:三层 -->
    
    <!-- 配置action -->
    <bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype">
        <property name="customerService" ref="customerService"></property>
    </bean>
    
    <!-- 配置service-->
    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
    </bean>
    
    <!-- 配置dao -->
    <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    
</beans>



struts-customer.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>
    
    <!-- 配置和客户管理相关动作 -->
    <package name="customer" extends="myDefault" namespace="/customer">
        <action name="addUICustomer" class="customerAction" method="addUICustomer">
            <result name="addUICustomer">/jsp/customer/add.jsp</result>
        </action>
        
        <action name="findAllCustomer" class="customerAction" method="findAllCustomer">
            <result name="findAllCustomer">/jsp/customer/list.jsp</result>
        </action>
    </package>
</struts>


完成

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,183评论 6 516
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,850评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,766评论 0 361
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,854评论 1 299
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,871评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,457评论 1 311
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,999评论 3 422
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,914评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,465评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,543评论 3 342
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,675评论 1 353
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,354评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,029评论 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,514评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,616评论 1 274
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,091评论 3 378
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,685评论 2 360

推荐阅读更多精彩内容