Home > Java > javaTutorial > body text

Detailed explanation of static resource loading examples in java

Y2J
Release: 2017-05-10 10:28:51
Original
2452 people have browsed it

This article mainly introduces the static resource loading processing method in spring boot. Friends who need it can refer to it

1.spring boot default resource processing

Spring Boot provides us with static resource processing by default, using various properties configured in WebMvcAutoConfiguration.

The default path for spring boot to load files is:

/META-INF/resources/
/resources/
/static/
/public/

Under these directories, of course we can also see the Java code from the spring boot source code:

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

The above are all mapping paths of static resources, PriorityThe order is :META-INF/resources > resources > static > public

All local static resources are configured under the classpath, not under the webapp.

If the Sping MVC provided by Spring Boot does not meet the requirements, you can achieve fully controlled MVC configuration through a configuration class (a class annotated with @Configuration) plus the @EnableWebMvc annotation.

Of course, under normal circumstances, Spring Boot's automatic configuration meets most of our needs. When you need to retain the convenience provided by Spring Boot and add your own additional configuration, you can define a configuration class and inherit WebMvcConfigurerAdapter without using the @EnableWebMvc annotation.

If @EnableWebMvc is used, it will automatically cover the official /static, /public, META-INF/resources, /resources and other directories for storing static resources.

2. Custom resource mapping

Here we mention the WebMvcConfigurerAdapter class. Overriding the methods in this class allows us to add For additional configurations, here we will introduce a few commonly used ones.

Custom resource mapping addResourceHandlers

For example, if we want to customize the static resource mapping directory, we only need to rewrite the addResourceHandlers method.

@Configuration
public class SimpleWebAppConfigurer extends WebMvcConfigurerAdapter {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/myresource/**").addResourceLocations("classpath:/myresource/");
    super.addResourceHandlers(registry);
  }
}
Copy after login

Add the mapping path through addResourceHandler, and then specify the path through addResourceLocations.

If we change /myresource/* to /* which is the same as the default, the system configuration will be overwritten. You can use addResourceLocations multiple times to add directories. The priority added first is higher than the one added later.

3. Use external resources

#If we want to specify an absolute path to the folder (such as H:/images/), then only You need to specify it using addResourceLocations.

// 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:
registry.addResourceHandler("/myimgs/**").addResourceLocations("file:H:/myimgs/");
Copy after login

Configure through configuration file. The above is to use code to define the mapping of static resources. In fact, Spring Boot also provides us with the configuration that can be directly configured in application.properties (or .yml) Methods.

The configuration method is as follows:

# 默认值为 /**
spring.mvc.static-path-pattern=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开
Copy after login

Use spring.resources.static-locations to redefine the path pointed by pattern, supporting classpath: and file: (already explained above)

Note that only one spring.mvc.static-path-pattern can be defined, and multiple comma-separated methods are currently not supported.

【Related recommendations】

1. Free Java video tutorial

2. Comprehensive analysis of Java annotations

3. Alibaba Java Development Manual

The above is the detailed content of Detailed explanation of static resource loading examples in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!