整体框架 springboot,前端用的是thymeleaf,在加载过程中一直不能加载本地的静态资源,解决:
一种是设置配置解决:
1、查看包情况,是否有如下包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、application.yml 配置:
spring:
thymeleaf:
#缓冲的配置
cache: false
check-template: true
check-template-location: true
#开启MVC thymeleaf 视图解析
enabled: true
encoding: utf-8
mode: HTML5
prefix: classpath:/templates/
suffix: .html
resources:
static-locations: classpath:/static/
mvc:
static-path-pattern: /static/**
二种是添加一个配置内解决:
单独写一个配置类
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
}
或者直接写在启动类里,如:

这两种方法任取一种解决

1261

被折叠的 条评论
为什么被折叠?



