问题描述
我使用 Thymeleaf 作为我的电子邮件模板,我在谷歌上搜索并能够使用以下配置成功运行代码:-
I am using Thymeleaf for my email templates and I was googling and was able to run the code successfully with the following config:-
@Configuration
public class TemplateEngineConfig {
@Autowired
private MailConfigProps mailConfigProps;
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(templateResolver());
return templateEngine;
}
private TemplateResolver templateResolver() {
TemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix(mailConfigProps.getTemplatePath());
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
resolver.setCacheable(true);
return resolver;
}
}
除了 setOrder 之外,这里的所有内容对我来说都是可以理解的.我尝试在不同的地方查找它,但我得到的唯一信息是为链中的模板引擎设置新顺序.顺序应以 1 开头.".
Everything here is understandable to me except setOrder. I tried looking up for it in different places and the only info that I get is that " Sets a new order for the template engine in the chain. The order should start with 1.".
以下是文档内容:
setOrder
public void setOrder(Integer order)
Sets a new order for the template engine in the chain. Order should start with 1.
Parameters:
order - the new order.
Can someone please share why `setOrder` is there and when should it be used in my application and what should be the appropriate values to be set in different scenarios.
如果我不提供这个值会怎样?
What happens if I don't provide this value?
推荐答案
您的应用程序可能使用不同类型的模板,例如 HTML、TXT 或 String
等.这些模板可能有多种类型的模板解析器.那些多模板解析器可以注册到模板引擎
.应用程序中的所有视图解析器在有序链中执行,直到其中一个能够解析该视图.因此,TemplateResolver#setOrder
基本上决定了链的顺序.有一个 resolvablePatterns
属性,用于确定模板解析器是否会考虑由它解析的视图名称.您还可以通过 setResolvablePatterns
Your application may use different types of Templates like HTML, TXT or String
etc.Those may have multiple types of template resolvers. Those multiple template resolver can be registered with the template engine
. All the view resolvers in the application executing in the ordered chain until one of them are able to resolve that view.So, TemplateResolver#setOrder
basically decide the order of the chain. There is a resolvablePatterns
property which is the one to determine whether a template resolver will consider a view name to be resolved by it or not. You can also set the resolvablePatterns
by setResolvablePatterns
templateResolver.setResolvablePatterns(Collections.singleton("text/*"));
这篇关于thymleaf 中的 TemplateResolver.setOrder 用户是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!