SpringBoot eleven: SpringBoot mapping rules for static resources

SpringBoot's mapping rules for static resources

Dynamic resource mapping (jar dependency)

1. Dynamic resources, all /webjars/** requests, go to classpath:/META-INF/resources/webjars/ to find resources

webjars: Introduce static resources in jar package

webjars official website: https://www.webjars.org/

If you want to introduce JQeury, Bootstrap, etc., you can go to the official website to find the corresponding jar dependencies and introduce them into SpringBoot

demonstration:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1-1</version>
</dependency>

After importing into pom.xml, go to view

For all /webjars/** requests that depend on , classpath:/META-INF/resources/webjars/ find resources

To test, visit http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js

 

Static resource mapping (folders such as static/public)

Features: Static resource mapping is mapped according to priority, high priority mapping will not map low priority

/** Access any resource of the current project

Classpath refers to the class path. Both java and resource belong to the class path, and static resource files are generally placed inside

When /** is not handled by anyone, it will automatically be mapped to the following five ways to find static resources ( priority mapping from high to low )

The first way "classpath:/META-INF/resources/" decentralize static resources
The second way "classpath:/resources/" decentralize static resources
The third way "classpath:/static/" decentralize static resources
The fourth way "classpath:/public/" decentralize static resources
The fifth way "/" Decentralize static resources

The picture demonstrates five kinds of static resource mapping

Usually development is usually put in static

The following is a picture placed in the static folder to demonstrate access (all unnecessary folders are deleted, and only static is left)

Start the project and visit http://localhost:8080/youngman.jpg

 

Welcome page settings

rule:

  • Homepage mapping, find index.html page under static resource folder , it is mapped by "/**
  • localhost:8080/ will go to the index page under the static resource file
  • When multiple index pages, map according to priority

For example, create a new index.html file under public, and then visit localhost:8080/

Note: If the template engine and the index page exist at the same time, when the template engine does not use the following figure, the static resource homepage will be used by default instead of templates.

 

 

Home page icon settings

All **/favicon.ico are found under static resource files (name and suffix are not allowed to be changed)

E.g:

Run localhost:8080/ and you will find that the icon becomes the homepage icon, and the homepage icon has changed

Note: Google Chrome may not display this icon, you can choose other browsers, and you need to clear the cache

 

Change the path of static files

Modify in the global configuration file application.properties

spring.resources.static-locations=classpath:/satic2/, after customizing the static folder path, the system is invalid

E.g:

As a result, all previous failures

Put it in the static2 folder

Take effect

 

Guess you like

Origin blog.csdn.net/qq_41055045/article/details/102530808