百知教育 — Spring系列课程 — MVC框架整合


第一章、MVC框架整合思想

1. 搭建Web运行环境
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.3</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2. 为什么要整合MVC框架
1. MVC框架提供了控制器(Controller)调用Service
DAO ---》 Service
2. 请求响应的处理
3. 接受请求参数 request.getParameter("")
4. 控制程序的运行流程
5. 视图解析 (JSP JSON Freemarker Thyemeleaf )
3. Spring可以整合那些MVC框架
1. struts1
2. webwork
3. jsf
4. struts2
5. springMVC
4.Spring整合MVC框架的核心思路
1. 准备工厂
1. Web开发过程中如何创建工厂
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
WebXmlApplicationContext()
2. 如何保证工厂唯一同时被共用
被共用:Web request|session|ServletContext(application)
工厂存储在ServletContext这个作用域中 ServletContext.setAttribute("xxxx",ctx); 唯一:ServletContext对象 创建的同时 ---》 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); ServletContextListener ---> ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContextListener 在ServletContext对象创建的同时,被调用(只会被调用一次) ,把工厂创建的代码,写在ServletContextListener中,也会保证只调用
一次,最终工厂就保证了唯一性
3. 总结
ServletContextListener(唯一)
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContext.setAttribute("xxx",ctx) (共用) 4. Spring封装了一个ContextLoaderListener
1. 创建工厂
2. 把工厂存在ServletContext中
ContextLoaderListener使用方式 

web.xml

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2. 代码整合
依赖注入:把Sevice对象注入个控制器对象。

第二章、Spring与Struts2框架整合 (选学)

1. Spring与Struts2整合思路分析
1. Struts2中的Action需要通过Spring的依赖注入获得Service对象。
2. Spring与Struts2整合的编码实现
  • 搭建开发环境

    • 引入相关jar (Spring Struts2)

      <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>2.3.8</version>
      </dependency>
    • 引入对应的配置文件

      • applicationContext.xml
      • struts.xml
      • log4j.properties
    • 初始化配置

      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener> <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
      </context-param> <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 (ContextLoaderListener —> Web.xml)
      • Struts2(Filter —> Web.xml)
  • 编码

    • 开发Service对象

      最终在Spring配置文件中创建Service对象
      <bean id="userService" class="com.baizhi.struts2.UserServiceImpl"/>
    • 开发Action对象

      • 开发类

        public class RegAction implements Action{
        private UserService userService;
        set get public String execute(){
        userService.register();
        return Action.SUCCESS;
        }
        }
      • Spring (applicationContext.xml)

        <bean id="regAction" class="com.baizhi.struts2.RegAction" scope="prototype">
        <property name="userService" ref=""/>
        </bean
      • Struts2(struts.xml)

        <package name="ssm" extends="struts-default">
        url reg.action ---> 会接受到用户的请求后,创建RegAction这个类的对象 进行相应的处理 <action name="reg" class="regAction">
        <result name="success">/index.jsp</result>
        </action>
        </package>
3. Spring+Struts2+Mybatis整合(SSM)
1. 思路分析
SSM = Spring+Struts2  Spring+Mybatis

2. 整合编码
  • 搭建开发环境

    • 引入相关jar (Spring Struts2 Mybatis)


      <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>2.3.8</version>
      </dependency> <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
      </dependency> <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
      <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
      <scope>provided</scope>
      </dependency> <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.14.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.1.14.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.14.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.1.14.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.14.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.2</version>
      </dependency> <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.18</version>
      </dependency> <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.48</version>
      </dependency> <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
      </dependency> <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
      </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.4.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.1.14.RELEASE</version>
      </dependency> <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.8</version>
      </dependency> <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.3</version>
      </dependency> <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
      </dependency> <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      </dependency>
    • 引入对应的配置文件

      • applicationContext.xml
      • struts.xml
      • log4j.properties
      • xxxxMapper.xml
    • 初始化配置

      • Spring (ContextLoaderListener —> Web.xml)

      • Struts2(Filter —> Web.xml)

      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener> <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
      </context-param> <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>
  • 编码

    • DAO (Spring+Mybatis)

      1. 配置文件的配置
      1. DataSource
      2. SqlSessionFactory ----> SqlSessionFactoryBean
      1. dataSource
      2. typeAliasesPackage
      3. mapperLocations
      3. MapperScannerConfigur ---> DAO接口实现类
      2. 编码
      1. entity
      2. table
      3. DAO接口
      4. 实现Mapper文件
    • Service (Spring添加事务)

      1. 原始对象 ---》 注入DAO
      2. 额外功能 ---》 DataSourceTransactionManager ---> dataSource
      3. 切入点 + 事务属性
      @Transactional(propagation,readOnly...)
      4. 组装切面
      <tx:annotation-driven
    • Controller (Spring+Struts2)

      1. 开发控制器 implements Action 注入Service
      2. Spring的配置文件
      1. 注入 Service
      2. scope = prototype
      3. struts.xml
      <action class="spring配置文件中action对应的id值"/>
4. Spring开发过程中多配置文件的处理
Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护。

DAO  ------  applicationContext-dao.xml
Service --- applicationContext-service.xml
Action --- applicationContext-action.xml 注意:虽然提供了多个配置文件,但是后续应用的过程中,还要进行整合
  • 通配符方式

    1. 非web环境
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext-*.xml");
    2. web环境
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
    <context-param>
  • <import标签

    applicationContext.xml 目的 整合其他配置内容
    <import resource="applicationContext-dao.xml " />
    <import resource="applicationContext-service.xml " />
    <import resource="applicationContext-action.xml " /> 1. 非web环境
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    2. web环境
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <context-param>

Spring一套全通5—SSM整合的更多相关文章

  1. Spring一套全通4—持久层整合

    百知教育 - Spring系列课程 - 持久层整合 第一章.持久层整合 1.Spring框架为什么要与持久层技术进行整合 1. JavaEE开发需要持久层进行数据库的访问操作. 2. JDBC Hib ...

  2. Spring一套全通—工厂

    百知教育 - Spring系列课程 - 工厂 第一章 引言 1. EJB存在的问题 2. 什么是Spring Spring是一个轻量级的JavaEE解决方案,整合众多优秀的设计模式 轻量级 1. 对于 ...

  3. spring MVC框架入门(外加SSM整合)

    spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...

  4. ssm整合(Spring+SpringMVC+Mybatis)

    一.Spring Spring致力于提供一种方法管理你的业务对象.IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用 ...

  5. ssm整合说明与模板-Spring Spring MVC Mybatis整合开发

    ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...

  6. SSM整合(1): spring 与 springmvc 整合

    久没有写博客了, 今年事情太多了, 也没了心思. 去深圳出差,  更重要的结婚的事情, 一茬接一茬. 好在最近闲暇一些, 就想记录一些曾经困扰过我的问题(现在用spring boot真是太方便了, 很 ...

  7. Spring框架学习笔记(4)——SSM整合以及创建Maven自定义模版

    Spring+Spring MVC+MyBatis+Maven SSM整合的核心还是Spring+MyBatis的整合,回顾一下MyBatis操作数据库流程,我们是使用一个SQLSessionFact ...

  8. SSM整合配置

    SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis) 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有 ...

  9. 【转】ssm整合

    http://m.blog.csdn.net/article/details?id=44455235 SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 发表于2015/3 ...

  10. java web开发入门八(ssm整合)基于intellig idea

    ssm整合 一.导入相关包 二.开发流程 1.写entity package com.eggtwo.euq.entity; import java.io.Serializable; import ja ...

随机推荐

  1. Python 3.12 抢先看——关于 f-string 的改动

    Python 3.12 抢先看--关于 f-string 的改动 哈喽大家好,我是咸鱼 相信小伙伴们对 python 中的 f-string 都不陌生 f-string 是格式化字符串的缩写,是以小写 ...

  2. 2018年第九届 蓝桥杯C组 C/C++决赛题解

    蓝桥杯历年国赛真题汇总:Here 1.年龄问题 s夫人一向很神秘.这会儿有人问起她的年龄,她想了想说: "20年前,我丈夫的年龄刚好是我的2倍,而现在他的年龄刚好是我的1.5倍". ...

  3. L1-046 整除光棍 (20分)

    问题描述 这里所谓的"光棍",并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整除.比如,111111就可 ...

  4. java调用本机的命令 如ping、打开文本等

    最近接触到用java代码调用主机的命令部分感觉有点意思整理总结一下 环境jdk1.8  操作系统win10,不用引入其他的包jdk自带的api就可以 一.java调用ping命令 import jav ...

  5. 一、mysql5.7 rpm 安装(单机)

    一.下载需要的rpm包mysql-community-client-5.7.26-1.el6.x86_64.rpmmysql-community-common-5.7.26-1.el6.x86_64. ...

  6. 六、mycat全局自增

    系列导航 一.Mycat实战---为什么要用mycat 二.Mycat安装 三.mycat实验数据 四.mycat垂直分库 五.mycat水平分库 六.mycat全局自增 七.mycat-ER分片 一 ...

  7. C语言哈希表uthash的使用方法详解(附下载链接)

    uthash简介   由于C语言本身不存在哈希,但是当需要使用哈希表的时候自己构建哈希会异常复杂.因此,我们可以调用开源的第三方头文件,这只是一个头文件:uthash.h.我们需要做的就是将头文件复制 ...

  8. C#查找算法1:二分查找

    二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列. 原理:将n个元素分成个数大致相同的两半,取 ...

  9. 星索称重/生产管理软件 联机版V1.0

    星索称重/生产管理软件 联机版V1.0   一.特点 1.支持多用户.多组织管理,灵活控制用户权限. 2.支持地磅秤.智能电子秤.轨道秤等多款称重设备. 3.支持三联单/热敏纸等多种打印模板. 二.系 ...

  10. C++17 解构绑定

    在python中,加入我们有一个函数返回了两个数值,如: def getData(x, y): return x,y 那么我们在使用这个函数时只需要使用两个新变量去接收函数返回值就可以: a,b = ...