springBoot--静态资源映射

静态资源映射

  • 前言
  • [1、通过继承 WebMvcConfigurerAdapter 来实现](#1、通过继承 WebMvcConfigurerAdapter 来实现)
  • [2、在 application.properties 配置](#2、在 application.properties 配置)

前言

在 web 开发中,静态资源的访问是必不可少的,如图片、js、css等资源的访问

1、通过继承 WebMvcConfigurerAdapter 来实现

即如果使用了 @EnableWebMvc ,则自动配置类 WebMvcAutoConfiguration 会失效,因此默认映射路径 /static , /public , META-INF/resources , /resources 都将失效

java 复制代码
package com.example.ruiji_demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author jitwxs
 * @date 2024年03月10日 20:52
 */
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
//    设置静态资映射
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry){
        log.info("开始进行静态资源映射");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates").addResourceLocations("classPath:/templates/");
    }
}

2、在 application.properties 配置

java 复制代码
spring.mvc.static-path-pattern=/static/**
相关推荐
程序媛刘刘6 分钟前
uniappx 使用体验
java·服务器·前端
寻月隐君9 分钟前
Python 数据结构与算法:课程笔记与实战解析
后端·python·github
gywl19 分钟前
Spring Boot 日志
java·spring boot·日志·外观模式
云雨雪22 分钟前
朋友,你也不想不懂RPC的事情被同事发现吧?(附DEMO,快来玩!)
后端·微服务·rpc
热心小张23 分钟前
Springboot单元测试
spring boot·单元测试·log4j
梦兮林夕28 分钟前
深入浅出 Gin 路由管理:从基础到最佳实践
后端·go·gin
Seven9735 分钟前
【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
java·后端·设计模式
matrixlzp39 分钟前
Java 生成图形验证码
java·spring
冰淇淋@39 分钟前
HTTP发送POST请求的两种方式
java·spring boot·http
梦醒沉醉1 小时前
Scala的初步使用
开发语言·后端·scala