Spring Mvc的相关知识

一、初识MVC

        1.Spring Mvc 是控制层的Spring框架,替换Servlet,除了它以外,还有 struct1和 struct2

        区别:

                1.struct1被struct2 取代

                2.struct2:采用 prototype多例模式,内存消耗快,经常会出现内存溢出,优点是成员变量线程安全

                3.Spring Mvc:采用singleton单例模式,使用它节省内存空间,但是成员变量需要考虑线程安全问题----线程安全的解决,可以参考http://t.csdn.cn/JKNLp

        2.Spring MVC 原理     

1.客户端发送请求给DispatcherServlect前端控制器

2.DispatcherServlect前端控制器向handlerMapping处理器映射器发送请求

3.HandlerMapping处理器映射器向DispatcherServlect前端控制器发送处理器执行链和拦截器链

4.DispatcherServlect前端控制器向处理器映射器对应的HandlerAdapter处理器适配器发送请求

5.HandlerAdapter处理器适配器查找Handler处理器

6.Handler处理器执行handler

7.handler向HandlerAdapter处理器适配器返回ModleAndView

8.HandlerAdapter处理器适配器向DispatcherServlect前端控制器返回MadleAndView

9.DispatcherServlect前端控制器向ViewResolver视图解析器发送请求,帮助解析视图

10.ViewResolver视图解析器向DispatcherServlect前端控制器返回View

11.DispatcherServlect前端控制器会拿数据进行渲染,生成HTML静态代码

12.将静态代码发送到客户端进行响应

二、Spring Mvc框架搭建

1.创建web工程

2.导入依赖:

5个核心依赖+日志依赖+web.jar+webmvc.jar+aop.jar

3.创建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
       
       
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.sofwin"></context:component-scan>

4.web.xml

<servlet>
	<servlet-name>a</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 
	定义了前端控制器在创建时可以接收的初始化参数
	通过初始化参数的设置来确定spring容器的生成
	 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</init-param>
</servlet>

<servlet-mapping>
	<servlet-name>a</servlet-name>
   <!-- 
 	1. 完整映射  /a/user
 	2. 类型映射  *.do struts1  *.action strust2
 	3. 路径映射  /*
	不能出现以上三种的混合: /*.do x
	除了jsp以外的请求全部进入到前端控制器
	 -->
	<url-pattern>/</url-pattern>
</servlet-mapping>

5.创建同步请求:需要一个普通的class文件

// ioc的衍生注解  webmvc,控制层的类
@Controller
public class testController {
	@RequestMapping("/a")
	public String test01() {
		return "/test01.jsp";
	}
}

三、同步请求方法

3.1 使用到的注解

  • @RequestMapping

  • GetMapping:只能接收get类型的请求

  • PostMapping:只能接收Post类型的请求

  • DeleteMapping:只能接收Delete类型的请求

3.2 同步请求的定义

猜你喜欢

转载自blog.csdn.net/xy58451921/article/details/127852043