springbootf访问静态文件资源

springboot目录结构:

网友说在springboot的配置文件中加 

现在访问static目录下的jquery文件

用jquery在页面做一个弹窗

启动服务看页面效果

页面没有出现弹窗 ,连jquery文件都报404错误

解决办法

  在项目中的某个包中新建一个类

package com.example.student.util;

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

@Configuration
public class StaticConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    /**
     * /static/** 的意思是将static下的所有文件夹及相关子文件夹都添加进扫描路径
     * 修改之后重启项目就可以对static下的静态资源进行分类而且访问的时候不会出现404了
     */

}

再次启动项目:

弹窗已经完美的出现在html页面上,打开浏览器控制台再无错误。

原文地址:https://www.cnblogs.com/zhanggguoqi/p/10641937.html