首页 文章

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

提问于
浏览
2

将Thymeleaf模板文件放在默认的 src/main/resources/templates 中对我来说没问题 . 当我想重命名目录时说 mytemplates ;这是行不通的 .

我收到无法找到模板位置:classpath:/ templates /(请添加一些模板或检查您的Thymeleaf配置)警告应用程序启动时 .

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

我使用以下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 回答

  • 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

  • 3

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

    以下是我的想法:

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

    使用 ClassLoaderTemplateResolver 时应该没有 classpath: 预加 .

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

相关问题