SpringBoot integrated WEB development - (b) Static Resource Access

1. Default Policy:

Location of a total of five static resources, developers can put either one static resources, namely:

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

"classpath:/resources/",

"classpath:/static/",

"classpath:/public/",

"/"。

  The five positions are arranged in order of priority, if the same resources had put such pictures, in accordance with the priority order look down, IDEA create SpringBoot project, created by default "classpath: / static /", will be on static resource files inside on the line, you do not need to create additional folders.

 

 

When the / META-INF / resources / case file deletion, he will visit the same name in the file resources:

 

 

 

 

2. Custom Policy:

  If you want to customize a static resource filtering policy, there are two ways:

   2.1 in the configuration file : application.properties


spring.mvc.static-path-pattern=/static/** spring.resources.static-locations=classpath:/static/

Filter rules / static / **, static resource position as classpath: / static /

http: // localhost: 8080 / static / p1.png to see resources

 

 

   2.2Java code definitions :

Writing a class configuration, the interface can be implemented WebMvcConfigurer, wherein the method is implemented addResourceHandlers, in which the writing addResourceHandler filtering rules and static resource location

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}

http://localhost:8080/static/p1.png即可看到资源

 

Guess you like

Origin www.cnblogs.com/crazy-lc/p/12316792.html