Skip to content

Commit e34c2b0

Browse files
committed
Merge pull request #10869 from kazuki43zoo:support-EnableSpringELCompiler-on-thymeleaf
* pr/10869: Polish "Add 'enableSpringElCompiler' to ThymeleafProperties" Add 'enableSpringElCompiler' to ThymeleafProperties
2 parents 8136869 + a0374c0 commit e34c2b0

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-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
}
@@ -146,6 +150,7 @@ public ThymeleafDefaultConfiguration(
146150
@ConditionalOnMissingBean(SpringTemplateEngine.class)
147151
public SpringTemplateEngine templateEngine() {
148152
SpringTemplateEngine engine = new SpringTemplateEngine();
153+
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
149154
this.templateResolvers.forEach(engine::addTemplateResolver);
150155
this.dialects.forEach(engine::addDialect);
151156
return engine;
@@ -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: 16 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

@@ -139,6 +141,20 @@ public void overrideChunkedModeViewNames() throws Exception {
139141
.isEqualTo(new String[] { "foo", "bar" });
140142
}
141143

144+
@Test
145+
public void overrideEnableSpringElCompiler() {
146+
load(BaseConfiguration.class, "spring.thymeleaf.enable-spring-el-compiler:true");
147+
assertThat(this.context.getBean(SpringWebFluxTemplateEngine.class)
148+
.getEnableSpringELCompiler()).isTrue();
149+
}
150+
151+
@Test
152+
public void enableSpringElCompilerIsDisabledByDefault() {
153+
load(BaseConfiguration.class);
154+
assertThat(this.context.getBean(SpringWebFluxTemplateEngine.class)
155+
.getEnableSpringELCompiler()).isFalse();
156+
}
157+
142158
@Test
143159
public void templateLocationDoesNotExist() throws Exception {
144160
load(BaseConfiguration.class,

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

Lines changed: 16 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

@@ -110,6 +112,20 @@ public void overrideViewNames() throws Exception {
110112
assertThat(views.getViewNames()).isEqualTo(new String[] { "foo", "bar" });
111113
}
112114

115+
@Test
116+
public void overrideEnableSpringElCompiler() {
117+
load(BaseConfiguration.class, "spring.thymeleaf.enable-spring-el-compiler:true");
118+
assertThat(this.context.getBean(SpringTemplateEngine.class)
119+
.getEnableSpringELCompiler()).isTrue();
120+
}
121+
122+
@Test
123+
public void enableSpringElCompilerIsDisabledByDefault() {
124+
load(BaseConfiguration.class);
125+
assertThat(this.context.getBean(SpringTemplateEngine.class)
126+
.getEnableSpringELCompiler()).isFalse();
127+
}
128+
113129
@Test
114130
public void templateLocationDoesNotExist() throws Exception {
115131
load(BaseConfiguration.class,

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)