SpringBoot怎样通过地址栏直接访问到静态页面?

最近发现,可以直接访问到css等,但是不能直接访问到html,后面发现是SpringBoot默认配置了一些目录是可以直接访问的:

    public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private String[] staticLocations;
    private boolean addMappings;
    private final ResourceProperties.Chain chain;
    private final ResourceProperties.Cache cache;

    public ResourceProperties() {
        this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
        this.addMappings = true;
        this.chain = new ResourceProperties.Chain();
        this.cache = new ResourceProperties.Cache();
    }
    ...
}

可以看出默认可以直接访问的是"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"

可以添加一些目录或者直接覆盖使用配置文件(yml/properties),我使用的是application.properties:

# 配置这个是可以让游览器直接访问到html
spring.resources.static-locations: classpath:/static/, classpath:/templates/

配置好后启动项目发现可以直接访问了,但是使用了Thymeleaf的地方就没用了(毕竟访问的是静态的),所以一般情况下这个配置不写吧,知道就行了。emmm。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值