springboot (7) - access static resources & WebJars & & Welcome page icon

table of Contents

 

Outline

 1. Visit WebJar resources

2. Access static resources

3.favicon.ico icon

4. Welcome Page


Outline

When using Springboot web development, the Boot underlying actual use is springmvc, the project added spring-boot-starter-web dependent, will provide embedded tomcat and mvc dependency, you can view the dependency tree

 1. Visit WebJar resources

Web front-end using more and more of JS or CSS, such as jQuery, Backbone.js and Bootstrap. Under normal circumstances, we are of these resources are copied to the Java Web directories are managed by hand, through this way easily lead to confusion document, inconsistent versions and other issues.

WebJars that these generic front-end Web resources packaged into Java Jar package, and then help them manage Maven tools, Web resources to ensure the uniqueness of these versions, the upgrade easier. About webjars resources, there is a dedicated website https://www.webjars.org/, we can find the resources they need to on this site, add the maven-dependent, you can use those resources directly in their own projects.

direct interview

 

Principle process:

View jar package introduced

SpringBoot will /webjars/**redirect access toclasspath:/META-INF/resources/webjars/**

源码如下

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));
                }

            }
        }

It is possible to use the directory localhost: 8080 / webjars / jquery / 3.3.1 / jquery.js access static resources

2. Access static resources

SpringBoot default configuration, provides several static resource directory:

/static:classpath:/static/

/public:classpath:/public/

/resources:classpath:/resources/

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

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

   #配置静态资源
    spring:
      resources:
        #指定静态资源目录
        static-locations: classpath:/mystatic/

 

3.favicon.ico icon

If there favicon.ico file in the configuration of static resource directory, SpringBoot will automatically be set to the application icon.

In the configuration file application.properites Spring Boot may be added to the configuration items spring.mvc.favicon.enabled = false disables default favicon,

4. Welcome Page

SpringBoot supports static templates and welcome page, it first looks index.html file in a static resource directory as your home is / ** mapped

                         No more public on the 1st java java actual project data, technical work. More importantly, the little ape is willing to be a friend you programmed the road!

Articles starting address: www.javayihao.top

No public debut: java One

Guess you like

Origin www.cnblogs.com/javayihao/p/11812575.html