1.首先,我们需要先分析一下,我们的spring容器在web环境中,只需要一份就可以了

另外,就是我们的spring容器,要在我们tomcat启动的时候就创建好了(包括其中的spring的对象),怎么保证我们的spring容器能创建呢?

我们可以配合监听器来创建我们的spring容器,然后我们怎么实现我们的监听器呢?

当ServletContext创建成功,就说明tomcat正常启动了,我们使用监听器监听我们的ServletContext,如果创建成功,加载配置文件,创建spring容器

2.我们先在我们的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_spring</display-name>
<!-- 配置spring的监听器
加载spring的配置文件,根据配置文件创建我们的spring对象
默认加载的spring配置文件位置:WEB-INF下的applicationContext.xml
-->
<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>
<filter>
<filter-name>ssh</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ssh</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>

3.  在我们的struts中根据容器获取对象

第一种是使用struts的 插件包(这种方式使用的比较少)

第二种是把struts的动作类交给spring进行管理

applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring的配置文件:导入约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
"> <!-- 自定义的java对象交给spring进行管理 -->
<bean id="customerAction" scope="prototype" class="com.itheima.action.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 第三方的jar包中的java对象交给spring进行管理 -->
<!-- 创建一个hibernateTemplate对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建sessionFactory对象 -->
<!-- 当spring和hibernate整合时,这个sessionFactory的实现类是由spring提供的:LocalSessionFactoryBean,
创建sessionFactory是根据hibernate的核心配置文件
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--配置hibernate的事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理通知 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的AOP -->
<aop:config>
<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>

在struts.xml中配置

<!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="struts-default" namespace="/customer">
<!--
1.class: 全限定类名
反射的请示创建动作类对象
class:需要修改为容器中动作类的bean的唯一标识
-->
<action name="addCustomerUI" class="customerAction" method="addCustomerUI">
<result name="success">/jsp/customer/add.jsp</result>
</action>
<action name="getAllCustomer" class="customerAction" method="getAllCustomer">
<result name="success">/jsp/customer/list.jsp</result>
</action>
</package>
</struts>

我们在CustomerAction中

package com.itheima.action;

import java.util.List;

import com.itheima.entity.Customer;
import com.itheima.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer(); private CustomerService customerService;
private List<Customer> customers; public void setCustomers(List<Customer> customers) {
this.customers = customers;
} public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
} public List<Customer> getCustomers() {
return customers;
} public Customer getModel() {
return customer;
} //进入添加页面
public String addCustomerUI(){
return this.SUCCESS;
}
//进入列表页面
public String getAllCustomer(){
customers = customerService.getAllCustomer();
return this.SUCCESS;
} }

最后.测试我们的结果,访问http://localhost:8080/ssh_spring/

每天进步一点点,今天就到这儿吧!

ssh整合之五struts和spring整合的更多相关文章

  1. 【SSH框架】系列之 Spring 整合 Hibernate 框架

    1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...

  2. Struts2框架04 struts和spring整合

    目录 1 servlet 和 filter 的异同 2 内存中的字符编码 3 gbk和utf-8的特点 4 struts和spring的整合 5 struts和spring的整合步骤 6 spring ...

  3. ssh整合之三hibernate和spring整合

    1.拷贝我们的spring事务控制所需的jar包 2.在spring容器中配置我们的hibernateTemplate以及事务管理器 <?xml version="1.0" ...

  4. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  5. 从0开始整合SSM框架--2.spring整合mybatis

    依赖:<properties> <!-- spring版本号 --> <spring.version>4.1.3.RELEASE</spring.versio ...

  6. SSH开发实践part4:Spring整合Struts

    1 好了,前面spring与hibernate的整合开发我们基本上讲完了,现在要开始服务层的开发,也就是处理事务的action,在这里我们需要引入spring与struts的整合.也就是将action ...

  7. struts与spring整合

    Spring与Struts框架整合 Spring,负责对象对象创建 Struts, 用Action处理请求 Spring与Struts框架整合, 关键点:让struts框架action对象的创建,交给 ...

  8. struts和spring整合

    开发流程: 1)引jar包,可以在配置工程中设置用户libarary,然后直接引入.如果在web-inf/lib没有用户导入的lib文件,可以参考问题0的解决方案 需要的是struts_core,sp ...

  9. struts+hibernate+spring整合过程常见问题收集

    1.java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor缺少asm-3.3.jar2.java.lang.NoClassDefF ...

随机推荐

  1. serializeArray()获取的表单参数转化成json格式的对象

    目标:将serializeArray()这个方法获取的表单对象  转换成json格式 function serializeObject(){ //easyui提交表单 $('#form').form( ...

  2. "码率适配限速”,如何使带宽成本减少30%?

    3月28日.29日,B站.爱奇艺即将先后完成IPO.爱奇艺的招股书显示,爱奇艺依然处于亏损状态.2015 年.2016 年.2017 年三年合计亏损约 94 亿元.高昂的版权费是造成视频网站亏损的重要 ...

  3. 使用枚举enum

    枚举可以把常量按照类别组织起来, 并且提供了构造方法和其他访问方法 用法: package com.nel.testPro.useage.use_enum; public enum Color imp ...

  4. cloneNode和replaceChild

    node.cloneNode(deep) var node=document.getElementById("myList2").lastChild.cloneNode(true) ...

  5. python基础学习笔记一

    1.变量 为了节省内存,python解释器会对一些简单的字符串以及小整型数,做出一些优化,当要定义的新变量的内容与定义过的内容相同时,会让两者使用同一个内存空间. 例如: 在这个例子里面,'old'是 ...

  6. 使用Docker快速搭建Nginx+PHP-FPM环境

    下载nginx官方镜像和php-fpm镜像 docker pull nginx docker pull bitnami/php-fpm 使用php-fpm镜像开启php-fpm应用容器 docker ...

  7. 在idea的maven相关配置

    1.下载maven   下载地址:点击 2.设置maven 打开maven目录下settings.xml 设置阿里中心仓库 <mirror>    <id>alimaven&l ...

  8. wipefs进程

    wipefs进程是啥,占用了百分之90多的cpu wipefs进程是啥,占用了百分之90多的cpu,把这个进程干掉了,过了一天又自动启动了,很多朋友应该遇到过类似的问题. wipefs是linux自带 ...

  9. echarts词云引用

    最近项目中需要使用echarts的词云图,因为几经波折才引用成功,所以想记下来跟大家分享,(我的随笔不会写那么多让人需要动脑子去理解的东西,就是记录一下步骤,因为经验甚少,底层原理懂得不多,所以就先记 ...

  10. 关于JQuery的绑定方法

    从jQuery1.7开始,jQuery引入了全新的事件绑定机制,on()和off()两个函数统一处理事件绑定.因为在此之前有bind(), live(), delegate()等方法来处理事件绑定,j ...