Skip to content

Commit 7a9d1a1

Browse files
committed
Change default Freemarker template file extension
This commit changes the default file extension for Freemarker templates from `*.ftl` to `*.ftlh`. This commit also enables by default the Freemarker setting `"recognize_standard_file_extensions"` to ensure that HTML escaping is performed by default in Spring Boot applications. Applications should adapt to this change by changing the file extensions of existing templates to `.ftlh`. Closes gh-15131
1 parent 048be18 commit 7a9d1a1

File tree

20 files changed

+9
-8
lines changed

20 files changed

+9
-8
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected void applyProperties(FreeMarkerConfigurationFactory factory) {
4242
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
4343
factory.setDefaultEncoding(this.properties.getCharsetName());
4444
Properties settings = new Properties();
45+
settings.put("recognize_standard_file_extensions", "true");
4546
settings.putAll(this.properties.getSettings());
4647
factory.setFreemarkerSettings(settings);
4748
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
3636

3737
public static final String DEFAULT_PREFIX = "";
3838

39-
public static final String DEFAULT_SUFFIX = ".ftl";
39+
public static final String DEFAULT_SUFFIX = ".ftlh";
4040

4141
/**
4242
* Well-known FreeMarker keys which are passed to FreeMarker's Configuration.

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@
495495
},
496496
{
497497
"name": "spring.freemarker.suffix",
498-
"defaultValue": ".ftl"
498+
"defaultValue": ".ftlh"
499499
},
500500
{
501501
"name": "spring.groovy.template.prefix",

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationReactiveIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void renderTemplate() {
108108
this.contextRunner.withPropertyValues().run((context) -> {
109109
FreeMarkerConfigurer freemarker = context.getBean(FreeMarkerConfigurer.class);
110110
StringWriter writer = new StringWriter();
111-
freemarker.getConfiguration().getTemplate("message.ftl").process(new DataModel(), writer);
111+
freemarker.getConfiguration().getTemplate("message.ftlh").process(new DataModel(), writer);
112112
assertThat(writer.toString()).contains("Hello World");
113113
});
114114
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationServletIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void renderTemplate() throws Exception {
143143
load();
144144
FreeMarkerConfigurer freemarker = this.context.getBean(FreeMarkerConfigurer.class);
145145
StringWriter writer = new StringWriter();
146-
freemarker.getConfiguration().getTemplate("message.ftl").process(new DataModel(), writer);
146+
freemarker.getConfiguration().getTemplate("message.ftlh").process(new DataModel(), writer);
147147
assertThat(writer.toString()).contains("Hello World");
148148
}
149149

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void renderNonWebAppTemplate() {
4949
this.contextRunner.run((context) -> {
5050
freemarker.template.Configuration freemarker = context.getBean(freemarker.template.Configuration.class);
5151
StringWriter writer = new StringWriter();
52-
freemarker.getTemplate("message.ftl").process(new DataModel(), writer);
52+
freemarker.getTemplate("message.ftlh").process(new DataModel(), writer);
5353
assertThat(writer.toString()).contains("Hello World");
5454
});
5555
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ externalized to `spring.freemarker.templateLoaderPath` and has a default value o
14171417
'`classpath:/templates/`') by surrounding the view name with a prefix and a suffix. The
14181418
prefix is externalized to `spring.freemarker.prefix`, and the suffix is externalized to
14191419
`spring.freemarker.suffix`. The default values of the prefix and suffix are empty and
1420-
'`.ftl`', respectively. You can override `FreeMarkerViewResolver` by providing a bean
1420+
'`.ftlh`', respectively. You can override `FreeMarkerViewResolver` by providing a bean
14211421
of the same name.
14221422
* If you use Groovy templates (actually, if `groovy-templates` is on your classpath), you
14231423
also have a `GroovyMarkupViewResolver` named '`groovyMarkupViewResolver`'. It looks for
@@ -2522,7 +2522,7 @@ error page rather than disabling it completely.
25222522

25232523
Overriding the error page with your own depends on the templating technology that you
25242524
use. For example, if you use Thymeleaf, you can add an `error.html` template.
2525-
If you use FreeMarker, you can add an `error.ftl` template. In general, you
2525+
If you use FreeMarker, you can add an `error.ftlh` template. In general, you
25262526
need a `View` that resolves with a name of `error` or a `@Controller` that handles
25272527
the `/error` path. Unless you replaced some of the default configuration, you should find
25282528
a `BeanNameViewResolver` in your `ApplicationContext`, so a `@Bean` named `error` would

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2702,7 +2702,7 @@ follows:
27022702
+- resources/
27032703
+- templates/
27042704
+- error/
2705-
| +- 5xx.ftl
2705+
| +- 5xx.ftlh
27062706
+- <other templates>
27072707
----
27082708

0 commit comments

Comments
 (0)