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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43710326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:39:58  来源:igfitidea点击:

Spring Boot cannot change Thymeleaf template directory with Java config

javaspringspring-bootthymeleaf

提问by Jan Bodnar

Placing Thymeleaf template files in the default src/main/resources/templatesworks OK for me. When I want to rename the directory say to mytemplates; it does not work.

将 Thymeleaf 模板文件放在默认位置src/main/resources/templates对我来说是可行的。当我想重命名目录时说mytemplates; 这是行不通的。

I receive Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)warning when the application starts.

当应用程序启动时,我收到无法找到模板位置:类路径:/模板/(请添加一些模板或检查您的 Thymeleaf 配置)警告。

When I point to the home page, I get org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolverserror.

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

I use the following Java configuration:

我使用以下 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");
    }
}

What I am doing wrong?

我做错了什么?

回答by SkyWalker

By default, thymeleaf templates are read from src/main/resources/templatesfolder. Chanduhas given a solution for custom template location name.

默认情况下,thymeleaf 模板是从src/main/resources/templates文件夹中读取的。Chandu给出了自定义模板位置名称的解决方案。

You first add a application.properties file in your class path and put all the following properties src/main/resources/application.propertiesin this file.

您首先在类路径中添加一个 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.

Resource Link: https://stackoverflow.com/a/41319170/2293534

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

回答by Achille_vanhoutte

Try the following :

尝试以下操作:

1st : define below setting in application.propertiesfile

第一:在application.properties文件中定义以下设置

spring.thymeleaf.templateResolverOrder=1

Now customize your implementation.

现在自定义您的实现。

@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;
    }

Source: Several template locations for Thymeleaf in Spring Boot

来源:Spring Boot 中 Thymeleaf 的几个模板位置

回答by Jan Bodnar

Here is what I have figured out:

这是我想出的:

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

There should be no classpath:prefix when using ClassLoaderTemplateResolver.

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

The template directories must not be empty. The Java config takes precedence over the properties configuration. (Using order does not seem to affect this.)

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