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

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

本文介绍了Spring Boot无法使用Java配置更改Thymeleaf模板目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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 Resolvers error.

我使用以下Java配置:

I use the following Java configuration:

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

我做错了什么?

推荐答案

尝试以下操作:

1st:在application.properties文件中定义以下设置

1st : define below setting in application.properties file

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

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

这篇关于Spring Boot无法使用Java配置更改Thymeleaf模板目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:15