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
This commit is contained in:
@@ -42,6 +42,7 @@ abstract class AbstractFreeMarkerConfiguration {
|
||||
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
|
||||
factory.setDefaultEncoding(this.properties.getCharsetName());
|
||||
Properties settings = new Properties();
|
||||
settings.put("recognize_standard_file_extensions", "true");
|
||||
settings.putAll(this.properties.getSettings());
|
||||
factory.setFreemarkerSettings(settings);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
|
||||
|
||||
public static final String DEFAULT_PREFIX = "";
|
||||
|
||||
public static final String DEFAULT_SUFFIX = ".ftl";
|
||||
public static final String DEFAULT_SUFFIX = ".ftlh";
|
||||
|
||||
/**
|
||||
* Well-known FreeMarker keys which are passed to FreeMarker's Configuration.
|
||||
|
||||
@@ -495,7 +495,7 @@
|
||||
},
|
||||
{
|
||||
"name": "spring.freemarker.suffix",
|
||||
"defaultValue": ".ftl"
|
||||
"defaultValue": ".ftlh"
|
||||
},
|
||||
{
|
||||
"name": "spring.groovy.template.prefix",
|
||||
|
||||
@@ -108,7 +108,7 @@ class FreeMarkerAutoConfigurationReactiveIntegrationTests {
|
||||
this.contextRunner.withPropertyValues().run((context) -> {
|
||||
FreeMarkerConfigurer freemarker = context.getBean(FreeMarkerConfigurer.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
freemarker.getConfiguration().getTemplate("message.ftl").process(new DataModel(), writer);
|
||||
freemarker.getConfiguration().getTemplate("message.ftlh").process(new DataModel(), writer);
|
||||
assertThat(writer.toString()).contains("Hello World");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ class FreeMarkerAutoConfigurationServletIntegrationTests {
|
||||
load();
|
||||
FreeMarkerConfigurer freemarker = this.context.getBean(FreeMarkerConfigurer.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
freemarker.getConfiguration().getTemplate("message.ftl").process(new DataModel(), writer);
|
||||
freemarker.getConfiguration().getTemplate("message.ftlh").process(new DataModel(), writer);
|
||||
assertThat(writer.toString()).contains("Hello World");
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ public class FreeMarkerAutoConfigurationTests {
|
||||
this.contextRunner.run((context) -> {
|
||||
freemarker.template.Configuration freemarker = context.getBean(freemarker.template.Configuration.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
freemarker.getTemplate("message.ftl").process(new DataModel(), writer);
|
||||
freemarker.getTemplate("message.ftlh").process(new DataModel(), writer);
|
||||
assertThat(writer.toString()).contains("Hello World");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1417,7 +1417,7 @@ externalized to `spring.freemarker.templateLoaderPath` and has a default value o
|
||||
'`classpath:/templates/`') by surrounding the view name with a prefix and a suffix. The
|
||||
prefix is externalized to `spring.freemarker.prefix`, and the suffix is externalized to
|
||||
`spring.freemarker.suffix`. The default values of the prefix and suffix are empty and
|
||||
'`.ftl`', respectively. You can override `FreeMarkerViewResolver` by providing a bean
|
||||
'`.ftlh`', respectively. You can override `FreeMarkerViewResolver` by providing a bean
|
||||
of the same name.
|
||||
* If you use Groovy templates (actually, if `groovy-templates` is on your classpath), you
|
||||
also have a `GroovyMarkupViewResolver` named '`groovyMarkupViewResolver`'. It looks for
|
||||
@@ -2522,7 +2522,7 @@ error page rather than disabling it completely.
|
||||
|
||||
Overriding the error page with your own depends on the templating technology that you
|
||||
use. For example, if you use Thymeleaf, you can add an `error.html` template.
|
||||
If you use FreeMarker, you can add an `error.ftl` template. In general, you
|
||||
If you use FreeMarker, you can add an `error.ftlh` template. In general, you
|
||||
need a `View` that resolves with a name of `error` or a `@Controller` that handles
|
||||
the `/error` path. Unless you replaced some of the default configuration, you should find
|
||||
a `BeanNameViewResolver` in your `ApplicationContext`, so a `@Bean` named `error` would
|
||||
|
||||
@@ -2702,7 +2702,7 @@ follows:
|
||||
+- resources/
|
||||
+- templates/
|
||||
+- error/
|
||||
| +- 5xx.ftl
|
||||
| +- 5xx.ftlh
|
||||
+- <other templates>
|
||||
----
|
||||
|
||||
|
||||
Reference in New Issue
Block a user