Location>code7788 >text

Eight, SpringBoot Web development access to static resources (with + detailed source code analysis)

Popularity:901 ℃/2024-09-08 15:51:41

Eight, SpringBoot Web development access to static resources (with + detailed source code analysis)

@

catalogs
  • Eight, SpringBoot Web development access to static resources (with + detailed source code analysis)
  • 1. Basic introduction
  • 2. Quick start
    • 2.1 Preparations
  • 3. Change the static resource access prefixes to what we want them to be
  • 4. Change the default static resource path in Spring Boot (to achieve custom static resource path)
  • 5. Static resource access considerations and details
  • 6. Summary:
  • 7. Finally:


1. Basic introduction

Access to static resources in SpringBoot:

  1. Static resources can be accessed directly by placing them under the class paths: /static, /public, /resources, /META-INF/resources - the corresponding files (This is set by default in Spring Boot ). On this point, we start with the This class can be found on the source code, with the corresponding configuration properties.

在这里插入图片描述

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", " classpath:/static/", "classpath:/public/"};
Note: classpath:/resources/ means that the server will look for it under the resources path, and you can't enter the resources directory when you enter the url address in the browser, because the server will look for it under classpath:/resources/, and if you write resources/ in the browser, you want to express the name of the url address. in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!

Note: classpath:/resources/ means that the server will look in the resources path, you can not enter the url address in the browser, you can not enter the resources directory, because the server is in the classpath:/resources/ to find, and if you write resources/, you want to express: let the browser from the resources/resouces path to find, this is not found to report a 404 error! in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!

  1. Common static resources: js, css, images (.jpg, .png, .gif, .bmp, .svg), font files (Fonts) and so on.
  2. Access method: Default: project root path/+static resource name e.g..http://localhost:8080/ The answer to this question can be found in the class. The answer to this can be found in the class.

在这里插入图片描述

2. Quick start

2.1 Preparations

Import the relevant jar dependencies in the pom .xml file. As follows

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    <modelVersion>4.0.0</modelVersion>

    <groupId></groupId>
    <artifactId>springboot_static_configuration</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!-- import (data)SpringBoot parent project-style of writing (grammar)-->
    <parent>
        <groupId></groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
    </parent>

    <!-- import (data)webProject Scene Launcher:会自动import (data)和webdevelopment-relatedjarPackage all dependencies【storehouse/jar】-->
    <!-- I'll explain later.spring-boot-starter-web What exactly are the relevant dependencies introduced-->
    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Write the startup program:

在这里插入图片描述

package ;


import ;
import ;

@SpringBootApplication // Logo activation scenarios
public class Application {

    public static void main(String[] args) {
        (,args);
    }
}


topBasic Introduction Among other things, we learned that Spring Boot's default access paths for static resources are4 We'll test here whether these four paths areDirect access to

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

Note: classpath means the class path, that is, as shown below:.resources The catalog, in a nutshellclasspath === (equivalent to) resources

在这里插入图片描述

Below: We create the default four Spring Boot directories under the resources class path. As shown in the figure below:
在这里插入图片描述

In the meantime we put a few images in these four directories for access testing.

在这里插入图片描述

Start the program to run the test:

在这里插入图片描述

Open a browser for direct access to static resource files:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Attention:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", " classpath:/static/", "classpath:/public/"};
Note: classpath:/resources/ means that the server will look for it under the resources path, and you can't enter the resources directory when you enter the url address in the browser, because the server will look for it under classpath:/resources/, and if you write resources/ in the browser, you want to express the name of the url address. in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!

在这里插入图片描述

resources/static is Spring Bopt's default static path, the default is to "resources/static" as the root path to access, so there is no need to add additional static in the browser, if you add, then you are actually accessing the If you add static in the browser, then you actually access the path: resources/static/static, this path does not exist in the resources, so the error can not be found.

3. Change the static resource access prefixes to what we want them to be

Change the static resource access prefix, e.g. we want thehttp://locahost:8080/rainbowsea/* Under the request path to request static resources, application scenario: static resource access prefix and controller request path conflict.

We need to use theyaml If you want to know more about yaml syntax, please go to ✏️✏️✏️.Spring Boot when the yaml syntax to use-CSDN Blogs

First of all, we are in theresources class path to create a file named of the document.

在这里插入图片描述

Prepare the following:

在这里插入图片描述

spring:
  mvc:
    static-path-pattern: /rainbowsea/**


Run the test:

在这里插入图片描述

was changed by us to our own/rainbowsea/** Note: The latter /** It cannot be omitted . Otherwise it's inaccessible.

在这里插入图片描述


4. Change the default static resource path in Spring Boot (to achieve custom static resource path)

Spring Boot also supports us to customize static resource paths for increased flexibility.

Change the default static resource path, e.g., we add the test directory under the class path as a static resource path ourselves and complete the test.

Again, to change the default static resources in Spring Boot, we'll use the yaml syntax here. In theresources class path to create a file named of the document.

yaml is written as follows:

在这里插入图片描述

spring.
  web.
    resources: # modify/specify static resource access path/location
      # Modify/specify the access path/location of static resources
      static-locations: ["classpath:/test/"] # parody
# Note: Try to start on the far left for more hints!

在这里插入图片描述

Under the resources class path, create a test directory, and in that directory, place a file named of the picture, visit the test.

在这里插入图片描述

Open a browser to access the test:

在这里插入图片描述

Essentially: static-locations modifies the value of the staticlocations property in the WebProperties class (i.e., the springboot

the default static path).

So here we have modified the default static resource path of Spring Boot, and the resources previously placed under the default static path of Spring Boot cannot be accessed.

在这里插入图片描述

Wanted: To keep the original Spring Boot default static resource paths, just add the original Spring Boot default paths.

在这里插入图片描述

Run the test:

在这里插入图片描述

5. Static resource access considerations and details

  1. Note: If you put it directly under the root path of resources class, you can't access it. Because we know from the source code of the WebProperties class that there are only four default static resource paths in Spring Boot, and the path of the resources class does not belong to one of these four.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

Run a test: we put an image in the resources class path and try to see if it can be accessed directly.

在这里插入图片描述

Open a browser to access the test:

在这里插入图片描述

  1. When the default request path of the resource's name and Controller Controller request processing the same path, the conflict. ** Priority to see if the Controller can handle; can not, the processing of the request to the static resources to deal with, if the static resources can not be found also the corresponding point of resources, then report: 404 can not be found, the page.

Static resources are accessed principle: static mapping is /** , that is, all requests are intercepted, the request comes in, first look at the Controller can not handle, can not handle the request to the static resources to deal with, if the static resources can not be found, the corresponding 404 page

在这里插入图片描述

在这里插入图片描述

package ;


import ;
import ;

@RestController // @Controller + @ResponseBody
public class HiController {

    @RequestMapping("") // Controller controls the path of the request to be processed and the name of the static resource to conflict with.
    public String hi(){
        return "hi"; }
    }
}

在这里插入图片描述


6. Summary:

  1. Understand that Spring Boot static resources are placed under the class paths: /static, /public, /resources, /META-INF/resources and can be accessed directly - the corresponding files (This is set by default in Spring Boot ). On this point, we start with the This class can be found on the source code, with the corresponding configuration properties.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", " classpath:/static/", "classpath:/public/"};
Note: classpath:/resources/ means that the server will look for it under the resources path, and you can't enter the resources directory when you enter the url address in the browser, because the server will look for it under classpath:/resources/, and if you write resources/ in the browser, you want to express the name of the url address. in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!
  1. Access method: Default: project root path/+static resource name e.g..http://localhost:8080/ The answer to this question can be found in the class. The answer to this can be found in the class.
  2. Change the static resource access prefix, e.g. we want thehttp://locahost:8080/rainbowsea/* Under the request path to request static resources, application scenario: static resource access prefix and controller request path conflict.

在这里插入图片描述

  1. Change the default static resource paths in Spring Boot (implement custom static resource paths).

在这里插入图片描述

  1. Note: If you put it directly under the root path of resources class, you can't access it. Because we know from the source code of the WebProperties class that there are only four default static resource paths in Spring Boot, and the path of the resources class does not belong to one of these four.

7. Finally:

"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for me to create, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."

在这里插入图片描述