8.实现SpringMVC入门案例

SpringMVC入门

一、目的

  1. 理解MVC模式
  2. 了解SpringMVC的概念
  3. 熟悉SpringMVC的执行流程
  4. 掌握SpringMVC搭建步骤

二、要求

客户端发起请求,服务端接收请求,执行逻辑并进行视图跳转。

三、原理

  1. MVC模式

    经典MVC模式中,M是指模型,V是视图,C则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。其中,View的定义比较清晰,就是用户界面。

  2. SpringMVC

    SpringMVC是一种基于Java的实现MVC设计模型的请求驱动类型的轻量级Web框架。它通过一套注解,让一个简单的Java类成为处理请求的控制器,而无须实现任何接口。同时它还支持RESTful编程风格的请求。

  3. 执行流程

    (1)用户发送请求至前端控制器DispatcherServlet

    (2)DispatcherServlet收到请求调用HandlerMapping处理器映射器

    (3)处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。

    (4)DispatcherServlet调用HandlerAdapter处理器适配器

    (5)HandlerAdapter经过适配调用具体的Handler处理器(Controller,也叫后端控制器)。Controller执行完成返回ModelAndView。

    (6)HandlerAdapter将Controller执行结果ModelAndView返回给DispatcherServlet。

    (7)DispatcherServlet将ModelAndView传给ViewReslover视图解析器

    (8)ViewReslover解析后返回具体View视图

    (9)DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。

    (10)DispatcherServlet响应用户。

四、步骤

  1. 使用maven搭建web项目(方式有多种,这里只是其中一种)

    (1)创建普通maven项目

    (2)在项目的main文件夹下新建文件夹webapps

在这里插入图片描述

(3)选择File—>Project Structure

在这里插入图片描述

(4)选择Facets—>±–>web

在这里插入图片描述

(5)选择项目中的对应模块

在这里插入图片描述

(6)修改Deployment Descriptors下web.xml路径

在这里插入图片描述

(7)修改Web Resource Directory路径

在这里插入图片描述

  1. xml方式

    (1)导入SpringMVC相关坐标

    	<dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.8</version>
            </dependency>
        </dependencies>
    

    (2)在webapps下创建hello.jsp视图页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        你好,SpringMVC!
    </body>
    </html>
    

    (3)在com.cqgcxy.controller包下创建HelloController控制器

    package com.cqgcxy.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
        @RequestMapping("/hello")
        public String hello(){
            System.out.println("hello···");
            return "hello.jsp";
        }
    }
    

    (4)在resources文件夹下创建SpringMVC核心文件spring-mvc.xml,并添加注解扫描

    在这里插入图片描述

    (5)web.xml中配置SpringMVC核心控制器DispathcerServlet

    	<servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    

    (6)配置Tomcat并启动

    • 选择Run—>Edit Configurations

      在这里插入图片描述

    • 选择±–>Tomcat Server—>Local

      在这里插入图片描述

    • 选择本地tomcat所在的地址【tomcat的bin目录上一层路径】,点击Fix

      在这里插入图片描述

    • 选择Artifacts—>Web Application:Exploded—>From Modules

      在这里插入图片描述

    • 选择项目中的对应模块,选择OK—>继续选择OK

      在这里插入图片描述

    • 启动Tomcat

      在这里插入图片描述

    (7)客户端向路径 http://localhost:8080/mvc_demo_war_exploded/hello发起请求测试

  2. 注解方式

    (1)添加servlet依赖,并新建com.cqgcxy.config包

    		<dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
            </dependency>
    

    (2)在config包下创建配置类SpringMvcConfig替换spring-mvc.xml

    package com.cqgcxy.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.cqgcxy.controller")
    public class SpringMvcConfig {
    }
    

    (3)在config包下创建配置类ServletContainersInitConfig替换web.xml

    package com.cqgcxy.config;
    
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
    
    public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
        @Override
        protected WebApplicationContext createServletApplicationContext() {
            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
            ctx.register(SpringMvcConfig.class);
            return ctx;
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[]{"/"};
        }
    
        protected WebApplicationContext createRootApplicationContext() {
            return null;
        }
    }
    

    (4)重新启动,并测试。

五、结果

  1. 客户端向路径 http://localhost:8080/mvc_demo_war_exploded/hello发起请求测试效果截图如下:

    在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值