From 274734e787b50681f27c2b227a69e28b83e909a5 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 9 Sep 2016 16:26:37 +0200 Subject: [PATCH] Add `spring.thymeleaf.check-template` property This commit adds a new `spring.thymeleaf.check-template` property which is only used for Thymeleaf 3.0+. Since thymeleaf/thymeleaf#419, the Thymeleaf template resolver implementations can implement the `setCheckExistence` method - this enables the template existence verification at **resolution** time, which means the resolver can return null as a `TemplateResolution` and let other template resolvers in the chain try. This new property is set to `true` by default and can be disabled if the application only has a single resolver and the template existence check is considered as a performance penalty with the configured resolver. Fixes gh-6500 --- .../AbstractTemplateResolverConfiguration.java | 2 +- .../thymeleaf/ThymeleafAutoConfiguration.java | 11 +++++++++++ .../thymeleaf/ThymeleafProperties.java | 15 ++++++++++++++- .../asciidoc/appendix-application-properties.adoc | 1 + 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/AbstractTemplateResolverConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/AbstractTemplateResolverConfiguration.java index 44d1deef7e..91c40a1156 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/AbstractTemplateResolverConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/AbstractTemplateResolverConfiguration.java @@ -37,7 +37,7 @@ abstract class AbstractTemplateResolverConfiguration { private static final Log logger = LogFactory .getLog(AbstractTemplateResolverConfiguration.class); - private final ThymeleafProperties properties; + protected final ThymeleafProperties properties; private final ApplicationContext applicationContext; 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 ef62f3e939..a7cdd995a5 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 @@ -29,6 +29,7 @@ import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.resourceresolver.SpringResourceResourceResolver; +import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ITemplateResolver; @@ -132,6 +133,16 @@ public class ThymeleafAutoConfiguration { super(properties, applicationContext); } + @Bean + @Override + public SpringResourceTemplateResolver defaultTemplateResolver() { + SpringResourceTemplateResolver resolver = super.defaultTemplateResolver(); + Method setCheckExistence = ReflectionUtils + .findMethod(resolver.getClass(), "setCheckExistence", boolean.class); + ReflectionUtils.invokeMethod(setCheckExistence, resolver, this.properties.isCheckTemplate()); + return resolver; + } + } @Configuration 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 3a58ef56d1..eb8e3687cb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,11 @@ public class ThymeleafProperties { public static final String DEFAULT_SUFFIX = ".html"; + /** + * Check that the template exists before rendering it (Thymeleaf 3+). + */ + private boolean checkTemplate = true; + /** * Check that the templates location exists. */ @@ -103,6 +108,14 @@ public class ThymeleafProperties { this.enabled = enabled; } + public boolean isCheckTemplate() { + return checkTemplate; + } + + public void setCheckTemplate(boolean checkTemplate) { + this.checkTemplate = checkTemplate; + } + public boolean isCheckTemplateLocation() { return this.checkTemplateLocation; } 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 67c67cc8f5..1045c577c7 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -394,6 +394,7 @@ content into your application; rather pick only the properties that you need. # THYMELEAF ({sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[ThymeleafAutoConfiguration]) spring.thymeleaf.cache=true # Enable template caching. + spring.thymeleaf.check-template=true # Check that the template exists before rendering it. spring.thymeleaf.check-template-location=true # Check that the templates location exists. spring.thymeleaf.content-type=text/html # Content-Type value. spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.