Set output_encoding in FreeMarkerView implementations

According to the official FreeMarker documentation, Spring's
FreeMarkerView implementations should be configuring the
output_encoding for template rendering.

To address that, this commit modifies the FreeMarkerView
implementations in Web MVC and WebFlux to explicitly set the
output_encoding for template rendering.

See https://freemarker.apache.org/docs/pgui_misc_charset.html#autoid_53
See gh-33071
Closes gh-33106
This commit is contained in:
Sam Brannen
2024-06-27 10:03:26 +02:00
parent 95887c81b9
commit 8b95697c8d
7 changed files with 71 additions and 27 deletions

View File

@@ -22,6 +22,7 @@ import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import freemarker.core.Environment;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
@@ -364,19 +365,26 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Process the FreeMarker template to the servlet response.
* Process the FreeMarker template and write the result to the response.
* <p>As of Spring Framework 6.2, this method sets the
* {@linkplain Environment#setOutputEncoding(String) output encoding} of the
* FreeMarker {@link Environment} to the character encoding of the supplied
* {@link HttpServletResponse}.
* <p>Can be overridden to customize the behavior.
* @param template the template to process
* @param model the model for the template
* @param response servlet response (use this to get the OutputStream or Writer)
* @throws IOException if the template file could not be retrieved
* @throws TemplateException if thrown by FreeMarker
* @see freemarker.template.Template#process(Object, java.io.Writer)
* @see freemarker.template.Template#createProcessingEnvironment(Object, java.io.Writer)
* @see freemarker.core.Environment#process()
*/
protected void processTemplate(Template template, SimpleHash model, HttpServletResponse response)
throws IOException, TemplateException {
template.process(model, response.getWriter());
Environment env = template.createProcessingEnvironment(model, response.getWriter());
env.setOutputEncoding(response.getCharacterEncoding());
env.process();
}