我正在尝试使用Thymeleaf和布局方言来设置html和文本模板处理:
@Configuration
public class ThymeleafConfiguration {
@Bean
public TemplateEngine emailTemplateEngine() {
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(textTemplateResolver());
templateEngine.addTemplateResolver(htmlTemplateResolver());
LayoutDialect dialect = new LayoutDialect();
templateEngine.addDialect(dialect);
return templateEngine;
}
private ITemplateResolver textTemplateResolver() {
final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setOrder(Integer.valueOf(1));
templateResolver.setResolvablePatterns(Collections.singleton("text/*"));
templateResolver.setPrefix("static/common/templates/email/");
templateResolver.setSuffix(".txt");
templateResolver.setTemplateMode(TemplateMode.TEXT);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(true);
return templateResolver;
}
private ITemplateResolver htmlTemplateResolver() {
...
}
}
HTML模板可以很好地与层次结构配合使用,但不能与文本模板配合使用
main.txt:
Title
[#th:block layout:fragment="content"]
content
[/th:block]
Footer
content.txt:
[#th:block layout:decorate="~{text/main}"]
[#th:block layout:fragment="content"]
Here is the URL for password change:
Some url...
[/th:block]
[/th:block]
结果只是内容部分,而没有标题和页脚部分:
Here is the URL for password change:
Some url...
该方言是否应该以文本模式工作?