Skip to content

Commit a6a041a

Browse files
committed
Add the enableSpringElCompiler on ThymeleafProperties
1 parent 4757ad6 commit a6a041a

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* @author Brian Clozel
7070
* @author Eddú Meléndez
7171
* @author Daniel Fernández
72+
* @author Kazuki Shimizu
7273
*/
7374
@Configuration
7475
@EnableConfigurationProperties(ThymeleafProperties.class)
@@ -131,13 +132,16 @@ public SpringResourceTemplateResolver defaultTemplateResolver() {
131132
@Configuration
132133
protected static class ThymeleafDefaultConfiguration {
133134

135+
private final ThymeleafProperties properties;
136+
134137
private final Collection<ITemplateResolver> templateResolvers;
135138

136139
private final Collection<IDialect> dialects;
137140

138-
public ThymeleafDefaultConfiguration(
141+
public ThymeleafDefaultConfiguration(ThymeleafProperties properties,
139142
Collection<ITemplateResolver> templateResolvers,
140143
ObjectProvider<Collection<IDialect>> dialectsProvider) {
144+
this.properties = properties;
141145
this.templateResolvers = templateResolvers;
142146
this.dialects = dialectsProvider.getIfAvailable(Collections::emptyList);
143147
}
@@ -148,6 +152,7 @@ public SpringTemplateEngine templateEngine() {
148152
SpringTemplateEngine engine = new SpringTemplateEngine();
149153
this.templateResolvers.forEach(engine::addTemplateResolver);
150154
this.dialects.forEach(engine::addDialect);
155+
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
151156
return engine;
152157
}
153158

@@ -215,12 +220,16 @@ private String appendCharset(MimeType type, String charset) {
215220
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
216221
static class ThymeleafReactiveConfiguration {
217222

223+
private final ThymeleafProperties properties;
224+
218225
private final Collection<ITemplateResolver> templateResolvers;
219226

220227
private final Collection<IDialect> dialects;
221228

222-
ThymeleafReactiveConfiguration(Collection<ITemplateResolver> templateResolvers,
229+
ThymeleafReactiveConfiguration(ThymeleafProperties properties,
230+
Collection<ITemplateResolver> templateResolvers,
223231
ObjectProvider<Collection<IDialect>> dialectsProvider) {
232+
this.properties = properties;
224233
this.templateResolvers = templateResolvers;
225234
this.dialects = dialectsProvider.getIfAvailable(Collections::emptyList);
226235
}
@@ -231,6 +240,7 @@ public SpringWebFluxTemplateEngine templateEngine() {
231240
SpringWebFluxTemplateEngine engine = new SpringWebFluxTemplateEngine();
232241
this.templateResolvers.forEach(engine::addTemplateResolver);
233242
this.dialects.forEach(engine::addDialect);
243+
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
234244
return engine;
235245
}
236246

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @author Stephane Nicoll
3030
* @author Brian Clozel
3131
* @author Daniel Fernández
32+
* @author Kazuki Shimizu
3233
* @since 1.2.0
3334
*/
3435
@ConfigurationProperties(prefix = "spring.thymeleaf")
@@ -94,6 +95,11 @@ public class ThymeleafProperties {
9495
*/
9596
private String[] excludedViewNames;
9697

98+
/**
99+
* Enable the SpringEL compiler in SpringEL expressions.
100+
*/
101+
private boolean enableSpringElCompiler;
102+
97103
/**
98104
* Enable Thymeleaf view resolution for Web frameworks.
99105
*/
@@ -191,6 +197,14 @@ public void setViewNames(String[] viewNames) {
191197
this.viewNames = viewNames;
192198
}
193199

200+
public boolean isEnableSpringElCompiler() {
201+
return this.enableSpringElCompiler;
202+
}
203+
204+
public void setEnableSpringElCompiler(boolean enableSpringElCompiler) {
205+
this.enableSpringElCompiler = enableSpringElCompiler;
206+
}
207+
194208
public Reactive getReactive() {
195209
return this.reactive;
196210
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.thymeleaf.TemplateEngine;
2929
import org.thymeleaf.context.Context;
3030
import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine;
31+
import org.thymeleaf.spring5.SpringWebFluxTemplateEngine;
3132
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
3233
import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver;
3334
import org.thymeleaf.templateresolver.ITemplateResolver;
@@ -51,6 +52,7 @@
5152
* Tests for {@link ThymeleafAutoConfiguration} in Reactive applications.
5253
*
5354
* @author Brian Clozel
55+
* @author Kazuki Shimizu
5456
*/
5557
public class ThymeleafReactiveAutoConfigurationTests {
5658

@@ -192,6 +194,18 @@ public void layoutDialectCanBeCustomized() throws Exception {
192194
.isInstanceOf(GroupingStrategy.class);
193195
}
194196

197+
@Test
198+
public void enableSpringELCompilerCanBeEnabled() {
199+
load(BaseConfiguration.class, "spring.thymeleaf.enableSpringELCompiler:true");
200+
assertThat(this.context.getBean(SpringWebFluxTemplateEngine.class).getEnableSpringELCompiler()).isTrue();
201+
}
202+
203+
@Test
204+
public void enableSpringELCompilerIsDisabledByDefault() {
205+
load(BaseConfiguration.class);
206+
assertThat(this.context.getBean(SpringWebFluxTemplateEngine.class).getEnableSpringELCompiler()).isFalse();
207+
}
208+
195209
private void load(Class<?> config, String... envVariables) {
196210
this.context = new GenericReactiveWebApplicationContext();
197211
TestPropertyValues.of(envVariables).applyTo(this.context);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.Test;
2828
import org.thymeleaf.TemplateEngine;
2929
import org.thymeleaf.context.Context;
30+
import org.thymeleaf.spring5.SpringTemplateEngine;
3031
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
3132
import org.thymeleaf.spring5.view.ThymeleafView;
3233
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
@@ -59,6 +60,7 @@
5960
* @author Stephane Nicoll
6061
* @author Eddú Meléndez
6162
* @author Brian Clozel
63+
* @author Kazuki Shimizu
6264
*/
6365
public class ThymeleafServletAutoConfigurationTests {
6466

@@ -217,6 +219,18 @@ public void cachingCanBeDisabled() {
217219
assertThat(templateResolver.isCacheable()).isFalse();
218220
}
219221

222+
@Test
223+
public void enableSpringELCompilerCanBeEnabled() {
224+
load(BaseConfiguration.class, "spring.thymeleaf.enableSpringELCompiler:true");
225+
assertThat(this.context.getBean(SpringTemplateEngine.class).getEnableSpringELCompiler()).isTrue();
226+
}
227+
228+
@Test
229+
public void enableSpringELCompilerIsDisabledByDefault() {
230+
load(BaseConfiguration.class);
231+
assertThat(this.context.getBean(SpringTemplateEngine.class).getEnableSpringELCompiler()).isFalse();
232+
}
233+
220234
private void load(Class<?> config, String... envVariables) {
221235
this.context = new AnnotationConfigWebApplicationContext();
222236
TestPropertyValues.of(envVariables).applyTo(this.context);

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ content into your application; rather pick only the properties that you need.
456456
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
457457
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
458458
spring.thymeleaf.enabled=true # Enable Thymeleaf view resolution for Web frameworks.
459+
spring.thymeleaf.enable-spring-el-compiler=false # Enable the SpringEL compiler in SpringEL expressions.
459460
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
460461
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
461462
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.

0 commit comments

Comments
 (0)