Spring boot -- visit static resources & WebJars & Icons & Welcome page




1. Access WebJar resources

2. Access to static resources

3.favicon.ico Icon

4. Welcome page


When spring boot is used for web development, the bottom layer of boot actually uses spring mvc. When spring boot starter web dependency is added to the project, embedded tomcat and mvc dependency will be provided, and dependency tree can be viewed

1. Access WebJar resources

The Web front end uses more and more JS or CSS, such as jQuery, Backbone.js and Bootstrap. In general, we copy these Web resources to the Java directory and manage them manually. This common way is easy to cause file confusion, version inconsistency and other problems.

Webjars package these general Web front-end resources into Java Jar packages, and then manage them with Maven tools to ensure the uniqueness of these Web resources, and upgrade them easily. As for webjars resources, there is a special website https://www.webjars.org/. We can find the resources we need on this website, and add Maven dependency to our projects, and then we can directly use these resources.

Direct access


Principle process:

View the imported jar package

SpringBoot redirects access to / webjars / * * to classpath:/META-INF/resources/webjars/**

Source code is as follows

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if(!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if(!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if(!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));


So you can use the directory localhost:8080/webjars/jquery/3.3.1/jquery.js to access static resources

2. Access to static resources

Under the default configuration of SpringBoot, the following static resource directories are provided:

/static: classpath:/static/

/public: classpath:/public/

/resources: classpath:/resources/

/META-INF/resources: classpath:/META-INF/resources/

Of course, you can specify the location of static files through the spring.resources.static-locations configuration.

   #Configure static resources
        #Specify static resource directory
        static-locations: classpath:/mystatic/


3.favicon.ico Icon

If there is a favicon.ico file in the configured static resource directory, SpringBoot will automatically set it as the application icon.

In the configuration file application.properties of Spring Boot, you can add the configuration item spring.mvc.favicon.enabled=false to turn off the default favicon,

4. Welcome page

Spring boot supports static and template welcome pages. First, check the index.html file in the static resource directory as the home page, which is mapped by / * *

The public number java number one is more java actual combat project data and technical work. What's more, ape is willing to be a friend on your way to programming!

First address: www.javayihao.top

First public address: java No. 1

Tags: Java Spring JQuery SpringBoot

Posted on Thu, 07 Nov 2019 03:43:05 -0500 by andynightingale