MVC框架 整合
- 2.问什么要整合MVC框架3.Spring可以整合哪些MVC框架4.Spring整合MVC框架的核心思路
一、MVC框架整合思想
1.搭建web运行环境
(1)新建模块
(2)选择新建模块
(3)输入位置和名称后点击创建
(4)右键创建好的模块,选择框架支持
(5)选择web应用程序,添加
(6)配置环境
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="#34; | |
xmlns:xsi="#34; | |
xsi:schemaLocation=" #34;> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>SpringWeb</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<properties> | |
< maven .compiler.source>8</maven.compiler.source> | |
<maven.compiler.target>8</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>4.0.1</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax. Servlet </groupId> | |
<artifactId>jstl</artifactId> | |
<version>1.2</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet.jsp</groupId> | |
<artifactId>javax.servlet.jsp-api</artifactId> | |
<version>2.3.3</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-beans</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webmvc</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<!--为了去掉默认的日志框架--> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-log4j12</artifactId> | |
<version>1.7.36</version> | |
</dependency> | |
<!--引入 log4j 日志框架--> | |
<dependency> | |
<groupId>log4j</groupId> | |
<artifactId>log4j</artifactId> | |
<version>1.2.17</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>8.0.28</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-aop</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.aspectj</groupId> | |
<artifactId>aspectjrt</artifactId> | |
<version>1.9.9.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.aspectj</groupId> | |
<artifactId>aspectjweaver</artifactId> | |
<version>1.8.3</version> | |
</dependency> | |
<!-- MyBatis 环境搭建--> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>8.0.28</version> | |
</dependency> | |
<dependency> | |
<groupId>org.mybatis</groupId> | |
<artifactId>mybatis</artifactId> | |
<version>3.5.9</version> | |
</dependency> | |
<!--Spring与 MyBatis整合,包括上边两个MyBatis jar包--> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring- jdbc </artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.mybatis</groupId> | |
<artifactId>mybatis-spring</artifactId> | |
<version>2.0.7</version> | |
</dependency> | |
<dependency> | |
<groupId>com.alibaba</groupId> | |
<artifactId>druid</artifactId> | |
<version>1.2.9</version> | |
</dependency> | |
<!--Spring控制事务jar包引入--> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-tx</artifactId> | |
<version>5.3.22</version> | |
</dependency> | |
</dependencies> | |
<build></build> | |
</project> | |
2.问什么要整合MVC框架
(1)MVC框架提供了 控制器 (Controller)调用 Service
(2)请求响应的处理
(3)接收请求参数
(4)控制程序的运行流程
(5)视图解析
3.Spring可以整合哪些MVC框架
(1)struts1
(2) webwork
(3)jsf
前三个比较久远了
(4) struts2
(5)springMVC(应用最为广泛)
4.Spring整合MVC框架的核心思路
1.准备工厂
(1) web开发过程中如何创建工厂
AppliactionContext ac = new ClassPathXmlApplicationContext("applicationContext.xml") | |
改为: WebXmlApplicationContext() |
(2)如何保证工厂唯一切同时被共用
(1)被共用:Web request |session|ServletContext(application) | |
工厂被存储在 ServletContext 这个作用域中 ServletContext.setAttribute("xxxx",ac); | |
(2)唯一:ServletContext对象在创建的同时 --->AppliactionContext ac = new ClassPathXmlApplicationContext("applicationContext.xml") | |
ServletContextListener(ServletContext对象在创建的同时被调用,只被调用一次) ---> AppliactionContext ac = new ClassPathXmlApplicationContext("applicationContext.xml") |
(3)总结
在ServletContextListener中创建工厂(唯一),把工厂存储在ServletContext的作用域中(共用)
(4) Spring封装了一个 ContextLoaderListener
作用:创建工厂,把工厂存在ServletContext中
(5) ContextLoaderListener的使用方式
<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.代码整合
依赖注入:把Service对象注入给控制器对象