Skip to content

Commit b64edb2

Browse files
committed
Update Content-Type based on encoding in MVC FreeMarkerView
Closes gh-33119
1 parent ce53443 commit b64edb2

File tree

12 files changed

+134
-54
lines changed

12 files changed

+134
-54
lines changed

framework-docs/modules/ROOT/pages/web/webmvc-view/mvc-freemarker.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Java::
3636
public FreeMarkerConfigurer freeMarkerConfigurer() {
3737
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
3838
configurer.setTemplateLoaderPath("/WEB-INF/freemarker");
39+
configurer.setDefaultCharset(StandardCharsets.UTF_8);
3940
return configurer;
4041
}
4142
}
@@ -58,6 +59,7 @@ Kotlin::
5859
@Bean
5960
fun freeMarkerConfigurer() = FreeMarkerConfigurer().apply {
6061
setTemplateLoaderPath("/WEB-INF/freemarker")
62+
setDefaultCharset(StandardCharsets.UTF_8)
6163
}
6264
}
6365
----
@@ -86,6 +88,7 @@ properties, as the following example shows:
8688
----
8789
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
8890
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
91+
<property name="defaultEncoding" value="UTF-8"/>
8992
</bean>
9093
----
9194

spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
* <p>The simplest way to use this class is to specify a "templateLoaderPath";
6363
* FreeMarker does not need any further configuration then.
6464
*
65-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
65+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
6666
*
6767
* @author Darren Davison
6868
* @author Juergen Hoeller
@@ -143,15 +143,18 @@ public void setFreemarkerVariables(Map<String, Object> variables) {
143143
* files.
144144
* <p>If not specified, FreeMarker will read template files using the platform
145145
* file encoding (defined by the JVM system property {@code file.encoding})
146-
* or {@code "utf-8"} if the platform file encoding is undefined.
147-
* <p>Note that the encoding is not used for template rendering. Instead, an
148-
* explicit encoding must be specified for the rendering process &mdash; for
149-
* example, via Spring's {@code FreeMarkerView} or {@code FreeMarkerViewResolver}.
146+
* or UTF-8 if the platform file encoding is undefined.
147+
* <p>Note that the supplied encoding may or may not be used for template
148+
* rendering. See the documentation for Spring's {@code FreeMarkerView} and
149+
* {@code FreeMarkerViewResolver} implementations for further details.
150150
* @see #setDefaultEncoding(Charset)
151151
* @see freemarker.template.Configuration#setDefaultEncoding
152152
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView#setEncoding
153153
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView#setContentType
154154
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setContentType
155+
* @see org.springframework.web.reactive.result.view.freemarker.FreeMarkerView#setEncoding
156+
* @see org.springframework.web.reactive.result.view.freemarker.FreeMarkerView#setSupportedMediaTypes
157+
* @see org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver#setSupportedMediaTypes
155158
*/
156159
public void setDefaultEncoding(String defaultEncoding) {
157160
this.defaultEncoding = defaultEncoding;
@@ -170,7 +173,7 @@ public void setDefaultCharset(Charset defaultCharset) {
170173
}
171174

172175
/**
173-
* Set a List of {@link TemplateLoader TemplateLoaders} that will be used to
176+
* Set a list of {@link TemplateLoader TemplateLoaders} that will be used to
174177
* search for templates.
175178
* <p>For example, one or more custom loaders such as database loaders could
176179
* be configured and injected here.
@@ -186,7 +189,7 @@ public void setPreTemplateLoaders(TemplateLoader... preTemplateLoaders) {
186189
}
187190

188191
/**
189-
* Set a List of {@link TemplateLoader TemplateLoaders} that will be used to
192+
* Set a list of {@link TemplateLoader TemplateLoaders} that will be used to
190193
* search for templates.
191194
* <p>For example, one or more custom loaders such as database loaders could
192195
* be configured and injected here.

spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* <p>See the {@link FreeMarkerConfigurationFactory} base class for configuration
4646
* details.
4747
*
48-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
48+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
4949
*
5050
* @author Darren Davison
5151
* @since 03.03.2004

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*
2525
* <p>Detected and used by {@link FreeMarkerView}.
2626
*
27+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
28+
*
2729
* @author Rossen Stoyanchev
2830
* @since 5.0
2931
*/

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerConfigurer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* &lt;@spring.bind "person.age"/&gt;
5757
* age is ${spring.status.value}</pre>
5858
*
59-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
59+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
6060
*
6161
* @author Rossen Stoyanchev
6262
* @since 5.0

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
* sets the supported media type to {@code "text/html;charset=UTF-8"} by default.
9191
* Thus, those default values are likely suitable for most applications.
9292
*
93-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
93+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
9494
*
9595
* @author Rossen Stoyanchev
9696
* @author Sam Brannen
@@ -158,7 +158,7 @@ protected Configuration obtainConfiguration() {
158158
* <p>If the encoding is not explicitly set here or in the FreeMarker
159159
* {@code Configuration}, FreeMarker will read template files using the platform
160160
* file encoding (defined by the JVM system property {@code file.encoding})
161-
* or {@code "utf-8"} if the platform file encoding is undefined. Note,
161+
* or UTF-8 if the platform file encoding is undefined. Note,
162162
* however, that {@link FreeMarkerConfigurer} sets the default encoding in the
163163
* FreeMarker {@code Configuration} to "UTF-8".
164164
* <p>It's recommended to specify the encoding in the FreeMarker {@code Configuration}

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,8 @@
2626
* <p>The view class for all views generated by this resolver can be specified
2727
* via the "viewClass" property. See {@link UrlBasedViewResolver} for details.
2828
*
29+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
30+
*
2931
* @author Rossen Stoyanchev
3032
* @since 5.0
3133
*/

spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*
2525
* <p>Detected and used by {@link FreeMarkerView}.
2626
*
27+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
28+
*
2729
* @author Darren Davison
2830
* @author Rob Harrop
2931
* @since 03.03.2004

spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
* &lt;@spring.bind "person.age"/&gt;
6363
* age is ${spring.status.value}</pre>
6464
*
65-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
65+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
6666
*
6767
* @author Darren Davison
6868
* @author Rob Harrop

spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerView.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
* byte sequences to character sequences when reading the FreeMarker template file.
5757
* Default is determined by the FreeMarker {@link Configuration}.</li>
5858
* <li><b>{@link #setContentType(String) contentType}</b>: the content type of the
59-
* rendered response. Defaults to {@code "text/html;charset=ISO-8859-1"} but should
60-
* typically be set to a value that corresponds to the actual generated content
59+
* rendered response. Defaults to {@code "text/html;charset=ISO-8859-1"} but may
60+
* need to be set to a value that corresponds to the actual generated content
6161
* type (see note below).</li>
6262
* </ul>
6363
*
@@ -72,9 +72,13 @@
7272
* {@code "text/html;charset=UTF-8"}. When using {@link FreeMarkerViewResolver}
7373
* to create the view for you, set the
7474
* {@linkplain FreeMarkerViewResolver#setContentType(String) content type}
75-
* directly in the {@code FreeMarkerViewResolver}.
75+
* directly in the {@code FreeMarkerViewResolver}; however, as of Spring Framework
76+
* 6.2, it is no longer necessary to explicitly set the content type in the
77+
* {@code FreeMarkerViewResolver} if you have set an explicit encoding via either
78+
* {@link #setEncoding(String)}, {@link FreeMarkerConfigurer#setDefaultEncoding(String)},
79+
* or {@link Configuration#setDefaultEncoding(String)}.
7680
*
77-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
81+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.26 or higher.
7882
* As of Spring Framework 6.0, FreeMarker templates are rendered in a minimal
7983
* fashion without JSP support, just exposing request attributes in addition
8084
* to the MVC-provided model map for alignment with common Servlet resources.
@@ -109,13 +113,11 @@ public class FreeMarkerView extends AbstractTemplateView {
109113
* <p>If the encoding is not explicitly set here or in the FreeMarker
110114
* {@code Configuration}, FreeMarker will read template files using the platform
111115
* file encoding (defined by the JVM system property {@code file.encoding})
112-
* or {@code "utf-8"} if the platform file encoding is undefined.
116+
* or UTF-8 if the platform file encoding is undefined.
113117
* <p>It's recommended to specify the encoding in the FreeMarker {@code Configuration}
114118
* rather than per template if all your templates share a common encoding.
115-
* <p>Note that the specified or default encoding is not used for template
116-
* rendering. Instead, an explicit encoding must be specified for the rendering
117-
* process. See the note in the {@linkplain FreeMarkerView class-level
118-
* documentation} for details.
119+
* <p>See the note in the {@linkplain FreeMarkerView class-level documentation}
120+
* for details regarding the encoding used to render the response.
119121
* @see freemarker.template.Configuration#setDefaultEncoding
120122
* @see #setCharset(Charset)
121123
* @see #getEncoding()

0 commit comments

Comments
 (0)