springboot static resource access

2.1 static resource access

1. Static resource directory

Under classpath:

visit:Current project root path/ + Static resource name

What spring official documents tell us:

In fact, it maps / * *, that is, as long as you write the resource name, it will automatically find the static resource for you; If you want to change the root directory of the default static resource access, you can write spring.mvc.pattern in the configuration file


What scene is this?

Let's look at the principle of static file resource access. How does it know to find static resources instead of requests.

It's actually very convenient

For example, let me create a Controller.java first

package com.example.boot05web01.Contorller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {

    public String hello(){
        return "aaa";

For example, I happen to have a dynamic request called YSN1.jpg. We also have a static resource called YSN1.jpg, but the dynamic request returns aaa to me. Guess whether we visit YSN1.jpg at the same time. Does he find us a dynamic resource or a static resource in the static directory.

The browser returns us the aaa in the Controller

Principle: because our static mapping (/ * *) intercepts all requests; Coincidentally, our dynamic resources also depend on all requests, so as soon as the request comes in, we first go to the Controller to see if it can be processed. If the Controller can handle it, it can handle it. If not, because our static resources map / * *, all requests that cannot be processed by all resources will be handed over to the static resource processor, and the static resources will go to us (/ static /public /resources /META-INF/resources) and find it according to the name we requested. If the static resources cannot be found, it will be 404

By default, your static resources can be accessed by adding our root directory Lu Jin and our static resource name. However, if you want to add a prefix to the static resources, this is often used in the future. Let's give an example: our web application has too many static resources and then our dynamic requests, but there must be corresponding ones in our application The interceptor needs to perform some functions. For example, we log in to the interceptor. We can only access dynamic requests after we log in. Therefore, if we block / * * in general, it is equivalent to blocking all static files. Therefore, it is convenient to configure the interceptor in the future. We make all static file accesses with a prefix, so that our interceptor can release the road strength with the specified prefix If there is a request, it will be very convenient for us to put our filtered static resources in the future. Therefore, we generally hope that the road strength of static file access has a prefix

2. Access prefix for static resources

The default is no prefix. If we want to give it a specified prefix, spring officials say that you only need to specify and configure one


The configuration in yml is:

    static-path-pattern: /resources/**

Therefore, in the future, when we access static files, we should add / resources (access prefix, which can be res independent of the package name) and static file name under the current project directory

Current project (localhost: port number) / + static path Pattren / + static resource name = = find under static resource folder

Another point is: People's officials also say that you can change the road strength of this static resource


Because the path of static resources is found in / static /public /resources /META-INF/resources, I don't want it to find it in these four directories.

Change the default static resource path

spring:													#spring
 resources:												#Under resource policy
    static-locations: classpath:/MyStaticRes			#A configuration item
#Classpath: it means that all our static resources find a location under the classpath
#/MyStaticRes: we want to put all static files in this folder

In this way, after the change, the static resources under the previous four folders will not be found.

Warm tip: during the test, the static resources in the first three folders failed, but '/ META-INF/resources' is still OK

Path to static resource:

ctrl+Arrow Click static-locations see -> return super.getStaticLocations(); -> return this.staticLocations; -> private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
 -> private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
				"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

You can also configure multiple static resource paths because this is also an array

Array writing:

    static-locations: [classpath:/MyStaticRes/]

In addition, our spring official document also says that it can also map all data under / webjars / *

webjars are:

Give us this jar Package, equivalent to css,js He made one of these things jar package


webjars official address

Import dependency in pom.xml


You can write directly in the url

http://localhost: port number / webjars/jquery/3.6.0/dist/jquery.js

/The address of jquery/3.6.0/dist/jquery.js depends on the package path inside

Including those using the compressed version of jquery but not the full version:

http://localhost: port number / webjars/jquery/3.6.0/dist/jquery.min.js

Tags: Java Spring Spring Boot

Posted on Wed, 17 Nov 2021 05:58:19 -0500 by xux