Java充电社
专辑
博文
联系我
本人继续续收门徒,亲手指导
SpringMVC教程
-> Helloword
1、Helloword
2、@Controller、@RequestMapping
3、接口测试利器
4、如何接受请求中的参数?
5、@RequestBody接收Json格式数据
6、多文件上传
7、返回页面常见的5种方式
8、返回json格式数据 & 通用返回值设计
9、SpringMVC返回null是什么意思?
10、异步处理请求
11、如何集成静态资源?
12、拦截器怎么用?
13、统一异常处理
14、实战篇:通用返回值 & 异常处理设计
15、全注解的方式 & 原理解析
16、源码解析SpringMVC处理请求的流程
17、源码解析SpringMVC容器的启动过程
18、RequestBodyAdvice:对@ReuqestBody进行增强
19、ResponseBodyAdvice:对@ResponseBody进行增强
20、RESTful接口详解
21、接口调用利器RestTemplate
22、参数解析器HandlerMethodArgumentResolver解密
23、@RequestParam用法及原理详解
24、@RequestBody原理解密
25、@RequestHeader详解
26、@CookieValue详解
27、@RequestAttribute详解
28、@SessionAttribute详解
29、重定向和转向详解
30、Converter转换器详解
31、跨域问题详解
32、类容协商,颠覆你的认知
33、终章
34、CORS通信
35、浏览器安全策略 & CORS
36、Http中的Content-Type详解
下一篇:@Controller、@RequestMapping
<div style="display:none"></div> 大家好,我是路人,前段时间把spring系列写完之后,就直接写springboot系列了,但是发现了一个问题,有不少粉丝问我springmvc系列哪里看?这些粉丝中可能有些朋友根本没有接触过springmvc,然后直接被我带入了springboot,会突然感觉很懵逼。 目前大多数公司都会使用Springboot来开发微服务,为其他端提供接口,而这些功能都是依靠springmvc实现的,所以为了大家看我的文章能够顺畅一些,还是决定先把springmvc系列补上,不管大家是否学过springmvc,都希望你们和我一起再过一遍springmvc,因为每个人对springmvc的理解也是不一样的,我会采用大量案例来进行讲解,也会介绍原理及源码,希望对你有所帮助。 ## 1、为什么需要学springmvc? 工作需要 > 目前用到java的企业,99.99%都会用到springmvc,即使没有直接使用springmvc,也会使用springboot,而springboot中基本上都会用到springmvc 面试需要 > 企业都在使用springmvc,所以面试的时候基本都会问到相关的问题。 更好的学习springboot、springcloud > 如果想学好 springboot 和 springcloud,那么必须先掌握spring、springmvc这2个技术。 ## 2、预备知识 学习springmvc之前,需要先掌握2个系列的课程:maven和spring,还未学的朋友,先去补补。 - [路人甲Java - maven系列](https://mp.weixin.qq.com/s/RfgETkcpLM_aSLUihGsJow) - [路人甲Java - spring高手系列](https://mp.weixin.qq.com/s/E7wNLtU-453b9YC3XoUvqQ) ## 3、软件版本 - idea 2020.3.3 - jdk1.8 - ≥maven3.6.1 - spring5.3.6 - apache-tomcat-9.0.46 ## 4、本文目标 通过springmvc实现一个helloword,将项目发布到tomcat中,然后在首页中发起一个请求,请求交给springmvc处理,由springmvc向客户端输出一个helloword,通过这个案例让大家熟悉下springmvc的使用步骤。 ## 5、Helloword案例 ### 开发步骤 ```html 1、创建一个maven项目 2、在项目中创建一个maven web模块 3、maven中添加springmvc相关依赖 4、web.xml中配置springmvc 5、添加springmvc配置文件 6、写一个HelloWordController 7、将项目部署到tomcat中 8、浏览器中验证效果 ``` 下面跟着我一步步来操作。 ### step1:创建一个maven项目 使用idea创建父子项目,打开idea->File->New->Project   按照下图输入对应的信息  点击Finish,项目创建成功,如下图  按照下图中的说明,将红框的部分删除  将pom.xml文件内容替换为下面的内容 ```xml <?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.javacode2018</groupId> <artifactId>springmvc-series</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project> ``` 此时项目结构如下图  如下图,`File->Settings`中指定项目maven的版本  ### step2、项目中创建一个maven模块 选中项目->点击鼠标右键->New->Module,创建一个maven模块  如下图,通过maven的插件来创建web模块,这个地方的maven插件需要注意下,不要选错了  点击上图中的Next,进入下面页面,输入模块的信息,然后点击Next  如下图,继续点击Finish,完成模块的创建  项目结构如下图  chat01-helloword模块中需要添加几个文件夹,操作如下图:    ### step3、maven中添加springmvc相关依赖 将下面内容替换到chat01-helloword/pom.xml中,主要添加了springmvc和servlet的依赖配置信息。 ```xml <?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.javacode2018</groupId> <artifactId>chat01-helloword</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>chat01-helloword Maven Webapp</name> <url>http://www.itsoku.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- 添加springmvc依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.6</version> </dependency> <!-- 添加servlet 依赖 --> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0-M1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>chat01-helloword</finalName> </build> </project> ``` ### step4、web.xml中配置springmvc web.xml版本太低了,我们先升级下他的版本  点击`File->Project Structure->Module`,进入到下面界面  点击上图中的`-`按钮,如下图,然后确定删除   然后按照下面操作,添加新版本的web.xml文件     web.xml版本升级成功  在web.xml中添加springmvc的配置,可以直接将下面替换到web.xml中,主要就是配置了一个DispatcherServlet这个servlet,这个springmvc的核心配置 ```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_4_0.xsd" version="4.0"> <!-- 声明springmvc核心配置对象:DispatcherServlet,这是一个servlet 这个servlet的url-parttern配置的是:*.do 表示以.do结尾的请求都发送给DispatcherServlet这个servlet去处理 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- contextConfigLocation 用来指定springmvc配置文件的位置,文件名称不一定要交springmvc,大家可以随意起名 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- load-on-startup:表示web容器启动的时,当前对象创建的顺序,值越小初始化越早,大于等于0 --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> ``` ### step5、添加springmvc配置文件 `chat01-helloword->resource->鼠标右键->New->XXML Configuration File->Spring Config`  输入文件名称:springmvc.xml  文件添加成功  替换下这个文件的内容 ```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"> <!-- springmvc容器(也就是一个spring容器)会扫描指定包中的组件,将其注册到springmvc容器中 --> <context:component-scan base-package="com.javacode2018.springmvcseries.chat01"/> </beans> ``` ### step6、写一个HelloWordController > 这个类中创建了一个hello方法,方法上面添加了一个@RequestMapping 注解,是Springmvc中的一个注解,value属性用来指定一个url列表,springmvc会将这些指定的url请求转发给当前方法处理。 > > 我们希望访问/hello.do的时候,跳转到/WEB-INF/view/hello.jsp这个页面,这个页面中输出一段内容 ```java package com.javacode2018.springmvcseries.chat01; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { /** * @RequestMapping:用来表示url和方法的映射 * value属性用来指定一个url列表,springmvc会将这些指定的url请求转发给当前方法处理 * @return */ @RequestMapping("/hello.do") public ModelAndView hello() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/view/hello.jsp"); //稍后将这个信息显示在hello.jsp中,modelAndView.addObject相当于request.setAttribute(name,value) modelAndView.addObject("msg","这是第一个springboot程序!"); return modelAndView; } } ``` /WEB-INF/view/中添加hello.jsp内容如下  ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>spingmvc系列</title> </head> <body> <h1>hello SpringMVC</h1> </body> </html> ``` 修改index.jsp的内容,如下,主要添加一个超链接,当点击这个超链接的时候,请求会发给springmvc,然后springmvc会将请求转发给HelloController的hello方法,然后通过这个方法最后将hello.jsp内容输出 ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>spingmvc系列</title> </head> <body> <div style="text-align: center"> <h1>hello SpringMVC</h1> <h1>msg:${msg}</h1> </div> </body> </html> ``` ### step7、将项目部署到tomcat中 这里我们将项目部署到tomcat10中,过程如下:      如下图,启动tomcat  ### step8、验证效果 访问:http://localhost:8080/chat01/  点击页面中的连接,会跳转到http://localhost:8080/chat01/hello.do,输出  ## 6、SpringMVC处理器请的过程 ### 6.1、详细的过程 1、tomcat启动的时候,会初始化DispatcherServlet,DispatcherServlet中会创建一个springmvc容器,其实就是我们熟悉的spring容器(ApplicationContext),只不过这个容器的类型是(WebApplicationContext),此容器会加载web.xml中contextConfigLocation指定的springmvc配置文件  2、由于springmvc.xml中指定了扫描包的规则,而HelloController符合这个扫描规则,所以会被注册到springmvc容器中  3、当发起*.do请求的时候,请求会到达DispatcherServlet中央处理器,中央处理器会根据请求路径,去springmvc容器中找到能够处理这个请求的方法,具体由哪个方法来处理这个请求呢? 这里就是通过@RequestMapping来匹配的,这个注解可以将请求和方法进行映射,匹配的请求会被@RequestMapping标注的方法处理,所以在这个步骤中springmvc容器会发现HelloController这个bean的hello方法方法可以处理/hello.do请求 4、DispatcherServlet中通过反射来调用helloController这个bean的hello方法 5、DispatcherServlet接收到了hello方法的返回值 6、DispatcherServlet根据hello方法的返回值,做跳转操作,相当于 ```java request.getRequestDispatcher("/WEB-INF/view/hello.jsp").forward(request,response); ``` ### 6.2、简化过程 **客户端发送请求 ---> 到达tomcat ---> tomcat发现是请求是*.do的请求 ---> tomcat 将请求转发给中央调度器DispatcherServlet ---> 中央调度器根据url将转发给我们自定义的controller ---> DispacherServlet根据controller的返回结果做跳转操作 ---> 将结果输出到客户端** ## 7、总结 本文主要通过一个案例详细介绍了springmvc开发项目的一个过程,大家把案例敲一遍,有问题,欢迎留言交流。 ## 8、软件及代码位置 软件地址: ```html 链接:https://pan.baidu.com/s/1_Ol-UZkN_6woMBtjcFygvQ 提取码:e66j ``` 代码位于码云上 ```html https://gitee.com/javacode2018/springmvc-series ``` <a style="display:none" target="_blank" href="https://mp.weixin.qq.com/s/_S1DD2JADnXvpexxaBwLLg" style="color:red; font-size:20px; font-weight:bold">继续收门徒,亲手带,月薪 4W 以下的可以来找我</a> ## 最新资料 1. <a href="https://mp.weixin.qq.com/s?__biz=MzkzOTI3Nzc0Mg==&mid=2247484964&idx=2&sn=c81bce2f26015ee0f9632ddc6c67df03&scene=21#wechat_redirect" target="_blank">尚硅谷 Java 学科全套教程(总 207.77GB)</a> 2. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484192&idx=1&sn=505f2faaa4cc911f553850667749bcbb&scene=21#wechat_redirect" target="_blank">2021 最新版 Java 微服务学习线路图 + 视频</a> 3. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484573&idx=1&sn=7f3d83892186c16c57bc0b99f03f1ffd&scene=21#wechat_redirect" target="_blank">阿里技术大佬整理的《Spring 学习笔记.pdf》</a> 4. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484544&idx=2&sn=c1dfe907cfaa5b9ae8e66fc247ccbe84&scene=21#wechat_redirect" target="_blank">阿里大佬的《MySQL 学习笔记高清.pdf》</a> 5. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485167&idx=1&sn=48d75c8e93e748235a3547f34921dfb7&scene=21#wechat_redirect" target="_blank">2021 版 java 高并发常见面试题汇总.pdf</a> 6. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485664&idx=1&sn=435f9f515a8f881642820d7790ad20ce&scene=21#wechat_redirect" target="_blank">Idea 快捷键大全.pdf</a> 
#custom-toc-container