• 一文带你入门SpringMVC


    1.概述

    在这里插入图片描述

    Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架

    Spring MVC的特点:

    • 轻量级,简单易学
    • 高效 , 基于请求响应的MVC框架
    • 与Spring兼容性好,无缝结合
    • 约定优于配置
    • 功能强大:RESTful、数据验证、格式化、本地化、主题等
    • 简洁灵活

    Spring的web框架围绕DispatcherServlet [ 调度Servlet ] 设计。

    DispatcherServlet的作用是将请求分发到不同的处理器。


    2.中心控制器

    Spring MVC框架像许多其他MVC框架一样, 以请求为驱动 , 围绕一个中心Servlet分派请求及提供其他功能,DispatcherServlet是一个实际的Servlet (它继承自HttpServlet 基类)

    在这里插入图片描述

    SpringMVC的原理如下图所示:

    当发起请求时被前置的控制器拦截到请求,根据请求参数生成代理请求,找到请求对应的实际控制器,控制器处理请求,创建数据模型,访问数据库,将模型响应给中心控制器,控制器使用模型与视图渲染视图结果,将结果返回给中心控制器,再将结果返回给请求者。

    在这里插入图片描述

    DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求,DispatcherServlet接收请求并拦截请求。

    我们假设请求的url为 : http://localhost:8080/SpringMVC/hello

    SpringMVC部署在服务器上的web站点,hello表示控制器

    通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。

    在这里插入图片描述

    1. HandlerMapping为处理器映射。DispatcherServlet调用HandlerMapping,HandlerMapping根据请求url查找Handler。
    2. HandlerExecution表示具体的Handler,其主要作用是根据url查找控制器,如上url被查找控制器为:hello。
    3. HandlerExecution将解析后的信息传递给DispatcherServlet,如解析控制器映射等。
    4. HandlerAdapter表示处理器适配器,其按照特定的规则去执行Handler。
    5. Handler让具体的Controller执行。
    6. Controller将具体的执行信息返回给HandlerAdapter,如ModelAndView。
    7. HandlerAdapter将视图逻辑名或模型传递给DispatcherServlet。
    8. DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名。
    9. 视图解析器将解析的逻辑视图名传给DispatcherServlet。
    10. DispatcherServlet根据视图解析器解析的视图结果,调用具体的视图。
    11. 最终视图呈现给用户。

    3.搭建SpringMVC

    新建一个Moudle,添加web支持

    更新pom依赖

    pom.xml

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>SpringMVCartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <maven.compiler.source>17maven.compiler.source>
            <maven.compiler.target>17maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.23version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.31version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.11version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>5.3.23version>
            dependency>
            
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.9.9.1version>
                <scope>runtimescope>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>2.0.7version>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.24version>
                <scope>providedscope>
            dependency>
        dependencies>
    
        <build>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
        build>
    
    project>
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    配置web.xml

    注册DispatcherServlet

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <servlet>
            <servlet-name>SpringMVCservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc-servlet.xmlparam-value>
            init-param>
            
            <load-on-startup>1load-on-startup>
        servlet>
        
        <servlet-mapping>
            <servlet-name>SpringMVCservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    配置springmvc-servlet.xml

    在resource目录下添加springmvc-servlet.xml配置文件,配置的形式与Spring容器配置基本类似

    
    <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:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <context:component-scan base-package="top.imustctf.controller"/>
        
        <mvc:default-servlet-handler />
        
        <mvc:annotation-driven />
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              id="internalResourceViewResolver">
            
            <property name="prefix" value="/WEB-INF/jsp/" />
            
            <property name="suffix" value=".jsp" />
        bean>
    beans>
    
    • 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

    创建Controller

    @Controller
    @RequestMapping("/HelloController")
    public class HelloController {
        // 真实访问地址 : 项目名/HelloController/hello
        @RequestMapping("/hello")
        public String sayHello(Model model){
            // 向模型中添加属性msg与值,可以在JSP页面中取出并渲染
            model.addAttribute("msg","hello,SpringMVC");
            // web-inf/jsp/hello.jsp
            // 因为在SpringMVC配置文件中添加了前缀和后缀
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建视图层

    WEB-INF/jsp目录中创建hello.jsp , 视图可以直接取出并展示从Controller带回的信息;

    可以通过EL表示取出Model中存放的值,或者对象;

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>SpringMVCtitle>
    head>
    <body>
        ${msg}
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    手动导包

    进入项目包管理页面,WEB-INF目录下新建一个lib目录,将右侧的包导入:

    在这里插入图片描述

    启动Tomcat,测试

    访问路径:

    http://localhost:8080/HelloController/hello
    在这里插入图片描述

    环境配置成功了

  • 相关阅读:
    java多线程基础——阻塞式队列
    初识网页与浏览器
    Golang 实现 websocket 通讯(一个服务器,两个客户端)
    介绍一下js垃圾回收机制
    Leetcode刷题【hot100】最长连续序列
    智能指针笔记
    阅读JavaScript文档-对象
    (附源码)php企业招聘网站 毕业设计 222219
    如何阅读 Paper
    RocketMq源码分析(一)--基本知识
  • 原文地址:https://blog.csdn.net/Gherbirthday0916/article/details/127789858