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>


完成

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容