美文网首页
springboot不通过controller直接访问静态htm

springboot不通过controller直接访问静态htm

作者: anthit | 来源:发表于2019-01-29 18:20 被阅读0次

    默认配置下,通过springboot搭建的web服务的资源中,可直接访问只有"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"这几个目录下的,源码在类ResourceProperties中,如下:

        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();
        }
        ...
    }
    

    也可以在application.properties/.yml中配置spring.resources.static-locations来覆盖默认配置,例如(application.yml):

        spring:
          resources:
            static-locations: classpath:/static/, classpath:/templates/
    

    以下是配置静态资源目录的源码:

        public void setStaticLocations(String[] staticLocations) {
            this.staticLocations = this.appendSlashIfNecessary(staticLocations);
        }
    
        private String[] appendSlashIfNecessary(String[] staticLocations) {
            String[] normalized = new String[staticLocations.length];
    
            for(int i = 0; i < staticLocations.length; ++i) {
                String location = staticLocations[i];
                normalized[i] = location.endsWith("/") ? location : location + "/";
            }
    
            return normalized;
        }
    

    参考文章:
    spring boot怎么直接访问html?

    相关文章

      网友评论

          本文标题:springboot不通过controller直接访问静态htm

          本文链接:https://www.haomeiwen.com/subject/axlzdqtx.html