java - Spring Boot打包jar时,Thymeleaf不会加载模板

标签 java spring spring-mvc spring-boot thymeleaf

我遇到了在 Stack Overflow 上多次看到的错误,但似乎没有一个解决方案适合我。

基本上,我使用 Thymeleaf 3 运行 Spring Boot 4,当我使用 ./gradlew bootRun 在本地进行测试时,我的模板加载得很好。

但是当我打包我的 jar 并尝试运行它时,我在访问我的端点时不断收到此错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers

我的模板都位于 src/main/resources/templates/下,并且是以 .html 结尾的文件,例如登录.html

这是我的 @Configuration 类,用于为 Thymeleaf 配置模板解析器:

@Configuration
@EnableWebMvc
@ComponentScan
public class TemplateConfig extends WebMvcConfigurerAdapter {

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5")
    public ClassLoaderTemplateResolver templateResolverClassLoader() {

        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();

        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setOrder(2);

        return templateResolver;
    }

    @Bean
    public ServletContextTemplateResolver templateResolverServletContext() {
        final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    @Bean
    @Description("Thymeleaf template engine with Spring integration")
    public SpringTemplateEngine templateEngine() {

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();

        final Set<TemplateResolver> templateResolvers = new HashSet<>();
        templateResolvers.add(templateResolverClassLoader());
        templateResolvers.add(templateResolverServletContext());
        templateEngine.setTemplateResolvers(templateResolvers);

        return templateEngine;
    }

    @Bean
    @Description("Thymeleaf view resolver")
    public ViewResolver viewResolver() {

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");

        return viewResolver;
    }

}

此外,如果我删除了上面的整个@Configuration 类,它仍然会出现相同的错误

我的 build.gradle 中总是有 thymeleaf 的东西

compile("org.springframework.boot:spring-boot-starter-thymeleaf")

我在这里错过了什么?谢谢!

最佳答案

我没有使用 Gradle,而是使用 Maven,所以我会尝试考虑一些可以帮助其他用户的要点。 Spring boot 的版本在配置上可能会有很小的差异,因此对于此答案,请考虑 Spring boot 1.5.10。

  1. 由于 Spring boot 使用“Convention over Configuration”工作,要使 thymeleaf 正常工作,您需要做的就是在 pom.xml 中添加正确的依赖项(对于 maven):
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>;

有了这个依赖,thymeleaf 就会在资源/模板中寻找 html。

  1. 确保您的 Spring boot 入口应用程序类具有正确的注释,例如:
    @SpringBootApplication
    @EntityScan(basePackageClasses = { Application.class })
    public class Application
    {    
        public static void main(String[] args)
        {
            SpringApplication.run(Application.class, args);
        }
    }
  1. 如果您不想缓存您的模板,请在您的属性或 yaml 文件中告知这一点。

    spring.thymeleaf.cache=false

关于java - Spring Boot打包jar时,Thymeleaf不会加载模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251763/

相关文章:

java - 调整 3 列通用组件的布局,其他组件位于上方/下方

java - 用图像(.png)填充 ShapeRenderer 矩形?

java - Jackson双向嵌套对象设置

java - thymeleaf : Getting value from a map while iterating through a list

java - 仅当没有 Exception 映射时,才会调用 @ExceptionHandler for Error

java - ImageIO.read( ) 总是旋转我上传的图片

java - 将特定的外部库包含到 Gradle 构建的 jar 文件中

java - 当另一端无法接收时处理队列中的项目

java - 使用hibernate向数据库插入数据

java - 在 Spring Hibernate 中首次运行服务器时自动创建数据库和表而不执行任何操作?