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
This commit is contained in:
parent
6687eb6f35
commit
274734e787
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue