Skip to content

Commit cf350cf

Browse files
kazuki43zoosnicoll
authored andcommitted
Add 'enableSpringElCompiler' to ThymeleafProperties
See gh-10869
1 parent 8136869 commit cf350cf

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
@@ -30,6 +30,7 @@
3030
* @author Stephane Nicoll
3131
* @author Brian Clozel
3232
* @author Daniel Fernández
33+
* @author Kazuki Shimizu
3334
* @since 1.2.0
3435
*/
3536
@ConfigurationProperties(prefix = "spring.thymeleaf")
@@ -95,6 +96,11 @@ public class ThymeleafProperties {
9596
*/
9697
private String[] excludedViewNames;
9798

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

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

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.enable-spring-el-compiler: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 AnnotationConfigReactiveWebApplicationContext();
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.enable-spring-el-compiler: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
@@ -453,6 +453,7 @@ content into your application; rather pick only the properties that you need.
453453
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
454454
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
455455
spring.thymeleaf.enabled=true # Enable Thymeleaf view resolution for Web frameworks.
456+
spring.thymeleaf.enable-spring-el-compiler=false # Enable the SpringEL compiler in SpringEL expressions.
456457
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
457458
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
458459
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.

0 commit comments

Comments
 (0)