Springboot在Controller类上添加RequestMapping后原来的相对路径静态资源读取不到

先前编码时类上没有RequestMapping,静态资源又使用的相对路径,现在添加RequestMapping后将获取不到静态资源
Springboot在Controller类上添加RequestMapping后原来的相对路径静态资源读取不到_第1张图片
在这里插入图片描述
由于使用了相对路径,静态资源原来的路径上叠加出现了RequestMapping("/my")中的/my
这个时候添加添加一个配置类

package com.xhu.firstdemo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //所有访问/my/的都要去指定的位置下找资源
        registry.addResourceHandler("/my/**").addResourceLocations("classpath:/static/");
    }
}

可以解决问题
要完全解决问题还是要用绝对路径,使用thymeleaf语法,在HTML标签引入

<html xmlns:th="http://www.thymeleaf.org">

路径

<link rel="stylesheet" th:href="@{/css/font.css}">

你可能感兴趣的:(Springboot在Controller类上添加RequestMapping后原来的相对路径静态资源读取不到)