Skip to content

Commit 96fc107

Browse files
committed
Customize Thymeleaf default template resolver order
Currently, the default TemplateResolver had no specific order. Thymeleaf handles that with a "always first" strategy (that can be confusing if several TemplateResolver have a "null" order. While it is a fine default (and changing it could lead to weird side effects), it has to be changed as soon as another TemplateResolver bean is defined in the project. The `spring.thymeleaf.template-resolver-order` property has been added to control the order of the default TemplateResolver. Closes spring-projectsgh-3575
1 parent 65c6492 commit 96fc107

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public TemplateResolver defaultTemplateResolver() {
101101
resolver.setCharacterEncoding(this.properties.getEncoding().name());
102102
}
103103
resolver.setCacheable(this.properties.isCache());
104+
Integer order = this.properties.getTemplateResolverOrder();
105+
if (order != null) {
106+
resolver.setOrder(order);
107+
}
104108
return resolver;
105109
}
106110

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ public class ThymeleafProperties {
7373
*/
7474
private boolean cache = true;
7575

76+
/**
77+
* Order of the template resolver in the chain. By default, the template resolver
78+
* is first in the chain. Order start at 1 and should only be set if you have
79+
* defined additional "TemplateResolver" beans.
80+
*/
81+
private Integer templateResolverOrder;
82+
7683
/**
7784
* Comma-separated list of view names that can be resolved.
7885
*/
@@ -152,6 +159,14 @@ public void setCache(boolean cache) {
152159
this.cache = cache;
153160
}
154161

162+
public Integer getTemplateResolverOrder() {
163+
return templateResolverOrder;
164+
}
165+
166+
public void setTemplateResolverOrder(Integer templateResolverOrder) {
167+
this.templateResolverOrder = templateResolverOrder;
168+
}
169+
155170
public String[] getExcludedViewNames() {
156171
return this.excludedViewNames;
157172
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* Tests for {@link ThymeleafAutoConfiguration}.
5252
*
5353
* @author Dave Syer
54+
* @author Stephane Nicoll
5455
*/
5556
public class ThymeleafAutoConfigurationTests {
5657

@@ -92,6 +93,18 @@ public void overrideCharacterEncoding() throws Exception {
9293
assertEquals("text/html;charset=UTF-16", views.getContentType());
9394
}
9495

96+
@Test
97+
public void overrideTemplateResolverOrder() throws Exception {
98+
EnvironmentTestUtils.addEnvironment(this.context,
99+
"spring.thymeleaf.templateResolverOrder:25");
100+
this.context.register(ThymeleafAutoConfiguration.class,
101+
PropertyPlaceholderAutoConfiguration.class);
102+
this.context.refresh();
103+
this.context.getBean(TemplateEngine.class).initialize();
104+
ITemplateResolver resolver = this.context.getBean(ITemplateResolver.class);
105+
assertEquals(Integer.valueOf(25), resolver.getOrder());
106+
}
107+
95108
@Test
96109
public void overrideViewNames() throws Exception {
97110
EnvironmentTestUtils.addEnvironment(this.context,

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ content into your application; rather pick only the properties that you need.
194194
spring.thymeleaf.encoding=UTF-8
195195
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
196196
spring.thymeleaf.cache=true # set to false for hot refresh
197+
spring.thymeleaf.template-resolver-order= # order of the template resolver in the chain
197198
198199
# FREEMARKER ({sc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}[FreeMarkerAutoConfiguration])
199200
spring.freemarker.allow-request-override=false

0 commit comments

Comments
 (0)