news 2026/6/10 14:58:25

springboot 整合 springMvc(包含springmvc的拦截器的使用)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot 整合 springMvc(包含springmvc的拦截器的使用)

文章目录

    • 项目目录
    • pom.xml
    • Main 程序入口
    • 配置文件application.yml
      • 自定义外部资源文件夹的路径
    • resources / static / login.html
    • resources / webapp/ register.html
    • 拦截器
    • springMvc配置文件 WebMvcConfig .java
    • controller

项目目录

pom.xml

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><!-- 所有springboot项目,都必须继承自 spring-boot-starter-parent --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.5</version></parent><groupId>com.atguigu</groupId><artifactId>boot-mvc</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- SpringBoot提供了许多预定义的Starter,如: spring-boot-starter-web用于构建Web应用程序, spring-boot-starter-data-jpa用于使用JPA进行数据库访问, spring-boot-starter-security用于安全认证和授权, ...等等 使用Starter非常简单,只需要在项目的构建文件(例如Maven的pom.xml)中添加所需的Starter依赖, SpringBoot会自动处理依赖管理和配置。 springboot提供的全部启动器地址: [https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters](https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters) springboot启动器的命名规范: 官方提供的:命名为:`spring-boot-starter-*` 第三方提供:命名为:`*-spring-boot-starter` --><!--web开发的场景启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies></project>

Main 程序入口

packagecom.atguigu;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassMainApplication{publicstaticvoidmain(String[]args){SpringApplication.run(MainApplication.class,args);}}

配置文件application.yml

自定义外部资源文件夹的路径

# springMvc相关的配置server:port:80# 端口号,默认是8080servlet:context-path:/boot# 设置项目根路径spring:web:resources:# 这是默认值# static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/static-locations:classpath:/webapp# 自定义外部资源文件夹的路径# 一旦自定义外部资源文件夹路径,就覆盖掉默认路径了# 访问外部资源的时候,不要写外部资源文件夹的名字哦。。。

resources / static / login.html

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>登录界面</title></head><body>登录界面</body></html>

resources / webapp/ register.html

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>注册Title</title></head><body>注册界面</body></html>

拦截器

packagecom.atguigu.interceptor;importjakarta.servlet.http.HttpServletRequest;importjakarta.servlet.http.HttpServletResponse;importorg.springframework.web.servlet.HandlerInterceptor;publicclassMyInterceptorimplementsHandlerInterceptor{@OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{System.out.println("request = "+request+"response = "+response+"handler = "+handler);returntrue;}}

springMvc配置文件 WebMvcConfig .java

  • springboot 项目中,你 springmvc 的功能,就像之前一样用就行了

  • 加上 @Configuration注解

  • 只要在MainApplication.java类所在包及其子包下,就会被@SpringBootApplication注解扫描注册

packagecom.atguigu.config;importcom.atguigu.interceptor.MyInterceptor;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * springboot项目中,你springmvc的功能就像之前一样用就行了 * * 加上 @Configuration注解 * * 只要在 MainApplication.java类 所在包及其子包下,就会被@SpringBootApplication注解扫描注册 */@ConfigurationpublicclassWebMvcConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(newMyInterceptor());}}

controller

packagecom.atguigu.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("hello")publicclassHelloController{@GetMapping("show")publicStringshow(){return"hello - show";}}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/2 17:08:01

端云一体 一天开发的元服务-奇趣故事匣经验分享

端云一体 一天重新开发的元服务-奇趣故事匣经验分享 ​ 万少&#xff1a;华为HDE、鸿蒙极客 个人主页&#xff1a;https://blog.zbztb.cn/ 2025年参与孵化了20鸿蒙应用、技术文章300、鸿蒙知识库用户500、鸿蒙免费课程2套。 如果你也喜欢交流AI和鸿蒙技术&#xff0c;欢迎扣我。…

作者头像 李华
网站建设 2026/6/10 18:27:42

MySQL死锁问题分析与解决:从日志解读到根治方案

前言 死锁是MySQL并发场景下的“隐形杀手”&#xff0c;轻则导致个别事务失败&#xff0c;重则引发系统雪崩。本文将从死锁监控、日志解读、原因分析、解决方案四个维度&#xff0c;提供一套完整的死锁处理流程&#xff0c;助你快速定位并彻底解决死锁问题。 一、死锁现场&…

作者头像 李华
网站建设 2026/6/9 16:33:06

DeepSeek是如何思考的?一文读懂大模型的工作原理

DeepSeek&#xff08;以及所有类似的大模型&#xff09;在回答你的问题时&#xff0c;并不是像搜索引擎那样去“调取”或“查询”一个后台数据库。 它不会去翻阅它训练时看过的资料&#xff0c;然后找出一段话复制给你。它的工作方式要更加神奇和复杂。 一、DeepSeek是如何“回…

作者头像 李华
网站建设 2026/6/10 13:36:55

从自测到部署:DMXAPI如何为AI编程提供全方位的技术保障?

前言&#xff1a;AI编程的“死亡之谷”与技术护城河2026年&#xff0c;全球AI应用开发成功率出现了一个令人深思的数据&#xff1a;从概念验证到生产部署&#xff0c;仅有18%的AI项目能够成功跨越这个“死亡之谷”。那些失败的82%&#xff0c;并非因为创意不足或技术不先进&…

作者头像 李华
网站建设 2026/6/11 6:18:23

Linux 内存映射与显存操作深度解析

Linux 内存映射与显存操作深度解析 一、内存映射基础概念 1. 什么是内存映射&#xff1f; 内存映射&#xff08;Memory Mapping&#xff09;是一种将文件或设备直接映射到进程地址空间的技术&#xff0c;使得应用程序可以像访问内存一样访问文件或设备。通过 mmap() 系统调用&a…

作者头像 李华
网站建设 2026/5/28 19:59:06

柑橘叶片病害数据集(5类)

该数据集包含健康的柑橘叶和4类柑橘叶病Citrus_Canker_Diseases_Leaf_Orange&#xff1a;拥有11248张照片Citrus_Nutrient_Deficiency_Yellow_Leaf_Orange&#xff1a;拥有12800张照片Healthy_Leaf_Orange&#xff1a;6384张照片Multiple_Diseases_Leaf_Orange:4800张照片Young…

作者头像 李华