微人事第四天:两种静态资源位置

我们知道静态资源文件放在四个特点的位置可以访问到,现在我们想放在我们自定义包下,然后通过路径也能访问到。
现在我们在resources文件下创建javaboy文件夹,在文件夹下创建hello.js文件
在这里插入图片描述
第一种
在application.properties文件下配置静态资源访问路径

spring.resources.static-locations=classpath:/javaboy/

启动访问路径:http://localhost:8080/hello.js
在这里插入图片描述
访问成功

第二种
第二种方式就是自己写配置类

package org.javaboy.staticresource.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 WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/javaboy/");

    }
}

这种方式是添加了springboot中为我们添加的静态资源访问路径
访问路径:http://localhost:8080/hello.js
同样可以成功访问到

发布了287 篇原创文章 · 获赞 24 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41998938/article/details/103987457
今日推荐