SpringBoot: hello, web brother (the application of Springboot in the web scene) The third issue of springboot series

About static resources

  • As long as the static resources are placed in the classpath: called /static (or /public or /resources or /META-INF/resources
    Access: current project root path / + static resource name
    Principle: static mapping /**.
  • When a request comes in, first go to the Controller to see if it can be processed. All requests that cannot be processed are handed over to the static resource handler. If the static resource is not found, it will respond with a 404 page

As an example

Put a cute rice cooker in the static
insert image description here

insert image description here
insert image description here
Visited,
let's write a controller
insert image description here

We ca n't access it.
insert image description here
We can see that the priority of our controller is higher than that of static resources.

Why are our default resource paths like these?

called /static (or /public or /resources or /META-INF/resources
Let's take a look at the source code
insert image description here
. This is too long. I cut a part and we can see that these are written in the source code.

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

So there is a question, where can we customize the static ones?

Of course, is
insert image description here
n't this annotation to read the core configuration file?
Let's customize it in the configuration file

insert image description here
Let's talk about the following

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

This is the static resource access prefix
Current project + static-path-pattern + static resource name = search under the static resource folder
Because we have configured it to be found under /res/, so we need to add the following under the path.
insert image description here
Of
insert image description here
course, we can add it A lot of folders
insert image description here
can be added like this

A story about the welcome page

Look at what the official documentation says, it
insert image description here
probably means:

  • springboot supports static mode and template mode
  • If it is a static method, the file named index.html will be regarded as the welcome page, which is the page we will jump to when we visit the root directory.
  • If the index is not found, html springboot will go to the controller to find the index request and then jump to that page

Static welcome page

Before we set up static resources to be placed under /gb/ and /gb1/

insert image description here
Let's put the index page under /gb/ Let
insert image description here
insert image description here
's access the root path address
insert image description here

Change the page icon

what is the icon

insert image description here
this is the icon
insert image description here

how to change

insert image description here
It means that you can change the name of the icon you want to change to this and put it in the static resource.
Just use my avatar. The
insert image description here
insert image description here
wise avatar has been placed and pinched.
insert image description here
If you succeed, you can see my wise avatar.
insert image description here

Why do I have to switch browsers to access

Because the browser reads the icon is shared in a session, the
browser will not change if the session is not closed,
directly changing the browser is equivalent to a new session

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324116084&siteId=291194637