Skip to content

Commit 2378fe0

Browse files
committed
Add support for using Velocity templates
1 parent b33eb95 commit 2378fe0

File tree

31 files changed

+1153
-179
lines changed

31 files changed

+1153
-179
lines changed

spring-boot-autoconfigure/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<artifactId>tomcat-jdbc</artifactId>
6767
<optional>true</optional>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.apache.velocity</groupId>
71+
<artifactId>velocity</artifactId>
72+
<optional>true</optional>
73+
</dependency>
6974
<dependency>
7075
<groupId>org.codehaus.groovy</groupId>
7176
<artifactId>groovy-templates</artifactId>

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java

+7-23
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
31+
import org.springframework.boot.autoconfigure.template.TemplateViewResolverConfigurer;
3132
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3233
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.context.annotation.Configuration;
35-
import org.springframework.core.Ordered;
3636
import org.springframework.core.io.DefaultResourceLoader;
3737
import org.springframework.core.io.Resource;
3838
import org.springframework.core.io.ResourceLoader;
@@ -45,7 +45,7 @@
4545

4646
/**
4747
* {@link EnableAutoConfiguration Auto-configuration} for FreeMarker.
48-
*
48+
*
4949
* @author Andy Wilkinson
5050
* @author Dave Syer
5151
* @since 1.1.0
@@ -65,8 +65,8 @@ public class FreeMarkerAutoConfiguration {
6565
@PostConstruct
6666
public void checkTemplateLocationExists() {
6767
if (this.properties.isCheckTemplateLocation()) {
68-
Resource resource = this.resourceLoader
69-
.getResource(this.properties.getTemplateLoaderPath());
68+
Resource resource = this.resourceLoader.getResource(this.properties
69+
.getTemplateLoaderPath());
7070
Assert.state(resource.exists(), "Cannot find template location: " + resource
7171
+ " (please add some templates "
7272
+ "or check your FreeMarker configuration)");
@@ -125,26 +125,10 @@ public freemarker.template.Configuration freeMarkerConfiguration(
125125
@ConditionalOnMissingBean(name = "freeMarkerViewResolver")
126126
public FreeMarkerViewResolver freeMarkerViewResolver() {
127127
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
128-
resolver.setPrefix(this.properties.getPrefix());
129-
resolver.setSuffix(this.properties.getSuffix());
130-
resolver.setCache(this.properties.isCache());
131-
resolver.setContentType(this.properties.getContentType());
132-
resolver.setViewNames(this.properties.getViewNames());
133-
resolver.setExposeRequestAttributes(this.properties
134-
.isExposeRequestAttributes());
135-
resolver.setExposeRequestAttributes(this.properties.isAllowRequestOverride());
136-
resolver.setExposeRequestAttributes(this.properties
137-
.isExposeSessionAttributes());
138-
resolver.setExposeRequestAttributes(this.properties
139-
.isExposeSpringMacroHelpers());
140-
resolver.setRequestContextAttribute(this.properties
141-
.getRequestContextAttribute());
142-
143-
// This resolver acts as a fallback resolver (e.g. like a
144-
// InternalResourceViewResolver) so it needs to have low precedence
145-
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
146-
128+
new TemplateViewResolverConfigurer().configureTemplateViewResolver(resolver,
129+
this.properties);
147130
return resolver;
148131
}
132+
149133
}
150134
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java

+13-128
Original file line numberDiff line numberDiff line change
@@ -19,106 +19,40 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
2223
import org.springframework.boot.context.properties.ConfigurationProperties;
2324

2425
/**
26+
* {@link ConfigurationProperties} for configuring FreeMarker
27+
*
2528
* @author Dave Syer
26-
*
29+
* @author Andy Wilkinson
30+
*
2731
* @since 1.1.0
2832
*/
2933
@ConfigurationProperties(prefix = "spring.freemarker")
30-
public class FreeMarkerProperties {
34+
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
3135

3236
public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
3337

3438
public static final String DEFAULT_PREFIX = "";
3539

3640
public static final String DEFAULT_SUFFIX = ".ftl";
3741

38-
private String prefix = DEFAULT_PREFIX;
39-
40-
private String suffix = DEFAULT_SUFFIX;
41-
42-
private String templateLoaderPath = DEFAULT_TEMPLATE_LOADER_PATH;
43-
44-
private boolean cache;
45-
46-
private String contentType = "text/html";
47-
48-
private String charSet = "UTF-8";
49-
50-
private String[] viewNames;
51-
52-
private boolean checkTemplateLocation = true;
53-
54-
private String requestContextAttribute;
55-
56-
private boolean exposeRequestAttributes = false;
57-
58-
private boolean exposeSessionAttributes = false;
59-
60-
private boolean allowRequestOverride = false;
61-
62-
private boolean exposeSpringMacroHelpers = true;
63-
6442
private Map<String, String> settings = new HashMap<String, String>();
6543

66-
public void setCheckTemplateLocation(boolean checkTemplateLocation) {
67-
this.checkTemplateLocation = checkTemplateLocation;
68-
}
69-
70-
public boolean isCheckTemplateLocation() {
71-
return this.checkTemplateLocation;
72-
}
73-
74-
public String[] getViewNames() {
75-
return this.viewNames;
76-
}
77-
78-
public void setViewNames(String[] viewNames) {
79-
this.viewNames = viewNames;
80-
}
81-
82-
public boolean isCache() {
83-
return this.cache;
84-
}
85-
86-
public void setCache(boolean cache) {
87-
this.cache = cache;
88-
}
89-
90-
public String getContentType() {
91-
return this.contentType
92-
+ (this.contentType.contains(";charset=") ? "" : ";charset="
93-
+ this.charSet);
94-
}
95-
96-
public void setContentType(String contentType) {
97-
this.contentType = contentType;
98-
}
99-
100-
public String getCharSet() {
101-
return this.charSet;
102-
}
103-
104-
public void setCharSet(String charSet) {
105-
this.charSet = charSet;
106-
}
107-
108-
public String getPrefix() {
109-
return this.prefix;
110-
}
44+
private String templateLoaderPath = DEFAULT_TEMPLATE_LOADER_PATH;
11145

112-
public void setPrefix(String prefix) {
113-
this.prefix = prefix;
46+
public FreeMarkerProperties() {
47+
super(DEFAULT_PREFIX, DEFAULT_SUFFIX);
11448
}
11549

116-
public String getSuffix() {
117-
return this.suffix;
50+
public Map<String, String> getSettings() {
51+
return this.settings;
11852
}
11953

120-
public void setSuffix(String suffix) {
121-
this.suffix = suffix;
54+
public void setSettings(Map<String, String> settings) {
55+
this.settings = settings;
12256
}
12357

12458
public String getTemplateLoaderPath() {
@@ -128,53 +62,4 @@ public String getTemplateLoaderPath() {
12862
public void setTemplateLoaderPath(String templateLoaderPath) {
12963
this.templateLoaderPath = templateLoaderPath;
13064
}
131-
132-
public String getRequestContextAttribute() {
133-
return this.requestContextAttribute;
134-
}
135-
136-
public void setRequestContextAttribute(String requestContextAttribute) {
137-
this.requestContextAttribute = requestContextAttribute;
138-
}
139-
140-
public boolean isExposeRequestAttributes() {
141-
return this.exposeRequestAttributes;
142-
}
143-
144-
public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
145-
this.exposeRequestAttributes = exposeRequestAttributes;
146-
}
147-
148-
public boolean isExposeSessionAttributes() {
149-
return this.exposeSessionAttributes;
150-
}
151-
152-
public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
153-
this.exposeSessionAttributes = exposeSessionAttributes;
154-
}
155-
156-
public boolean isAllowRequestOverride() {
157-
return this.allowRequestOverride;
158-
}
159-
160-
public void setAllowRequestOverride(boolean allowRequestOverride) {
161-
this.allowRequestOverride = allowRequestOverride;
162-
}
163-
164-
public boolean isExposeSpringMacroHelpers() {
165-
return this.exposeSpringMacroHelpers;
166-
}
167-
168-
public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
169-
this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
170-
}
171-
172-
public Map<String, String> getSettings() {
173-
return this.settings;
174-
}
175-
176-
public void setSettings(Map<String, String> settings) {
177-
this.settings = settings;
178-
}
179-
18065
}

0 commit comments

Comments
 (0)