《Spring MVC》 第二章 让程序run起来

news2024/9/23 11:17:39

前言

Spring MVC 是 Spring 框架提供的一款基于 MVC 模式的轻量级 Web 开发框架。
Spring MVC 本质是对 Servlet 的进一步封装,其最核心的组件是DispatcherServlet,它是 Spring MVC 的前端控制器,主要负责对请求和响应的统一地处理和分发。Controller 接收到的请求其实就是DispatcherServlet 根据一定的规则分发给它的。

1、Spring MVC 的常用组件

组件提供者描述
DispatcherServlet框架提供前端控制器,它是整个 Spring MVC 流程控制中心,负责统一处理请求和响应,调用其他组件对用户请求进行处理。
HandlerMapping框架提供处理器映射器,根据请求的 url、method 等信息查找相应的 Handler。
Handler开发人员提供处理器,通常被称为 Controller(控制器)。它可以在 DispatcherServlet 的控制下,对具体的用户请求进行处理。
HandlerAdapter框架提供处理器适配器,负责调用具体的控制器方法,对用户发来的请求来进行处理。
ViewResolver框架提供视图解析器,其职责是对视图进行解析,得到相应的视图对象。常见的视图解析器有 ThymeleafViewResolver、InternalResourceViewResolver 等。
View开发人员提供视图,它作用是将模型(Model)数据通过页面展示给用户。

1.1、Spring MVC 具有以下特点

  • Spring MVC 是 Spring 家族原生产品,可以与 IoC 容器等 Spring 基础设施无缝对接;
  • Spring MVC 支持各种视图技术,例如 JSP、Thymeleaf、 JSP 和 FreeMaker 等。
  • Spring MVC 基于原生的 Servlet 实现,通过功能强大的前端控制器 DispatcherServlet,对请求和响应进行统一处理;
  • Spring MVC 对表示层各细分领域需要解决的问题全方位覆盖,并提供一整套全面的解决方案;
  • 代码清新简洁,大幅度提升开发效率;
  • 内部组件化程度高,可插拔式组件即插即用,想要使用什么功能,配置相应组件即可;
  • 性能卓著,尤其适合现代大型、超大型互联网项目的开发。

2、第一个程序

2.1、使用Idea创建一个spring工程

在这里插入图片描述
在这里插入图片描述

2.2、导入 spring jar包

spring-core 、spring-context、spring-beans、spring-aop、spring-expression、spring-web、spring-webmvc、spring-aspects、commons-logging、javax.servlet-api

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>


2.3、导入 thymeleaf包

Thymeleaf 是一款用于渲染html的模板引擎
attoparser、slf4j-api、thymeleaf、thymeleaf-spring5、unbescape

<!-- https://mvnrepository.com/artifact/org.attoparser/attoparser -->
<dependency>
    <groupId>org.attoparser</groupId>
    <artifactId>attoparser</artifactId>
    <version>2.0.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.14.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.14.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.unbescape/unbescape -->
<dependency>
    <groupId>org.unbescape</groupId>
    <artifactId>unbescape</artifactId>
    <version>1.1.6.RELEASE</version>
</dependency>

2.4、web.xml配置总控(前端控制器)

<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
         version="3.1">
    <!--配置总控-->
    <servlet>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置 DispatcherServlet 的一个初始化参数:spring mvc 配置文件按的位置和名称-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
    </servlet>
    <!--配置映射路径-->
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2.4、src/main/resources下面新增springMVC.xml配置

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<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"
       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="com.hqyj.springmvc"/>
    <!--thymeleaf视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/static/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

2.5、构建java源代码、Test代码库

2.5.1、在main目录下创建java目录、test目录

在上述目录下创建package:com.hqyj.springmvc
在这里插入图片描述

2.5.2、打开项目结构配置

在这里插入图片描述

2.5.3、打开Modules配置

  • 设置java目录为Sources
  • 设置test目录为Tests
    在这里插入图片描述

2.5.4、检查Resources 、Language level

在这里插入图片描述

2.6、定义controller层

新增HelloController

package com.xxxx.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping("/")
    public String sayHelloWorld(){
        return "index";
    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }

}

2.7、定义视图层

在这里插入图片描述

2.7.1、首页index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>首页</title>
</head>
<body>
<h1 th:text="欢迎来到首页"></h1>
登录:<a th:href="@{/login}">登录</a>
<br>
注册:<a th:href="@{/register}">注册</a>
</body>
</html>

2.7.2、登录页login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1 th:text="欢迎来到登录页面"></h1>
首页:<a th:href="@{/}">首页</a>
<br>
注册:<a th:href="@{/register}">注册</a>
</body>
</html>

2.7.3、注册页register.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1 th:text="欢迎来到注册页面"></h1>
登录:<a th:href="@{/login}">登录</a>
<br>
首页:<a th:href="@{/}">首页</a>
</body>
</html>

2.7.4、通过Idea部署项目

2.7.4.1、打包项目

在这里插入图片描述

2.7.4.2、配置Tomcat Server(Local)

需要下载Tomcat,此处是8.5版本
在这里插入图片描述

2.7.4.3、加载项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.7.4.4、启动项目

在这里插入图片描述

2.7.4.5、在浏览器访问服务

在这里插入图片描述

3、Spring MVC工作流程

在这里插入图片描述
SpringMVC 的执行流程如下。

  • 用户通过浏览器发起一个 HTTP 请求,该请求会被 DispatcherServlet(前端控制器)拦截;
  • DispatcherServlet 调用 HandlerMapping(处理器映射器)找到具体的处理器(Handler)及拦截器,最后以 HandlerExecutionChain 执行链的形式返回给 DispatcherServlet。
  • DispatcherServlet 将执行链返回的 Handler 信息发送给 HandlerAdapter(处理器适配器);
  • HandlerAdapter 根据 Handler 信息找到并执行相应的 Handler(即 Controller 控制器)对请求进行处理;
  • Handler 执行完毕后会返回给 HandlerAdapter 一个 ModelAndView 对象(Spring MVC 的底层对象,包括 Model 数据模型和 View 视图信息);
  • HandlerAdapter 接收到 ModelAndView 对象后,将其返回给DispatcherServlet ;
  • DispatcherServlet 接收到 ModelAndView 对象后,会请求 ViewResolver(视图解析器)对视图进行解析;
  • ViewResolver 解析完成后,会将 View 视图并返回给DispatcherServlet;
  • DispatcherServlet 接收到具体的 View 视图后,进行视图渲染,将 Model 中的模型数据填充到 View 视图中的 request 域,生成最终的 View(视图);
  • 视图负责将结果显示到浏览器(客户端)。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/452782.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

用好Python自然语言工具包-- 实例“基于本地知识库的自动问答”

首先鸣谢thomas-yanxin 本问中示例来自他在GitHub上的开源项目“基于本地知识库的自动问答”&#xff0c;链接如下&#xff1a; thomas-yanxin/LangChain-ChatGLM-Webui: 基于LangChain和ChatGLM-6B的针对本地知识库的自动问答 (github.com) 目录 1. 基础知识&#xff1a; …

JVM调优最佳参数

项目背景 C端的项目&#xff0c;用户量比较多&#xff0c;请求比较多。 启动参数表 Xmx指定应用程序可用的最大堆大小。 Xms指定应用程序可用的最小堆大小。 &#xff08;一般情况下&#xff0c;需要设置Xmx和Xms为相等的值&#xff0c;且为一个固定的值&#xff09; 如果该值…

HCIP之链路聚合、VRRP

链路聚合 链路聚合 --- 可以将多个物理接口绑定成一个逻辑接口&#xff0c;即将N条物理链路聚合为一条逻辑链路。可以在不升级硬件的条件下&#xff0c;达到增加带宽的效果 我们将逻辑链路&#xff0c;称为聚合链路&#xff0c;在华为设备中称为ETH-TRUNK链路&#xff08;这个技…

Vue表单进阶操作

多选框另类使用场景 这个复选框和上面爱好那个复选框是不一样的&#xff0c;它不需要收集value值&#xff0c;只需要知道是否被选择&#xff0c;也就是ture或false&#xff0c;这时候就可以安装输入框的方式去写&#xff0c;直接去定义字符串&#xff0c;而不是数组 然后把全部…

“esp8266mod模块连接机智云Arduino实现pwm调节led的亮度“+_+

经过几天的漫长的探索和调试&#xff0c;终于连上机智云了。 历经的困难&#xff1a;esp8266总是连接机智云app超时&#xff0c;连接无反应&#xff0c;无数据。 1、机智云开发者中心&#xff0c;新建数据点&#xff0c;生成muc代码包&#xff0c;具体配置可以参考其他文章。…

go破冰之旅·5·常量、变量、数据类型

成体系的、快速学通Go&#xff0c;就在此时&#xff0c;持续连载&#xff01; 上一篇&#xff1a; https://lan6193.blog.csdn.net/article/details/123454411https://lan6193.blog.csdn.net/article/details/123454411上文熟悉了Go的基础符号、基础规则&#xff0c;本文我们…

前端项目代码规范

一、变量与函数的命名&#xff08;变量名和函数名是最好的注释&#xff09; 通常情况下函数小陀峰、类名大陀峰、变量短横线/小陀峰、const全大写单词要表达出正确的语义&#xff0c;如&#xff1a;array类型或其它集合类型用英语复数格式、其它类型不要用复数格式区分函数为功…

async/await 在 C# 语言中是如何工作的?(下)

接《async/await 在 C# 语言中是如何工作的&#xff1f;&#xff08;上&#xff09;》、《async/await 在 C# 语言中是如何工作的&#xff1f;&#xff08;中&#xff09;》&#xff0c;今天我们继续介绍 SynchronizationContext 和 ConfigureAwait。 ▌SynchronizationContext…

【SVN已解决】修改svn服务端地址为ip或者域名地址的方法

介绍 这里是小编成长之路的历程&#xff0c;也是小编的学习之路。希望和各位大佬们一起成长&#xff01; 以下为小编最喜欢的两句话&#xff1a; 要有最朴素的生活和最遥远的梦想&#xff0c;即使明天天寒地冻&#xff0c;山高水远&#xff0c;路远马亡。 一个人为什么要努力&a…

Vue之指令详解与自定义指令

指令 想要了解自定义指令&#xff0c;那肯定得先明白什么是指令。 指令的本质&#xff1a;语法糖&#xff0c;标志位。在编译阶段 render 函数里&#xff0c;会把指令编译成 JavaScript 代码。 常见的Vue内置指令有&#xff1a; v-on 即 。v-on:click”function“&#xff…

Node【Express框架【二】】

文章目录 &#x1f31f;前言&#x1f31f;中间件&#x1f31f;中间件函数&#x1f31f;什么是中间件函数&#x1f31f;中间件函数可以做什么 &#x1f31f;Express中间件的类型&#x1f31f;应用级中间件&#x1f31f;路由器级中间件&#x1f31f;错误处理中间件&#x1f31f;内…

人为惨案之kube-controller-manager 不断重启根因溯源

文章目录 背景问题发现排查CSI provision排查kube-controller-manager查看controller log紧急恢复求助chatgpt 背景 2023年4月21日10:38:07&#xff0c;在集群中测试RBAC的时候&#xff0c;在kuboard的界面神出鬼没的删除了几个clusterRole。练习一个CKA的练习题目. Create a…

如何实现计算机上的文件共享

文件共享 第一步&#xff1a;设置无线热点第二步&#xff1a;设置本地用户权限第三步&#xff1a;设置共享文件夹第四步&#xff1a;打开自己的移动热点&#xff0c;并且让对方连接自己的热点第五步&#xff1a;让对方的电脑进行连接自己共享的文件 第一步&#xff1a;设置无线…

React-Redux详解

React-Redux详解 前言 React-Redux是一个用于在React应用中管理状态的第三方库。它是基于Redux架构的&#xff0c;提供了一种在React应用中高效管理状态的方式。React-Redux通过将Redux的核心概念和React组件相结合&#xff0c;使得在React应用中使用Redux变得更加简单和方便。…

从WebGL到Babylonjs

从WebGL到Babylonjs 一、关于WebGL 前世今生 OpenGL > OpenGL ES > WebGL本质&#xff1a;通过js代码去调用OpenGL的一系列Api 二、WebGL程序的构成 1、一个简单的webgl程序 const canvas document.querySelector(canvas); const gl canvas.getContext(webgl2); c…

llama.cpp一种在本地CPU上部署的量化模型(超低配推理llama)

0x00 背景 前不久&#xff0c;Meta前脚发布完开源大语言模型LLaMA&#xff0c; 随后就被网友“泄漏”&#xff0c;直接放了一个磁力链接下载链接。 然而那些手头没有顶级显卡的朋友们&#xff0c;就只能看看而已了 但是 Georgi Gerganov 开源了一个项目llama.cpp ggergano…

HBase高可用

一、HBase高可用简介 HBase集群如果只有一个master&#xff0c;一旦master出现故障&#xff0c;将导致整个集群无法使用&#xff0c;所以在实际的生产环境中&#xff0c;需要搭建HBase的高可用&#xff0c;也就是让HMaster高可用&#xff0c;也就是需要再选择一个或多个节点也…

【C语言】那些 “虾仁猪心“ 的坑题

本章介绍 最近翻笔记&#xff0c;整理了下那些日子里面掉过的坑题&#xff0c;说多都是泪&#xff01;&#xff01;也许是自己的储备知识不足&#xff0c;才造成的大坑&#xff0c;今天把题拿出来给大家溜溜&#xff0c;看大家做时候有没有踩坑&#xff01; 文章目录 1:第一题2…

项目笔记-瑞吉外卖

文章目录 1.业务开发day011.软件开发整体介绍2.项目整体介绍:star:3.开发环境搭建4.登录功能&#xff1a;star4.1代码实现 5.退出功能6.页面效果出现 1.业务开发 day01 1.软件开发整体介绍 2.项目整体介绍⭐️ 后端&#xff1a;管理菜品和员工信息前台&#xff1a;通过手机端…

根据cadence设计图学习硬件知识 day03 了解 一些芯片 和 数据手册下载的地方

1. MT53D512M32D2DS 芯片&#xff08;动态随机存取存储器&#xff09;的技术指标 1.1 16n Prefetch (预加载) (n --芯片位宽) DDR 体系 链接&#xff1a;DDR扫盲—-关于Prefetch(预取)与Burst(突发)的深入讨论_ddr prefetch_qq_25814297-npl的博客-CSDN博客 1.2 每个通…