Add 'enableSpringElCompiler' to ThymeleafProperties

See gh-10869
This commit is contained in:
Kazuki Shimizu 2017-11-02 22:12:43 +09:00 committed by Stephane Nicoll
parent 8136869f67
commit cf350cf85b
5 changed files with 55 additions and 2 deletions

View File

@ -69,6 +69,7 @@ import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
* @author Brian Clozel
* @author Eddú Meléndez
* @author Daniel Fernández
* @author Kazuki Shimizu
*/
@Configuration
@EnableConfigurationProperties(ThymeleafProperties.class)
@ -131,13 +132,16 @@ public class ThymeleafAutoConfiguration {
@Configuration
protected static class ThymeleafDefaultConfiguration {
private final ThymeleafProperties properties;
private final Collection<ITemplateResolver> templateResolvers;
private final Collection<IDialect> dialects;
public ThymeleafDefaultConfiguration(
public ThymeleafDefaultConfiguration(ThymeleafProperties properties,
Collection<ITemplateResolver> templateResolvers,
ObjectProvider<Collection<IDialect>> dialectsProvider) {
this.properties = properties;
this.templateResolvers = templateResolvers;
this.dialects = dialectsProvider.getIfAvailable(Collections::emptyList);
}
@ -148,6 +152,7 @@ public class ThymeleafAutoConfiguration {
SpringTemplateEngine engine = new SpringTemplateEngine();
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
return engine;
}
@ -215,12 +220,16 @@ public class ThymeleafAutoConfiguration {
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
static class ThymeleafReactiveConfiguration {
private final ThymeleafProperties properties;
private final Collection<ITemplateResolver> templateResolvers;
private final Collection<IDialect> dialects;
ThymeleafReactiveConfiguration(Collection<ITemplateResolver> templateResolvers,
ThymeleafReactiveConfiguration(ThymeleafProperties properties,
Collection<ITemplateResolver> templateResolvers,
ObjectProvider<Collection<IDialect>> dialectsProvider) {
this.properties = properties;
this.templateResolvers = templateResolvers;
this.dialects = dialectsProvider.getIfAvailable(Collections::emptyList);
}
@ -231,6 +240,7 @@ public class ThymeleafAutoConfiguration {
SpringWebFluxTemplateEngine engine = new SpringWebFluxTemplateEngine();
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
return engine;
}

View File

@ -30,6 +30,7 @@ import org.springframework.util.MimeType;
* @author Stephane Nicoll
* @author Brian Clozel
* @author Daniel Fernández
* @author Kazuki Shimizu
* @since 1.2.0
*/
@ConfigurationProperties(prefix = "spring.thymeleaf")
@ -95,6 +96,11 @@ public class ThymeleafProperties {
*/
private String[] excludedViewNames;
/**
* Enable the SpringEL compiler in SpringEL expressions.
*/
private boolean enableSpringElCompiler;
/**
* Enable Thymeleaf view resolution for Web frameworks.
*/
@ -192,6 +198,14 @@ public class ThymeleafProperties {
this.viewNames = viewNames;
}
public boolean isEnableSpringElCompiler() {
return this.enableSpringElCompiler;
}
public void setEnableSpringElCompiler(boolean enableSpringElCompiler) {
this.enableSpringElCompiler = enableSpringElCompiler;
}
public Reactive getReactive() {
return this.reactive;
}

View File

@ -28,6 +28,7 @@ import org.junit.Test;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine;
import org.thymeleaf.spring5.SpringWebFluxTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
@ -51,6 +52,7 @@ import static org.hamcrest.Matchers.not;
* Tests for {@link ThymeleafAutoConfiguration} in Reactive applications.
*
* @author Brian Clozel
* @author Kazuki Shimizu
*/
public class ThymeleafReactiveAutoConfigurationTests {
@ -192,6 +194,18 @@ public class ThymeleafReactiveAutoConfigurationTests {
.isInstanceOf(GroupingStrategy.class);
}
@Test
public void enableSpringElCompilerCanBeEnabled() {
load(BaseConfiguration.class, "spring.thymeleaf.enable-spring-el-compiler:true");
assertThat(this.context.getBean(SpringWebFluxTemplateEngine.class).getEnableSpringELCompiler()).isTrue();
}
@Test
public void enableSpringElCompilerIsDisabledByDefault() {
load(BaseConfiguration.class);
assertThat(this.context.getBean(SpringWebFluxTemplateEngine.class).getEnableSpringELCompiler()).isFalse();
}
private void load(Class<?> config, String... envVariables) {
this.context = new AnnotationConfigReactiveWebApplicationContext();
TestPropertyValues.of(envVariables).applyTo(this.context);

View File

@ -27,6 +27,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafView;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
@ -59,6 +60,7 @@ import static org.hamcrest.Matchers.containsString;
* @author Stephane Nicoll
* @author Eddú Meléndez
* @author Brian Clozel
* @author Kazuki Shimizu
*/
public class ThymeleafServletAutoConfigurationTests {
@ -217,6 +219,18 @@ public class ThymeleafServletAutoConfigurationTests {
assertThat(templateResolver.isCacheable()).isFalse();
}
@Test
public void enableSpringElCompilerCanBeEnabled() {
load(BaseConfiguration.class, "spring.thymeleaf.enable-spring-el-compiler:true");
assertThat(this.context.getBean(SpringTemplateEngine.class).getEnableSpringELCompiler()).isTrue();
}
@Test
public void enableSpringElCompilerIsDisabledByDefault() {
load(BaseConfiguration.class);
assertThat(this.context.getBean(SpringTemplateEngine.class).getEnableSpringELCompiler()).isFalse();
}
private void load(Class<?> config, String... envVariables) {
this.context = new AnnotationConfigWebApplicationContext();
TestPropertyValues.of(envVariables).applyTo(this.context);

View File

@ -453,6 +453,7 @@ content into your application; rather pick only the properties that you need.
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.enabled=true # Enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.enable-spring-el-compiler=false # Enable the SpringEL compiler in SpringEL expressions.
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.