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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user