Commit 96fc107c authored by Stephane Nicoll's avatar Stephane Nicoll

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 gh-3575
parent 65c6492b
...@@ -101,6 +101,10 @@ public class ThymeleafAutoConfiguration { ...@@ -101,6 +101,10 @@ public class ThymeleafAutoConfiguration {
resolver.setCharacterEncoding(this.properties.getEncoding().name()); resolver.setCharacterEncoding(this.properties.getEncoding().name());
} }
resolver.setCacheable(this.properties.isCache()); resolver.setCacheable(this.properties.isCache());
Integer order = this.properties.getTemplateResolverOrder();
if (order != null) {
resolver.setOrder(order);
}
return resolver; return resolver;
} }
......
...@@ -73,6 +73,13 @@ public class ThymeleafProperties { ...@@ -73,6 +73,13 @@ public class ThymeleafProperties {
*/ */
private boolean cache = true; private boolean cache = true;
/**
* Order of the template resolver in the chain. By default, the template resolver
* is first in the chain. Order start at 1 and should only be set if you have
* defined additional "TemplateResolver" beans.
*/
private Integer templateResolverOrder;
/** /**
* Comma-separated list of view names that can be resolved. * Comma-separated list of view names that can be resolved.
*/ */
...@@ -152,6 +159,14 @@ public class ThymeleafProperties { ...@@ -152,6 +159,14 @@ public class ThymeleafProperties {
this.cache = cache; this.cache = cache;
} }
public Integer getTemplateResolverOrder() {
return templateResolverOrder;
}
public void setTemplateResolverOrder(Integer templateResolverOrder) {
this.templateResolverOrder = templateResolverOrder;
}
public String[] getExcludedViewNames() { public String[] getExcludedViewNames() {
return this.excludedViewNames; return this.excludedViewNames;
} }
......
...@@ -51,6 +51,7 @@ import static org.junit.Assert.assertTrue; ...@@ -51,6 +51,7 @@ import static org.junit.Assert.assertTrue;
* Tests for {@link ThymeleafAutoConfiguration}. * Tests for {@link ThymeleafAutoConfiguration}.
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll
*/ */
public class ThymeleafAutoConfigurationTests { public class ThymeleafAutoConfigurationTests {
...@@ -92,6 +93,18 @@ public class ThymeleafAutoConfigurationTests { ...@@ -92,6 +93,18 @@ public class ThymeleafAutoConfigurationTests {
assertEquals("text/html;charset=UTF-16", views.getContentType()); assertEquals("text/html;charset=UTF-16", views.getContentType());
} }
@Test
public void overrideTemplateResolverOrder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"spring.thymeleaf.templateResolverOrder:25");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
this.context.getBean(TemplateEngine.class).initialize();
ITemplateResolver resolver = this.context.getBean(ITemplateResolver.class);
assertEquals(Integer.valueOf(25), resolver.getOrder());
}
@Test @Test
public void overrideViewNames() throws Exception { public void overrideViewNames() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
......
...@@ -194,6 +194,7 @@ content into your application; rather pick only the properties that you need. ...@@ -194,6 +194,7 @@ content into your application; rather pick only the properties that you need.
spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache=true # set to false for hot refresh spring.thymeleaf.cache=true # set to false for hot refresh
spring.thymeleaf.template-resolver-order= # order of the template resolver in the chain
# FREEMARKER ({sc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}[FreeMarkerAutoConfiguration]) # FREEMARKER ({sc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}[FreeMarkerAutoConfiguration])
spring.freemarker.allow-request-override=false spring.freemarker.allow-request-override=false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment