• SpringMVC概述及入门


    SpringMVC概述

    • SpringMVC是一种基于Java实现MVC模型的轻量级web框架
    • 优点
      • 使用简单,开发便捷(相比于servlet)
      • 灵活性强

    学习目标
    在这里插入图片描述

    SpringMVC入门案例

    step1:创建一个新的webapp模块

    在这里插入图片描述

     <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.10.RELEASE</version>
        </dependency>
    
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
              <port>80</port>
              <path>/</path>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    step2:创建一个处理器类Controller

    在这里插入图片描述

    在该controller类上添加@Controller表示将该bean交给SpringMVC管理
    @RequestMapping("/save"):处理save的请求
    @ResponseBody : 返回值是响应体
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    //2.定义Controller
    //2.1使用@Controller定义bean
    
    @Controller
    
    public class UserController {
        //2.2设置当前操作的访问路径
        @RequestMapping("/save")
        @ResponseBody
        public String save(){
            System.out.println("user save ...");
            return "{'module':'springmvc'}";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    step3:创建一个配置类

    在这里插入图片描述
    SpringMVCConfig.java

    @Configuration
    @ComponentScan("com.itheima.controller")
    public class SpringMVCConfig {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    step4:创建初始化Servlet类extends

    在这里插入图片描述
    ServletContainersInitConfig.java

    //4.定义一个servlet容器启动的配置类,在里面加载spring的配置
    public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //    加载SpringMVC容器
        @Override
    
        protected WebApplicationContext createServletApplicationContext() {
            AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
            ctx.register(SpringMVCConfig.class);
            return ctx;
        }
    //    哪些请求交由SpringMVC处理
        @Override
        protected String[] getServletMappings() {
            return new String[]{"/"};//表示所有请求都交给SpringMVC处理
        }
    //    加载spring配置容器的对象
        @Override
        protected WebApplicationContext createRootApplicationContext() {
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    step5:Run

    1.删除web.xml文件
    在这里插入图片描述
    2.配置Tomcat插件

    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
              <port>80</port>
              <path>/</path>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    3.启动程序
    在这里插入图片描述

    入门案例工作流程分析

    在这里插入图片描述

  • 相关阅读:
    图鸟使用阿
    JAVA中继承的实现
    蚂蚁金服二面被血虐,spring- 并发 -JVM 把我直接问懵, 我经历了什么
    linux下部署nacos(单机、集群)
    Kafka与MySQL的组合使用
    Elasticsearch系列-基础知识
    算法 - 拆炸弹(JavaScript)
    基于SpringBoot大学生心理健康咨询管理系统的分析与设计
    库存三层模型(中)
    初学Nodejs(2):Buffer缓冲区
  • 原文地址:https://blog.csdn.net/weixin_42888638/article/details/125511353