• 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 Map类的简介说明
    Jetpack 多个Activity共享ViewModel(AndroidViewModel)
    ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
    【数据结构与算法】之深入解析“摘樱桃II”的求解思路与算法示例
    Fiddler的安装及配置2-1
    detr目标检测算法源码详解
    [人脸算法]技术方向综述
    [面试直通]操作系统核心之存储系统(上)
    gRPC四种通信模式
    Nacos 2.1.1 正式发布
  • 原文地址:https://blog.csdn.net/weixin_42888638/article/details/125511353