From 96fc107ca1b5565a669eac04b77e83bdbbf5a7c7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 28 Jul 2015 14:28:31 +0200 Subject: [PATCH] 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 --- .../thymeleaf/ThymeleafAutoConfiguration.java | 4 ++++ .../thymeleaf/ThymeleafProperties.java | 15 +++++++++++++++ .../ThymeleafAutoConfigurationTests.java | 13 +++++++++++++ .../asciidoc/appendix-application-properties.adoc | 1 + 4 files changed, 33 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index fc43c22732..3c7f4080c9 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java index 13b63ca42b..2e2fdcff7d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java index dc2c114eef..761680083f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java @@ -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, diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index dc693fabc0..88f7cff8ae 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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= 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