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/**
相关推荐
长谷深风11112 小时前
Agent执行系统中的身份与版本设计
java·大数据·人工智能·ai·大模型·task·aiagent
Wang's Blog12 小时前
Java框架快速入门: Spring Security+OAuth2之UserDetails与JDBC认证实践
java·网络·spring
爱从未走散12 小时前
面试八股题
java·面试·职场和发展
艺杯羹12 小时前
AI编程时代软件工程怎么学:从底层思维认知到驱动智能体的架构跃迁
java·人工智能·ai·架构·软件工程·ai编程
土司大王12 小时前
LeetCode hot100——51.N 皇后:Java 回溯模板、列与对角线剪枝、O(n!) 复杂度分析
java·leetcode·剪枝
码外生活12 小时前
🎥 手搓一套直播高并发环境!一个后端小白从 0 到 1 的搭建笔记
后端·spring cloud
重生之小比特12 小时前
【Java SE】IDEA 调试 Debug 案例完整分析
java·ide·intellij-idea
晴天1612 小时前
JavaScript 与 Java 垃圾回收机制对比
java·开发语言·javascript
Lyra_Infra12 小时前
记一次麒麟 Linux 下达梦数据库 (DM8) 部署与 MySQL 命令行无界面迁移踩坑指南
数据库·后端·dba
Daemonkey13 小时前
苦 Elasticsearch/Prometheus 久矣?试试 Rust 打造的云原生可观测新星:OpenObserve
后端