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/**
相关推荐
IT_陈寒13 分钟前
Vite热更新失效?你可能漏了这个配置项
前端·人工智能·后端
Wx-bishekaifayuan14 分钟前
springboot生活商城系统21035-计算机课程设计、毕业设计
spring boot·后端·python·spring·elasticsearch·golang·课程设计
小程序设计21 分钟前
本科毕设避坑|大数据 / 单片机物联网 / 网络工程 / PLC / 机械设计选题与落地心得
java·hive·hadoop·单片机
FYKJ_201025 分钟前
django药饮个性化推荐系统54839-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
卓怡学长27 分钟前
w206基于SpringBoot的社区团购小程序设计与实现
java·spring boot·spring·intellij-idea
云浪28 分钟前
通过 Goroutine 搞懂 Go 并发与常见的 9 个坑
后端·go
Wx-bishekaifayuan35 分钟前
springboot贵州旅游系统55916-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
mldong43 分钟前
事务不归引擎管:ITransactionTemplate,聚合一致性的最后一块拼图
后端·架构
IT_陈寒6 小时前
Java中equals方法比了个寂寞?原来这才是正确的重写姿势
前端·人工智能·后端
Thneonl6 小时前
Celery 生产踩坑:1000 任务积压与 acks_late 双重执行
后端·python