Springboot is the most comprehensive way to directly access static resources

Method 1: Rewrite webConfig
package com.example.thymeleaf.commons;
 
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config. annotation.WebMvcConfigurer;
 
/**
 * Configure static resource mapping
 *
 * @author sunziwen
 * @version 1.0
 * @date 2018-11-16 14:57
 **/
@Component
public class WebMvcConfig implements WebMvcConfigurer {     /**      * Add static resource File, the address can be accessed directly from the outside      *      * @param registry      */     @Override     public void addResourceHandlers(ResourceHandlerRegistry registry) {







        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

 

Method 2: Modify the application file

application.yml : configuration method

spring:
  # Modify the default static addressing resource directory
  resources:
    static-locations: classpath:/templates/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring:
# Static file request matching method
   mvc:
    static-path-pattern: /**

application.properties: configuration method

# Static file request matching method
spring.mvc.static-path-pattern=/**
# Modify the default static addressing resource directory
spring.resources.static-locations = classpath:/templates/,classpath:/META-INF/resources /,classpath:/resources/,classpath:/static/,classpath:/public/

 

Access path      http://localhost:8080/project name/static/XXX.png

 

 

Guess you like

Origin blog.csdn.net/qq_36090127/article/details/103858249