spring boot的web开发(静态资源映射规则)

在以前spring mvc时代,我们是将静态资源放在WebContent文件夹下。只要在前端控制器中没有被拦截,那么我们就可以通过(例如:http://localhost:8080/springmvc-mybatis/js/index.js)这样的访问路径,访问我们的静态资源。

那么在spring boot时代,是怎么去访问静态资源的呢?像以前在spring mvc时代,我们要用这个框架,就得把它给整合进来(导包+配置前端控制器+处理器映射器+处理器适配器+视图解析器等等),拦截规则也是我们在前端控制器做了配置。但是现在spring boot通通帮我们配置好了,所以想要了解静态资源的映射规则,就必须去看spring boot的自动配置文件即WebMvcAutoConfiguration

 

  • 所有的/webjars/**,都会去classpath:/META-INF/resources/webjars/   找资源
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Integer cachePeriod = this.resourceProperties.getCachePeriod();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
                }

            }
        }

webjars:以jar包引入的静态资源,比如jquery,bootstrap等

我们以引入jquery为例,首先需要在pom.xml中引入依赖

  <!-- 引入jquery -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>

接着我们可以在External Libraries,看到我们引入的jar包

我们可以通过访问http://localhost:8080/webjars/jquery/3.4.1/jquery.js,访问到/META-INF/resources/webjars/jquery/3.4.1/jquery.js

 

  • 访问当前任何静态资源,会去默认静态文件夹找资源
————————也就是addResourceHandlers(上面那段代码)if下面那一部分———————— 
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
                }

点开new String[]{staticPathPattern}的staticPathPattern,可以看到它要处理的是/**请求

再点开this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod)的getStaticLocations()

 

可以看到,它映射到的路径是CLASSPATH_RESOURCE_LOCATIONS

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

 可以看到CLASSPATH_RESOURCE_LOCATIONS对应的是

"classpath:/META-INF/resources/",

"classpath:/resources/",

"classpath:/static/",

"classpath:/public/"

这个四条路径

也就是说当我们访问http://localhost:8080/templates/success.html,它就会去这四个目录下找到有没有templates/success.html这个资源,而不是去http://localhost:8080/templates/这个目录下找success,html这个资源

由于这是静态资源文件夹,所以我们一般是拿来存放静态资源的(比如css,js),这些资源一般是写死的,不需要做改变的。比较少拿来放html文件,因为在thymeleaf这个模板引擎下,html是可以动态填充数据的,一般放在template这个文件夹下。至于thymeleaf的用法我会在下一篇博客讲解。

 

  • 自定义静态资源映射文件夹

  上面那个四个是默认的静态文件夹,默认情况下访问静态资源都会去这四个静态文件夹找资源,但是我们也可以不用这几个文件夹,可以自定义静态资源文件夹,只要在application.properties下做如下配置,那么它就不再去默认的静态文件夹找资源,而是去classpath:/hello/下找

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

 

  • 欢迎页的映射
 @Bean
        public WebMvcAutoConfiguration.WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {
            return new WebMvcAutoConfiguration.WelcomePageHandlerMapping(resourceProperties.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }

点开getWelcomePage()

再点开getStaticWelcomePageLocations

可以看到当我们直接访问https://localhost:8080/时,会自动映射到https://localhost:8080/index.html

 

  • 图标映射

        @Configuration
        @ConditionalOnProperty(
            value = {"spring.mvc.favicon.enabled"},
            matchIfMissing = true
        )
        public static class FaviconConfiguration {
            private final ResourceProperties resourceProperties;

            public FaviconConfiguration(ResourceProperties resourceProperties) {
                this.resourceProperties = resourceProperties;
            }

                ——————————————————映射到faviconRequestHandler———————————————
            @Bean
            public SimpleUrlHandlerMapping faviconHandlerMapping() {
                SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                mapping.setOrder(-2147483647);
                mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                return mapping;
            }

            @Bean
            public ResourceHttpRequestHandler faviconRequestHandler() {
                ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
                requestHandler.setLocations(this.resourceProperties.getFaviconLocations());
                return requestHandler;
            }
        }

点开faviconRequestHandler

再点开 getFaviconLocations,可以看到映射静态文件夹下

所有我们只要在静态文件夹下放入favicon.ico这个资源

再到html文件中,加入如下配置

那么访问路径时,便能加载到该图标

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值