Springboot+thymeleaf项目打war包 部署至服务器 templates和webapp目录下都有页面 解决页面未成功打包问题

参考博客: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文件 在打包前一定要删除掉 :
在这里插入图片描述

发布了98 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37767455/article/details/103866828