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

@@ -18,6 +18,7 @@ package org.springframework.web.servlet.view.freemarker;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
@@ -115,6 +116,7 @@ public class FreeMarkerView extends AbstractTemplateView {
* process. See the note in the {@linkplain FreeMarkerView class-level
* documentation} for details.
* @see freemarker.template.Configuration#setDefaultEncoding
* @see #setEncoding(Charset)
* @see #getEncoding()
* @see #setContentType(String)
*/
@@ -122,6 +124,17 @@ public class FreeMarkerView extends AbstractTemplateView {
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}
@@ -316,10 +329,10 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Retrieve the FreeMarker {@link Template} for the given locale, to be
* rendered by this view.
* <p>By default, the template specified by the "url" bean property
* will be retrieved.
* 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 {@code Template} to render
* @throws IOException if the template file could not be retrieved
@@ -333,8 +346,9 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Retrieve the FreeMarker {@link Template} for the specified name and locale,
* using the {@linkplain #setEncoding(String) configured encoding} if set.
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
* the specified name and locale and using the {@linkplain #setEncoding(String)
* configured encoding} if set.
* <p>Can be called by subclasses to retrieve a specific template,
* for example to render multiple templates into a single view.
* @param name the file name of the desired template

View File

@@ -16,8 +16,6 @@
package org.springframework.web.servlet.config.annotation;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@@ -37,6 +35,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.testfixture.servlet.MockServletConfig;
import org.springframework.web.testfixture.servlet.MockServletContext;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
@@ -165,7 +164,7 @@ class ViewResolutionIntegrationTests {
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
configurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
configurer.setDefaultEncoding(UTF_8);
return configurer;
}
}
@@ -184,7 +183,7 @@ class ViewResolutionIntegrationTests {
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
configurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
configurer.setDefaultEncoding(UTF_8);
return configurer;
}
}