Spring Boot access static resources css/js

Insert image description here

Welcome everyone to follow the official account [Xiaobai Technology Circle] and send B03 or b03 to receive a large Java interview package!

I. Introduction

When we use Spring Boot to build web applications (such as building a blog), we often need to access some static resources in Html, such as:

  • css style;
  • js script;
  • favicon.ico icons, etc.;

In Spring Boot, if no configuration is done, static resources cannot be directly accessed, and a 404 error will usually be reported.

2. Spring Boot’s default directory for accessing static resources

Spring Boot accesses static resources and has two default directories by default:

  • classpath/staticTable of contents:src/mian/resource
  • ServletContextUnder the root directory:src/main/webapp

This is classpath3 ?

Here is a brief introduction, classpath is the classes directory under WEB-INF, which is the src/main/resource directory in the Spring Boot project.

2.1 classpath directory - access the default folder named static

Project directory screenshot:

image.png

Someone here wants to say, can I modify the access path? The answer is yes, it can definitely be done.

Set in the properties file and it will be ok. spring.resources.static-locations

spring.resources.static-locationsThe default value is:classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

Image modification: I changed the default path to src/main/resource/static/images/, in which I wrote an index.html and html img

image.png

When accessing, it looks for the path I set.

2.2 ServletContext root directory (src/main/webapp) - webapp is the default access folder

This may be familiar to many people. Generally speaking, src/main/java contains Java code, resource contains configuration files and xml, and webapp contains Page, js and the like.

The root directory of ServletContent issrc/main/webapp

Generally, there is no webapp folder in the maven project created. Here we create a webapp ourselves in the src/main directory

Project directory, and access screenshots:

image.png

3. Spring Boot solution for accessing static resources

The above knowledge points are mainly for popular science. As for how to access static resources in Spring Boot,You can solve the above problems through the following two solutions:

3.1 The first option (recommended)

Modify application.yml and add the following configuration:

# 放开 Spring Boot 项目中 /static 目录下静态资源的拦截
spring:
  mvc:
    static-path-pattern: /static/**

application.propertiesThe file is added as follows:

spring.mvc.static-path-pattern=/static/**

3.2 The second option

Add a WebMvcConfig.java configuration class to tell springboot the loading path of static resources:


package com.exception.qms.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @date 2020/10/7
 * @time 14:37
 * @discription
 **/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
    

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}

Choose one of the two, after the addition is completed, we can access the static resources normally in Spring Boot~

Guess you like

Origin blog.csdn.net/lcmaijia/article/details/134356017