Fail on startup if Thymeleaf template directory missing

Better even than logging would be to fail fast? Surely it's a
mistake not to have any /templates if Thymeleaf is in use.
User can disable failfast by exlcuding thymeleaf configuration
or by providing their own ITemplateResolver.

Fixes gh-294
pull/382/merge
Dave Syer 11 years ago
parent 8d9c26b2df
commit f38a36ff70

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.thymeleaf;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import javax.annotation.PostConstruct;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import nz.net.ultraq.thymeleaf.LayoutDialect; import nz.net.ultraq.thymeleaf.LayoutDialect;
@ -36,6 +37,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.dialect.IDialect; import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect; import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect;
@ -74,6 +76,19 @@ public class ThymeleafAutoConfiguration {
"spring.thymeleaf."); "spring.thymeleaf.");
} }
@PostConstruct
public void checkTemplateLocationExists() {
if (this.environment
.getProperty("checkTemplateLocation", Boolean.class, true)) {
Resource resource = this.resourceLoader.getResource(this.environment
.getProperty("prefix", DEFAULT_PREFIX));
if (!resource.exists()) {
throw new IllegalStateException("Cannot find template location: "
+ resource + " (are you really using Thymeleaf?)");
}
}
}
@Bean @Bean
public ITemplateResolver defaultTemplateResolver() { public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new TemplateResolver(); TemplateResolver resolver = new TemplateResolver();

@ -17,14 +17,14 @@
package org.springframework.boot.autoconfigure.thymeleaf; package org.springframework.boot.autoconfigure.thymeleaf;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
@ -47,41 +47,50 @@ import static org.junit.Assert.assertTrue;
*/ */
public class ThymeleafAutoConfigurationTests { public class ThymeleafAutoConfigurationTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void createFromConfigClass() throws Exception { public void createFromConfigClass() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.thymeleaf.mode:XHTML",
context.register(ThymeleafAutoConfiguration.class, "spring.thymeleaf.suffix:");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
Map<String, Object> map = new HashMap<String, Object>(); this.context.refresh();
map.put("spring.thymeleaf.mode", "XHTML"); TemplateEngine engine = this.context.getBean(TemplateEngine.class);
map.put("spring.thymeleaf.suffix", "");
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test", map));
context.refresh();
TemplateEngine engine = context.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
String result = engine.process("template.txt", attrs); String result = engine.process("template.txt", attrs);
assertEquals("<html>bar</html>", result); assertEquals("<html>bar</html>", result);
context.close();
} }
@Test @Test
public void overrideCharacterEncoding() throws Exception { public void overrideCharacterEncoding() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context,
context.register(ThymeleafAutoConfiguration.class, "spring.thymeleaf.encoding:UTF-16");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
Map<String, Object> map = new HashMap<String, Object>(); this.context.refresh();
map.put("spring.thymeleaf.encoding", "UTF-16"); this.context.getBean(TemplateEngine.class).initialize();
context.getEnvironment().getPropertySources() ITemplateResolver resolver = this.context.getBean(ITemplateResolver.class);
.addFirst(new MapPropertySource("test", map));
context.refresh();
context.getBean(TemplateEngine.class).initialize();
ITemplateResolver resolver = context.getBean(ITemplateResolver.class);
assertTrue(resolver instanceof TemplateResolver); assertTrue(resolver instanceof TemplateResolver);
assertEquals("UTF-16", ((TemplateResolver) resolver).getCharacterEncoding()); assertEquals("UTF-16", ((TemplateResolver) resolver).getCharacterEncoding());
ThymeleafViewResolver views = context.getBean(ThymeleafViewResolver.class); ThymeleafViewResolver views = this.context.getBean(ThymeleafViewResolver.class);
assertEquals("UTF-16", views.getCharacterEncoding()); assertEquals("UTF-16", views.getCharacterEncoding());
context.close(); }
@Test(expected = BeanCreationException.class)
public void templateLocationDoesNotExist() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"spring.thymeleaf.prefix:classpath:/no-such-directory/");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
} }
@Test @Test

Loading…
Cancel
Save