我的示例project的结构
【1】路径配置
(例如,我的java包是 com.example.test2.config)
一、在pom.xml里加一段下面的代码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、新建一个java类,放入下面的代码
package com.example.test2.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
}
}
三、在application.properties文件加入
spring.thymeleaf.mode=HTML
(下面是我的application.properties示例代码,具体看自己的代码调整)