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

📅 发布时间:2026/7/10 20:17:00 👁️ 浏览次数:
springboot 整合 springMvc(包含springmvc的拦截器的使用)
文章目录项目目录pom.xmlMain 程序入口配置文件application.yml自定义外部资源文件夹的路径resources / static / login.htmlresources / webapp/ register.html拦截器springMvc配置文件 WebMvcConfig .javacontroller项目目录pom.xml?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!-- 所有springboot项目都必须继承自 spring-boot-starter-parent --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.0.5/version/parentgroupIdcom.atguigu/groupIdartifactIdboot-mvc/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- 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开发的场景启动器--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.28/version/dependency/dependencies/projectMain 程序入口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!DOCTYPEhtmlhtmllangenheadmetacharsetUTF-8title登录界面/title/headbody登录界面/body/htmlresources / webapp/ register.html!DOCTYPEhtmlhtmllangenheadmetacharsetUTF-8title注册Title/title/headbody注册界面/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 requestresponse responsehandler handler);returntrue;}}springMvc配置文件 WebMvcConfig .javaspringboot 项目中你 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());}}controllerpackagecom.atguigu.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;RestControllerRequestMapping(hello)publicclassHelloController{GetMapping(show)publicStringshow(){returnhello - show;}}