Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向Spring Boot添加自定义Thymeleaf模板解析器_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

向Spring Boot添加自定义Thymeleaf模板解析器

向Spring Boot添加自定义Thymeleaf模板解析器,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,默认情况下,Spring引导应用程序在classpath://templates 我们如何添加一个以上的解析器,例如,我们需要从本地目录中搜索模板,如“c:\MyTemplates” 您可以通过调用该方法或使用FileTemplateResolver调用该方法在上添加更多模板解析器 @Configuration public class ThymeleafExtension { @Autowired private SpringTemplateEngine templateEn

默认情况下,Spring引导应用程序在classpath://templates


我们如何添加一个以上的解析器,例如,我们需要从本地目录中搜索模板,如“c:\MyTemplates”

您可以通过调用该方法或使用
FileTemplateResolver
调用该方法在上添加更多模板解析器

@Configuration
public class ThymeleafExtension {

    @Autowired
    private SpringTemplateEngine templateEngine;

    @PostConstruct
    public void extension() {
        FileTemplateResolver resolver = new FileTemplateResolver();
        resolver.setPrefix("D:\\templates\\");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(templateEngine.getTemplateResolvers().size());
        resolver.setCacheable(false);
        templateEngine.addTemplateResolver(resolver);
    }
}

对前面的回答有一点改进:

@Configuration
public class ThymeleafConfig {

    public ThymeleafConfig(TemplateEngine templateEngine) {
        templateEngine.addTemplateResolver(new StringTemplateResolver());
    }
}

模板存储库实现似乎不尊重Order属性:在第203行,Order属性没有排序:-本文帮助了我。我不知道Spring boot只在templates文件夹中搜索thymeleaf模板。我把我的文件放在静态文件夹中,但无法解析+1.