Spring笔记四 - MVC框架整合
作者: Wyt
系列文章目录
Spring笔记(1) - 工厂
Spring笔记(2) - AOP编程
Spring笔记(3) - 持久层整合
Spring笔记(4) - MVC框架整合
Spring笔记(5) - 注解编程基础
文章目录
1. MVC框架整合思想
1.1 搭建Web运行环境
1.1.1 创建Module
1.
左上角File -> Project Stucture -> Modules -> 点击 + -> 勾上Create from archetype -> 在列表中选择maven-archetype-webapp -> 根据自己的需要配置module和maven的相关配置
2.
在pom.xml中将
<maven.compiler.source>
<maven.compiler.target>
两个标签体的内容改为自己的jdk版本
3.
删除pom.xml中
<build>标签体的内容
我们可以在日后的开发对其进行完善
4.
在main包下创建java和resource包,分别用来存放源代码和配置文件
右键 -> Mark Directory as 指定其为能被自动识别的对应的文件夹类型
1.1.2 引入依赖
<dependencies>
<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>
<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>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</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.9.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
1.2 为什么要整合MVC框架
1. MVC框架提供了控制器(Controller)调用Service
DAO --> Service --> Controller
2. 请求响应的处理
3. 接受请求参数
4. 控制程序的运行流程
5. 视图解析 (JSP JSON Freemaker Thymeleaf)
1.3 Spring可以整合哪些MVC框架
1. struts1
2. webwork
3. jsf
这三个比较旧, 现在基本不用
4. struts2
5. springMVC (最常用)
1.4 Spring整合MVC框架的核心思路
1.4.1 准备工厂
1. Web开发过程如何创建工厂
ApplicawtionContext 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>
1.4.2 代码整合
* 依赖注入:把Sevice对象注入个控制器对象。
控制: SpringMVC Controller
功能单一, 依赖Service
Controller类功能:
1. 接受client请求参数
2. 调用Service对象 (重要)
3. 流程跳转 (相应JSON)
* 可以在Controller类中以成员变量的形式创建Service
并为其提供 get set 成员变量方法
通过Spring进行依赖注入
在配置文件中进行赋值