Allow multiple templateLoaderPaths to be configured for FreeMarker
Closes gh-1767
This commit is contained in:
parent
ef2455938e
commit
8a1e010d0a
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.freemarker;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
@ -65,10 +67,18 @@ public class FreeMarkerAutoConfiguration {
|
|||
@PostConstruct
|
||||
public void checkTemplateLocationExists() {
|
||||
if (this.properties.isCheckTemplateLocation()) {
|
||||
Resource resource = this.resourceLoader.getResource(this.properties
|
||||
.getTemplateLoaderPath());
|
||||
Assert.state(resource.exists(), "Cannot find template location: " + resource
|
||||
+ " (please add some templates, "
|
||||
Resource templatePathResource = null;
|
||||
List<Resource> resources = new ArrayList<Resource>();
|
||||
for (String templateLoaderPath : this.properties.getTemplateLoaderPath()) {
|
||||
Resource resource = this.resourceLoader.getResource(templateLoaderPath);
|
||||
resources.add(resource);
|
||||
if (resource.exists()) {
|
||||
templatePathResource = resource;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Assert.notNull(templatePathResource, "Cannot find template location(s): "
|
||||
+ resources + " (please add some templates, "
|
||||
+ "check your FreeMarker configuration, or set "
|
||||
+ "spring.freemarker.checkTemplateLocation=false)");
|
||||
}
|
||||
|
@ -80,7 +90,7 @@ public class FreeMarkerAutoConfiguration {
|
|||
protected FreeMarkerProperties properties;
|
||||
|
||||
protected void applyProperties(FreeMarkerConfigurationFactory factory) {
|
||||
factory.setTemplateLoaderPath(this.properties.getTemplateLoaderPath());
|
||||
factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
|
||||
factory.setDefaultEncoding(this.properties.getCharSet());
|
||||
Properties settings = new Properties();
|
||||
settings.putAll(this.properties.getSettings());
|
||||
|
|
|
@ -40,7 +40,7 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
|
|||
|
||||
private Map<String, String> settings = new HashMap<String, String>();
|
||||
|
||||
private String templateLoaderPath = DEFAULT_TEMPLATE_LOADER_PATH;
|
||||
private String[] templateLoaderPath = new String[] { DEFAULT_TEMPLATE_LOADER_PATH };
|
||||
|
||||
public FreeMarkerProperties() {
|
||||
super(DEFAULT_PREFIX, DEFAULT_SUFFIX);
|
||||
|
@ -54,12 +54,12 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
|
|||
this.settings = settings;
|
||||
}
|
||||
|
||||
public String getTemplateLoaderPath() {
|
||||
public String[] getTemplateLoaderPath() {
|
||||
return this.templateLoaderPath;
|
||||
}
|
||||
|
||||
public void setTemplateLoaderPath(String templateLoaderPath) {
|
||||
this.templateLoaderPath = templateLoaderPath;
|
||||
public void setTemplateLoaderPath(String... templateLoaderPaths) {
|
||||
this.templateLoaderPath = templateLoaderPaths;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ public class FreeMarkerAutoConfigurationTests {
|
|||
@Test(expected = BeanCreationException.class)
|
||||
public void nonExistentTemplateLocation() {
|
||||
registerAndRefreshContext("spring.freemarker.templateLoaderPath:"
|
||||
+ "classpath:/does-not-exist/");
|
||||
+ "classpath:/does-not-exist/,classpath:/also-does-not-exist");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -83,6 +83,13 @@ public class FreeMarkerAutoConfigurationTests {
|
|||
+ "classpath:/templates/empty-directory/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonExistentLocationAndEmptyLocation() {
|
||||
new File("target/test-classes/templates/empty-directory").mkdir();
|
||||
registerAndRefreshContext("spring.freemarker.templateLoaderPath:"
|
||||
+ "classpath:/does-not-exist/,classpath:/templates/empty-directory/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultViewResolution() throws Exception {
|
||||
registerAndRefreshContext();
|
||||
|
|
|
@ -123,7 +123,7 @@ content into your application; rather pick only the properties that you need.
|
|||
spring.freemarker.requestContextAttribute=
|
||||
spring.freemarker.settings.*=
|
||||
spring.freemarker.suffix=.ftl
|
||||
spring.freemarker.templateLoaderPath=classpath:/templates/
|
||||
spring.freemarker.templateLoaderPath=classpath:/templates/ # comma-separated list
|
||||
spring.freemarker.viewNames= # whitelist of view names that can be resolved
|
||||
|
||||
# GROOVY TEMPLATES ({sc-spring-boot-autoconfigure}/groovy/template/GroovyTemplateAutoConfiguration.{sc-ext}[GroovyTemplateAutoConfiguration])
|
||||
|
|
Loading…
Reference in New Issue