• 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

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:p="http://www.springframework.org/schema/p"
    6. xmlns:aop="http://www.springframework.org/schema/aop"
    7. xmlns:mvc="http://www.springframework.org/schema/mvc"
    8. xsi:schemaLocation="http://www.springframework.org/schema/beans
    9. https://www.springframework.org/schema/beans/spring-beans.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context.xsd
    12. http://www.springframework.org/schema/aop
    13. https://www.springframework.org/schema/aop/spring-aop.xsd
    14. http://www.springframework.org/schema/mvc
    15. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    16. <context:component-scan base-package="com.sofwin">context:component-scan>

    4.web.xml

    1. <servlet>
    2. <servlet-name>aservlet-name>
    3. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    4. <init-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:applicationContext.xmlparam-value>
    7. init-param>
    8. servlet>
    9. <servlet-mapping>
    10. <servlet-name>aservlet-name>
    11. <url-pattern>/url-pattern>
    12. servlet-mapping>

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

    1. // ioc的衍生注解 webmvc,控制层的类
    2. @Controller
    3. public class testController {
    4. @RequestMapping("/a")
    5. public String test01() {
    6. return "/test01.jsp";
    7. }
    8. }

    三、同步请求方法

    3.1 使用到的注解

    • @RequestMapping

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

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

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

    3.2 同步请求的定义

  • 相关阅读:
    简·奥斯汀社会作文比赛冲藤必备
    第十三届蓝桥杯国赛
    docker 容器数据在盘与盘之间迁移
    docker安装mysql同步数据到linux与docker容器卷
    Java - Object#finalize在JDK9中被标记废弃了!
    现在入行程序员,等于49年入国军
    阿里P8大牛发布《亿级流量并发手册》GitHub下载榜飙升至第一
    学会使用这五大电阻!
    MongoDB中的分布式集群架构
    A Self-Attentive model for Knowledge Tracing论文笔记和代码解析
  • 原文地址:https://blog.csdn.net/xy58451921/article/details/127852043