我是靠谱客的博主 端庄钢笔,最近开发中收集的这篇文章主要介绍【web开发】webjars和静态资源映射规则springboot对静态资源的映射规则,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

springboot对静态资源的映射规则

  1. WebMvcAutoConfiguration类中addResourceHandlers方法:
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

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

            }
        }

所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源。
在这里插入图片描述
webjars:以jar包方式引入静态资源。
addResourceHandlers方法中ResourceProperties类型变量:

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties

可以设置和静态资源有关的参数。

从addResourceHandlers方法中staticPathPattern变量可以看出,当"/**"访问当前项目的任何资源时,去以下位置找静态资源:

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

例如要访问:
在这里插入图片描述
可以直接访问:
http://localhost:8080/asserts/js/bootstrap.min.js
2. WebMvcAutoConfiguration类中welcomePageHandlerMapping方法:

        @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
            WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
            welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
            welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
            return welcomePageHandlerMapping;
        }
        
        private Optional<Resource> getWelcomePage() {
            String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
            return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
        }

        private Resource getIndexHtml(String location) {
            return this.resourceLoader.getResource(location + "index.html");
        }

可以看出欢迎页是静态资源文件夹下的所有index.html,被"/**"映射,例如http://localhost:8080/。
3. 所有的**/favicon.ico,都是在静态资源文件下找。
4. 可以在application.properties中更改静态资源文件位置:

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

最后

以上就是端庄钢笔为你收集整理的【web开发】webjars和静态资源映射规则springboot对静态资源的映射规则的全部内容,希望文章能够帮你解决【web开发】webjars和静态资源映射规则springboot对静态资源的映射规则所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部