其他分享
首页 > 其他分享> > SpringBoot使用webjars处理静态资源

SpringBoot使用webjars处理静态资源

作者:互联网

配置方法

  1. 打开WebJars - Web Libraries in Jars,找到jquery
  2. 导入依赖
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.6.0</version>
    </dependency>

     3. 在resources文件夹下,建立static,public,resources文件夹,其中3个文件夹的文件访问优先级为,resources>public>static。

public void addResourceHandlers(ResourceHandlerRegistry registry) {
          if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
          } else {
                this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());
                    if (this.servletContext != null) {
                        ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                        registration.addResourceLocations(new Resource[]{resource});
                    }

                });
          }
 }

根据代码,如果在properties文件中建立了映射,则不会自动访问resources的文件夹。

标签:webjars,SpringBoot,静态,文件夹,registry,registration,public,resources
来源: https://blog.csdn.net/asduk/article/details/120244153