【问题标题】:Spring Boot cannot change Thymeleaf template directory with Java configSpring Boot 无法使用 Java 配置更改 Thymeleaf 模板目录
【发布时间】:2017-09-28 08:24:30
【问题描述】:

将 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");
    }
}

我做错了什么?

【问题讨论】:

    标签: java spring spring-boot thymeleaf


    【解决方案1】:

    以下配置 (Spring Boot 2.5.4),我在其中实例化 FileTemplateResolver 最适合我。

    在科特林中:

    @Configuration
    class ThymeleafConfig {
    
       @Value("\${spring.thymeleaf.prefix}")
       private val mailTemplatesPath: String? = null
    
       @Bean
       fun thymeleafTemplateEngineWithFileResolver(templateResolver: 
                        ITemplateResolver?): SpringTemplateEngine? {
        val templateEngine = SpringTemplateEngine()
        templateEngine.setTemplateResolver(thymeleafFileTemplateResolver())
        return templateEngine
       }
    
       fun thymeleafFileTemplateResolver(): ITemplateResolver {
           val templateResolver = FileTemplateResolver()
           templateResolver.prefix = mailTemplatesPath
           templateResolver.suffix = ".html"
           templateResolver.setTemplateMode("HTML")
           templateResolver.characterEncoding = "UTF-8"
           templateResolver.isCacheable = false
           return templateResolver
       }
    
    }
    

    然后属性spring.thymeleaf.prefix可以设置为任何目录,例如

    spring:
      thymeleaf:
        prefix: /home/foo/
    

    您还可以在运行 JAR 时从外部设置此属性。

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      第一个:在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

      【讨论】:

      • 它给了我一些提示,为什么它可能不起作用。但我无法让它工作; Spring Boot 不断给出相同的错误消息。 (没有 setCheckExistence() 方法。)
      【解决方案3】:

      这是我的发现:

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

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

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

      【讨论】:

        【解决方案4】:

        默认情况下,thymeleaf 模板从 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

        【讨论】:

          猜你喜欢
          • 2018-02-18
          • 2016-01-26
          • 2017-08-14
          • 2020-05-14
          • 1970-01-01
          • 2018-11-15
          • 2019-10-31
          • 2017-07-30
          • 1970-01-01
          相关资源
          最近更新 更多