当Spring Boot打包jar时,Thymeleaf将不会加载模板

时间:2017-09-16 08:17:59

标签: 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结尾的文件,例如login.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中总是有百里香的东西

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

我在这里想念什么?谢谢!

1 个答案:

答案 0 :(得分:1)

我不是在Gradle上工作,而是在Maven上工作,所以我会尽力考虑一些可以帮助其他用户的观点。 Spring boot的版本在配置上几乎没有差异,因此对于此答案,请考虑Spring boot 1.5.10。

  1. 当Spring引导使用“ Convention over Configuration”工作时,为使百里香叶正常工作所需要做的就是在pom.xml中添加正确的依赖项(对于Maven):
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>;

只有这种依赖性,百里香已经在资源/模板中寻找html。

  1. 确保您的Spring boot entry应用程序类具有正确的注释,例如:
    @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