Merge pull request #10869 from kazuki43zoo:support-EnableSpringELCompiler-on-thymeleaf

* pr/10869:
  Polish "Add 'enableSpringElCompiler' to ThymeleafProperties"
  Add 'enableSpringElCompiler' to ThymeleafProperties
pull/10969/merge
Stephane Nicoll 7 years ago
commit e34c2b0c01

@ -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);
}
@ -146,6 +150,7 @@ public class ThymeleafAutoConfiguration {
@ConditionalOnMissingBean(SpringTemplateEngine.class)
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(this.properties.isEnableSpringElCompiler());
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
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;
}

@ -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;
}

@ -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 {
@ -139,6 +141,20 @@ public class ThymeleafReactiveAutoConfigurationTests {
.isEqualTo(new String[] { "foo", "bar" });
}
@Test
public void overrideEnableSpringElCompiler() {
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();
}
@Test
public void templateLocationDoesNotExist() throws Exception {
load(BaseConfiguration.class,

@ -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 {
@ -110,6 +112,20 @@ public class ThymeleafServletAutoConfigurationTests {
assertThat(views.getViewNames()).isEqualTo(new String[] { "foo", "bar" });
}
@Test
public void overrideEnableSpringElCompiler() {
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();
}
@Test
public void templateLocationDoesNotExist() throws Exception {
load(BaseConfiguration.class,

@ -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.

Loading…
Cancel
Save