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
This commit is contained in:
Stephane Nicoll
2015-07-28 14:28:31 +02:00
parent 65c6492b6f
commit 96fc107ca1
4 changed files with 33 additions and 0 deletions

View File

@@ -101,6 +101,10 @@ public class ThymeleafAutoConfiguration {
resolver.setCharacterEncoding(this.properties.getEncoding().name());
}
resolver.setCacheable(this.properties.isCache());
Integer order = this.properties.getTemplateResolverOrder();
if (order != null) {
resolver.setOrder(order);
}
return resolver;
}

View File

@@ -73,6 +73,13 @@ public class ThymeleafProperties {
*/
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.
*/
@@ -152,6 +159,14 @@ public class ThymeleafProperties {
this.cache = cache;
}
public Integer getTemplateResolverOrder() {
return templateResolverOrder;
}
public void setTemplateResolverOrder(Integer templateResolverOrder) {
this.templateResolverOrder = templateResolverOrder;
}
public String[] getExcludedViewNames() {
return this.excludedViewNames;
}

View File

@@ -51,6 +51,7 @@ import static org.junit.Assert.assertTrue;
* Tests for {@link ThymeleafAutoConfiguration}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public class ThymeleafAutoConfigurationTests {
@@ -92,6 +93,18 @@ public class ThymeleafAutoConfigurationTests {
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
public void overrideViewNames() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,

View File

@@ -194,6 +194,7 @@ content into your application; rather pick only the properties that you need.
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
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])
spring.freemarker.allow-request-override=false