Spring——MVC框架整合

Spring——MVC框架整合

第一章 MVC框架整合思想

1.1 搭建WEB运行环境

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.9.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.2.9.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.2.9.RELEASE</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.21</version>
</dependency>
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.5.5</version>
</dependency>
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>2.0.5</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.2.9.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.5</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.9.6</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.7</version>
</dependency>

1.2 为什么要整合MVC框架

1. MVC框架提供了控制器(Controller)调用Service
2. 请求响应的处理
3. 接受请求参数
4. 控制程序的运行流程
5. 视图解析(JSP JSON Freemarker Thymeleaf)

1.3 Spring可以整合那些框架

1. struts1
2. webwork
3. jsf
4. struts2
5. springMVC

1.4 Spring整合MVC框架的核心思路

1.4.1 准备工厂
1. WEB开发过程中,如何创建工厂?
	- ApplicationContext ctx = new WebXmlApplicationContext("applicationContext.xml");
	
2. 如何保证工厂唯一同时被共用?
	- 被共用:在web开发中,ServletContext(application)为最大的作用域
	- Spring工厂存储在ServletContext这个作用域中,ServletContext。setAttribute("xxxx",ctx);
	- 因为ServletContext唯一,所以在ServletContext创建的同时创建Spring工厂对象,就能保证Spring工厂	  对象唯一同时被共用。
	- ServletContextListener 在ServletContext对象创建时被调用(只调用一次),把工厂创建的代码,写在		 ServletContextListener 中,也会保证只调用一次。
	
3. 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 代码整合
控制器
public class XXXController{
	1.接收用户请求参数
	2.调用Service对象
	3.流程跳转(或响应JSON)
}

调用Service对象时,依赖注入:把Service对象注入给控制器对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值