Spring Boot无法使用Java配置更改Thymeleaf模板目录

时间:2017-04-30 19:13:45

标签: java spring spring-boot thymeleaf

将Thymeleaf模板文件放在默认的src/main/resources/templates中对我来说还可以。当我想重命名目录时,请说mytemplates;它不起作用。

我收到无法找到模板位置:classpath:/ templates /(请添加一些模板或检查您的Thymeleaf配置)警告何时 申请开始。

当我指向主页时,我得到 org.thymeleaf.exceptions.TemplateInputException:错误解析模板"索引",模板可能不存在或可能无法被任何配置访问模板解析器错误。

我使用以下Java配置:

package com.zetcode.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

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

        ClassLoaderTemplateResolver tres = new ClassLoaderTemplateResolver();

        tres.setPrefix("classpath:/mytemplates/");
        tres.setSuffix(".html");        
        tres.setCacheable(false);
        tres.setTemplateMode("HTML5");
        tres.setCharacterEncoding("UTF-8");

        return tres;
    }

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

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());

        return templateEngine;
    }

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

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setCache(false);
        viewResolver.setOrder(1);

        return viewResolver;
    }    

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}

我做错了什么?

3 个答案:

答案 0 :(得分:5)

默认情况下,从 src / main / resources / templates 文件夹中读取百万美元模板。 Chandu已为自定义模板位置名称提供了解决方案。

首先在类路径中添加application.properties文件,并在此文件中添加以下所有属性 src / main / resources / application.properties

spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.

资源链接:https://stackoverflow.com/a/41319170/2293534

答案 1 :(得分:4)

尝试以下方法:

1st:在application.properties文件

中定义以下设置
spring.thymeleaf.templateResolverOrder=1

现在自定义您的实施。

@Bean
public ClassLoaderTemplateResolver yourTemplateResolver() {
        ClassLoaderTemplateResolver yourTemplateResolver = new ClassLoaderTemplateResolver();
        yourTemplateResolver.setPrefix("yourTemplates/");
        yourTemplateResolver.setSuffix(".html");
        yourTemplateResolver.setTemplateMode(TemplateMode.HTML);
        yourTemplateResolver.setCharacterEncoding("UTF-8");
        yourTemplateResolver.setOrder(0);  // this is iportant. This way spring 
                                            //boot will listen to both places 0 
                                            //and 1
        emailTemplateResolver.setCheckExistence(true);

        return yourTemplateResolver;
    }

来源:Several template locations for Thymeleaf in Spring Boot

答案 2 :(得分:1)

以下是我的想法:

ClassLoaderTemplateResolver tres = new ClassLoaderTemplateResolver();
tres.setPrefix("mytemplates/");

使用classpath:时,不应有ClassLoaderTemplateResolver前缀。

模板目录不能为空。 Java配置优先于属性配置。 (使用顺序似乎不会影响这一点。)