Allow encoding to be set with a Charset in FreeMarker support

Closes gh-33102
This commit is contained in:
Sam Brannen
2024-06-26 16:18:09 +02:00
parent 4e13a69948
commit 73c0783df1
6 changed files with 59 additions and 15 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.web.reactive.result.view.freemarker;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import freemarker.cache.ClassTemplateLoader;
@@ -68,7 +69,7 @@ public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
public FreeMarkerConfigurer() {
setDefaultEncoding("UTF-8");
setDefaultEncoding(StandardCharsets.UTF_8);
}

View File

@@ -167,12 +167,24 @@ public class FreeMarkerView extends AbstractUrlBasedView {
* process. See the note in the {@linkplain FreeMarkerView class-level
* documentation} for details.
* @see freemarker.template.Configuration#setDefaultEncoding
* @see #setEncoding(Charset)
* @see #getEncoding()
*/
public void setEncoding(@Nullable String encoding) {
this.encoding = encoding;
}
/**
* Set the encoding used to decode byte sequences to character sequences when
* reading the FreeMarker template file for this view.
* <p>See {@link #setEncoding(String)} for details.
* @since 6.2
* @see java.nio.charset.StandardCharsets
*/
public void setEncoding(@Nullable Charset encoding) {
setEncoding(encoding != null ? encoding.name() : null);
}
/**
* Get the encoding used to decode byte sequences to character sequences
* when reading the FreeMarker template file for this view, or {@code null}
@@ -364,7 +376,9 @@ public class FreeMarkerView extends AbstractUrlBasedView {
}
/**
* Get the FreeMarker template for the given locale, to be rendered by this view.
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
* the specified locale and using the {@linkplain #setEncoding(String) configured
* encoding} if set.
* <p>By default, the template specified by the "url" bean property will be retrieved.
* @param locale the current locale
* @return the FreeMarker template to render
@@ -379,7 +393,9 @@ public class FreeMarkerView extends AbstractUrlBasedView {
}
/**
* Retrieve the FreeMarker template for the given locale, to be rendered by this view.
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
* the specified locale and using the {@linkplain #setEncoding(String) configured
* encoding} if set.
* <p>By default, the template specified by the "url" bean property will be retrieved,
* and the returned mono will subscribe on the
* {@linkplain Schedulers#boundedElastic() bounded elastic scheduler} as template

View File

@@ -135,7 +135,7 @@ class WebFluxViewResolutionIntegrationTests {
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setPreTemplateLoaders(classTemplateLoader);
configurer.setDefaultEncoding(UTF_8.name());
configurer.setDefaultEncoding(UTF_8);
return configurer;
}
}
@@ -158,7 +158,7 @@ class WebFluxViewResolutionIntegrationTests {
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setPreTemplateLoaders(classTemplateLoader);
configurer.setDefaultEncoding(ISO_8859_1.name());
configurer.setDefaultEncoding(ISO_8859_1);
return configurer;
}