Spring Boot 中 Thymeleaf 的几个模板位置 [英] Several template locations for Thymeleaf in Spring Boot
问题描述
当前 Spring Boot
允许使用 spring.thymeleaf.prefix
属性为 Thymeleaf
模板位置设置一个值.
Currently Spring Boot
allow one value for the Thymeleaf
templates location with the spring.thymeleaf.prefix
property.
默认值为 classpath:/templates/
.
我想要 thymeleaf 模板的另一个位置(但保留默认的),例如:
I want to have another location for the thymeleaf templates (but keep the default one), outside the jar, for example:
spring.thymeleaf.prefix = classpath:/templates/, file:/resources/templates
我是否必须为我想要的新位置定义另一个模板解析器?
Do i have to define another template resolver for the new location i want ?
推荐答案
在 application.properties
文件中定义设置
spring.thymeleaf.templateResolverOrder=1
现在在创建 ITemplateResolver 的自定义 Bean 中,将 order 以及前缀和后缀设置为 0.这样spring boot会同时监听两个地方
Now in your custom Bean which creates ITemplateResolver set order to 0 along with prefix and suffix. This way spring boot will listen to both places
将 order 设置为 0 很重要
Setting order to 0 is important
bean 创建的一个例子可以是
An example of bean creation can be
@Bean
public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("mails/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode(TemplateMode.HTML);
emailTemplateResolver.setCharacterEncoding("UTF-8");
emailTemplateResolver.setOrder(0);
emailTemplateResolver.setCheckExistence(true);
return emailTemplateResolver;
}
这篇关于Spring Boot 中 Thymeleaf 的几个模板位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!