参考博客:SpringBoot+Thyemleaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible
application.yml的配置:
#thymeleaf 解决打war包 templates目录下文件未成功打包问题
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
content-type: text/html
cache: false
还看到有人说在启动类上加上@EnableWebMvc:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
@EnableWebMvc
public class BdysoApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(BdysoApplication.class, args);
}
/*
加上@EnableWebMvc表示忽略SpringBoot默认的/static目录,
使用src/main/webapp目录作为静态资源目录。
此外,你可能还需要重新制定静态资源的处理方式,就像这样:
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
注意下图中的ApplicationTests文件 在打包前一定要删除掉 :