Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @author Brian Clozel
* @author Eddú Meléndez
* @author Daniel Fernández
* @author Kazuki Shimizu
*/
@Configuration
@EnableConfigurationProperties(ThymeleafProperties.class)
Expand Down Expand Up @@ -131,13 +132,16 @@ public SpringResourceTemplateResolver defaultTemplateResolver() {
@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);
}
Expand All @@ -148,6 +152,7 @@ public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
return engine;
}

Expand Down Expand Up @@ -215,12 +220,16 @@ private String appendCharset(MimeType type, String charset) {
@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);
}
Expand All @@ -231,6 +240,7 @@ public SpringWebFluxTemplateEngine templateEngine() {
SpringWebFluxTemplateEngine engine = new SpringWebFluxTemplateEngine();
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
return engine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @author Stephane Nicoll
* @author Brian Clozel
* @author Daniel Fernández
* @author Kazuki Shimizu
* @since 1.2.0
*/
@ConfigurationProperties(prefix = "spring.thymeleaf")
Expand Down Expand Up @@ -94,6 +95,11 @@ public class ThymeleafProperties {
*/
private String[] excludedViewNames;

/**
* Enable the SpringEL compiler in SpringEL expressions.
*/
private boolean enableSpringElCompiler;

/**
* Enable Thymeleaf view resolution for Web frameworks.
*/
Expand Down Expand Up @@ -191,6 +197,14 @@ public void setViewNames(String[] viewNames) {
this.viewNames = viewNames;
}

public boolean isEnableSpringElCompiler() {
return this.enableSpringElCompiler;
}

public void setEnableSpringElCompiler(boolean enableSpringElCompiler) {
this.enableSpringElCompiler = enableSpringElCompiler;
}

public Reactive getReactive() {
return this.reactive;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand All @@ -51,6 +52,7 @@
* Tests for {@link ThymeleafAutoConfiguration} in Reactive applications.
*
* @author Brian Clozel
* @author Kazuki Shimizu
*/
public class ThymeleafReactiveAutoConfigurationTests {

Expand Down Expand Up @@ -192,6 +194,18 @@ public void layoutDialectCanBeCustomized() throws Exception {
.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 GenericReactiveWebApplicationContext();
TestPropertyValues.of(envVariables).applyTo(this.context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -59,6 +60,7 @@
* @author Stephane Nicoll
* @author Eddú Meléndez
* @author Brian Clozel
* @author Kazuki Shimizu
*/
public class ThymeleafServletAutoConfigurationTests {

Expand Down Expand Up @@ -217,6 +219,18 @@ public void cachingCanBeDisabled() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,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.
Expand Down