Skip to content

Commit 304920d

Browse files
Dave Syerphilwebb
authored andcommitted
Make Thymeleaf @ConditionalOnWebApplication
If user creates a Thymeleaf app with a parent-child context then the child should contain all the web-specific pieces (and they are likely to fail fast if they need to be ServletContextAware, or slower if they try to locate a WebApplicationContext at runtime). This can't happen if the view resolver is being added to the parent. Freemarker and Velocity already have similar tests because it is assumed that they should be usable outside a web app, so this change just does the same for Thymeleaf. Fixes gh-1611
1 parent 68ff7d4 commit 304920d

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3233
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3334
import org.springframework.boot.bind.RelaxedPropertyResolver;
3435
import org.springframework.context.EnvironmentAware;
@@ -149,19 +150,20 @@ public LayoutDialect layoutDialect() {
149150

150151
@Configuration
151152
@ConditionalOnClass({ Servlet.class })
153+
@ConditionalOnWebApplication
152154
protected static class ThymeleafViewResolverConfiguration implements EnvironmentAware {
153155

154156
private RelaxedPropertyResolver environment;
155157

158+
@Autowired
159+
private SpringTemplateEngine templateEngine;
160+
156161
@Override
157162
public void setEnvironment(Environment environment) {
158163
this.environment = new RelaxedPropertyResolver(environment,
159164
"spring.thymeleaf.");
160165
}
161166

162-
@Autowired
163-
private SpringTemplateEngine templateEngine;
164-
165167
@Bean
166168
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
167169
public ThymeleafViewResolver thymeleafViewResolver() {

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.mock.web.MockHttpServletResponse;
3131
import org.springframework.mock.web.MockServletContext;
3232
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
33+
import org.springframework.web.servlet.ViewResolver;
3334
import org.springframework.web.servlet.support.RequestContext;
3435
import org.thymeleaf.TemplateEngine;
3536
import org.thymeleaf.context.Context;
@@ -38,8 +39,10 @@
3839
import org.thymeleaf.templateresolver.ITemplateResolver;
3940
import org.thymeleaf.templateresolver.TemplateResolver;
4041

42+
import static org.hamcrest.Matchers.containsString;
4143
import static org.junit.Assert.assertArrayEquals;
4244
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertThat;
4346
import static org.junit.Assert.assertTrue;
4447

4548
/**
@@ -49,7 +52,7 @@
4952
*/
5053
public class ThymeleafAutoConfigurationTests {
5154

52-
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
55+
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
5356

5457
@After
5558
public void close() {
@@ -137,4 +140,44 @@ public void createLayoutFromConfigClass() throws Exception {
137140
context.close();
138141
}
139142

143+
@Test
144+
public void useDataDialect() throws Exception {
145+
this.context.register(ThymeleafAutoConfiguration.class,
146+
PropertyPlaceholderAutoConfiguration.class);
147+
this.context.refresh();
148+
TemplateEngine engine = this.context.getBean(TemplateEngine.class);
149+
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
150+
String result = engine.process("data-dialect", attrs);
151+
assertEquals("<html><body data-foo=\"bar\"></body></html>", result);
152+
}
153+
154+
@Test
155+
public void renderTemplate() throws Exception {
156+
this.context.register(ThymeleafAutoConfiguration.class,
157+
PropertyPlaceholderAutoConfiguration.class);
158+
this.context.refresh();
159+
TemplateEngine engine = this.context.getBean(TemplateEngine.class);
160+
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
161+
String result = engine.process("home", attrs);
162+
assertEquals("<html><body>bar</body></html>", result);
163+
}
164+
165+
@Test
166+
public void renderNonWebAppTemplate() throws Exception {
167+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
168+
ThymeleafAutoConfiguration.class,
169+
PropertyPlaceholderAutoConfiguration.class);
170+
assertEquals(0, context.getBeanNamesForType(ViewResolver.class).length);
171+
try {
172+
TemplateEngine engine = context.getBean(TemplateEngine.class);
173+
Context attrs = new Context(Locale.UK, Collections.singletonMap("greeting",
174+
"Hello World"));
175+
String result = engine.process("message", attrs);
176+
assertThat(result, containsString("Hello World"));
177+
}
178+
finally {
179+
context.close();
180+
}
181+
}
182+
140183
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public void testBindingExceptionForMachineClient() throws Exception {
8181
"http://localhost:" + this.port + "/bind", Map.class);
8282
String resp = entity.getBody().toString();
8383
assertThat(resp, containsString("Error count: 1"));
84-
assertThat(resp, containsString("errors=[{codes="));
84+
assertThat(resp, containsString("errors=[{"));
85+
assertThat(resp, containsString("codes=["));
8586
assertThat(resp, containsString("org.springframework.validation.BindException"));
8687
}
8788

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html><body th:text="${foo}">Home</body></html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html><body>Message: <span th:text="${greeting}">Hello</span></body></html>

0 commit comments

Comments
 (0)