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

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

我做错了什么?

参考方案

尝试以下方法:

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

Java:“自动装配”继承与依赖注入 - java

Improve this question 我通常以常见的简单形式使用Spring框架: 控制器服务存储库通常,我会在CommonService类中放一个通用服务,并使所有其他服务扩展到类中。一个开发人员告诉我,最好在每个服务中插入CommonClass而不是使用继承。我的问题是,有一个方法比另一个更好吗? JVM或性能是否会受到另一个影响?更新资料Comm…

Spring中的应用程序上下文有什么作用? - java

我昨天问了一个问题(Using Spring in standalone apps),有关如何在独立应用程序中使用Spring。由此得知,您只创建一次应用程序上下文对象。因此,现在的问题是(即使在评论中得到了部分回答)创建应用程序上下文时会发生什么?当您说时,Spring是否会创建这些豆子并将它们连接在一起new ClassPathXmlApplicatio…

Spring Data Cassandra的事务管理 - java

我正在使用Spring和Cassandra作为基础数据库。曾提到过弹簧伞项目“ spring data cassandra”。与休眠不同,在这里无法找到如何管理事务。如果您中的某些人已经合并,请共享要包含的事务管理器的详细信息。 参考方案 Cassandra不支持传统(ACID)的事务。在某些特殊情况下,可以通过一些构造来实现事务原子性,例如原子批处理(请参…

注解中的Spring属性值 - java

如何获取注释内的属性值。例如我有一个注释@GetMyValue(value1="Val1",intVal=10) 现在,我希望“ Val1”和10来自属性文件。我试过了@GetMyValue(value1="${test.value}",intVal="${test.int.value}") 这不起…

Spring Boot:java.time.Duration的默认序列化从字符串更改为数字 - java

我们最近从Spring Boot 2.1.9升级到2.2.1,这导致我们的测试失败。调查导致结果,默认情况下java.time.Duration类型现在序列化为不同的序列。现在,我们将得到"PT15M",而不是在JSON消息中包含字符串"900.0"。 POJO定义如下所示@JsonProperty(required …