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/**
相关推荐
王的宝库4 分钟前
Go 项目结构:从单文件到标准工程布局
开发语言·后端·golang
许彰午17 分钟前
27-AuthService登录链路
java·低代码·架构
泡海椒22 分钟前
JQuick-Curl 性能分析:并发场景下的性能表现与调优,第三方接口调用不只要快写也要稳跑
java·开发语言·okhttp
码事漫谈1 小时前
类型即世界:一个 C++ 程序员的本体论入门
后端
Rain的Java大神之路1 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
自动化监测Learner2 小时前
Navicat Premium 17 中文版安装教程(2026最新版)
java·linux·数据库
那个松鼠很眼熟w2 小时前
4.Spring-Ai入门案例
java·人工智能·spring
计算机毕设定制辅导-无忧学长2 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
泡海椒3 小时前
JQuick-Curl 二次开发:自定义解析器、扩展点开发指南,如何把框架能力真正变成团队资产
java·开发语言·okhttp·maven
MetaLite3 小时前
AI编程与工程底座-让AI遵守SpringBoot边界
人工智能·spring boot·ai编程