SpringBoot | 如何配置静态资源的地址与访问路径

一、spring.mvc.static-path-pattern

#这表示只有静态资源的访问路径为/static/**时,才会处理请求 e.g:localhost:9001/static/1.txt
#spring.mvc.static-path-pattern=/static/**

二、spring.resources.static-locations

 

#这表示按照下面路径的顺序依次查找,才会处理请求 e.g:localhost:9001/1.txt
#spring.resources.static-locations=classpath:/static,classpath:/upload

项目结构:

 

访问http://localhost:9001/1.txt出现如下结果表明访问成功。

 

 

三、静态资源的Bean配置

 

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/1/14 12:46
 * @description V1.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


   /**
     * @author xiaobu
     * @date 2019/1/18 13:51
     * @param registry  registry
     * @descprition  等价于 http://localhost:9001/1.txt 依次在static upload目录下找1.txt文件
     * @version 1.0
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/","classpath:/upload/");
    }

	}

 

 


 易混淆的点:

项目路径:
#默认是server.servlet.context-path=/

#server.servlet.context-path=/test

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值