Skip to content

Commit 73c0783

Browse files
committed
Allow encoding to be set with a Charset in FreeMarker support
Closes gh-33102
1 parent 4e13a69 commit 73c0783

File tree

6 files changed

+59
-15
lines changed

6 files changed

+59
-15
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.nio.charset.Charset;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.List;
@@ -146,6 +147,7 @@ public void setFreemarkerVariables(Map<String, Object> variables) {
146147
* <p>Note that the encoding is not used for template rendering. Instead, an
147148
* explicit encoding must be specified for the rendering process &mdash; for
148149
* example, via Spring's {@code FreeMarkerView} or {@code FreeMarkerViewResolver}.
150+
* @see #setDefaultEncoding(Charset)
149151
* @see freemarker.template.Configuration#setDefaultEncoding
150152
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView#setEncoding
151153
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView#setContentType
@@ -155,6 +157,18 @@ public void setDefaultEncoding(String defaultEncoding) {
155157
this.defaultEncoding = defaultEncoding;
156158
}
157159

160+
/**
161+
* Set the default encoding for the FreeMarker {@link Configuration}, which
162+
* is used to decode byte sequences to character sequences when reading template
163+
* files.
164+
* <p>See {@link #setDefaultEncoding(String)} for details.
165+
* @since 6.2
166+
* @see java.nio.charset.StandardCharsets
167+
*/
168+
public void setDefaultEncoding(Charset defaultEncoding) {
169+
setDefaultEncoding(defaultEncoding.name());
170+
}
171+
158172
/**
159173
* Set a List of {@link TemplateLoader TemplateLoaders} that will be used to
160174
* search for templates.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.reactive.result.view.freemarker;
1818

1919
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.List;
2122

2223
import freemarker.cache.ClassTemplateLoader;
@@ -68,7 +69,7 @@ public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
6869

6970

7071
public FreeMarkerConfigurer() {
71-
setDefaultEncoding("UTF-8");
72+
setDefaultEncoding(StandardCharsets.UTF_8);
7273
}
7374

7475

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,24 @@ protected Configuration obtainConfiguration() {
167167
* process. See the note in the {@linkplain FreeMarkerView class-level
168168
* documentation} for details.
169169
* @see freemarker.template.Configuration#setDefaultEncoding
170+
* @see #setEncoding(Charset)
170171
* @see #getEncoding()
171172
*/
172173
public void setEncoding(@Nullable String encoding) {
173174
this.encoding = encoding;
174175
}
175176

177+
/**
178+
* Set the encoding used to decode byte sequences to character sequences when
179+
* reading the FreeMarker template file for this view.
180+
* <p>See {@link #setEncoding(String)} for details.
181+
* @since 6.2
182+
* @see java.nio.charset.StandardCharsets
183+
*/
184+
public void setEncoding(@Nullable Charset encoding) {
185+
setEncoding(encoding != null ? encoding.name() : null);
186+
}
187+
176188
/**
177189
* Get the encoding used to decode byte sequences to character sequences
178190
* when reading the FreeMarker template file for this view, or {@code null}
@@ -364,7 +376,9 @@ protected ObjectWrapper getObjectWrapper() {
364376
}
365377

366378
/**
367-
* Get the FreeMarker template for the given locale, to be rendered by this view.
379+
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
380+
* the specified locale and using the {@linkplain #setEncoding(String) configured
381+
* encoding} if set.
368382
* <p>By default, the template specified by the "url" bean property will be retrieved.
369383
* @param locale the current locale
370384
* @return the FreeMarker template to render
@@ -379,7 +393,9 @@ protected Template getTemplate(Locale locale) throws IOException {
379393
}
380394

381395
/**
382-
* Retrieve the FreeMarker template for the given locale, to be rendered by this view.
396+
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
397+
* the specified locale and using the {@linkplain #setEncoding(String) configured
398+
* encoding} if set.
383399
* <p>By default, the template specified by the "url" bean property will be retrieved,
384400
* and the returned mono will subscribe on the
385401
* {@linkplain Schedulers#boundedElastic() bounded elastic scheduler} as template

spring-webflux/src/test/java/org/springframework/web/reactive/config/WebFluxViewResolutionIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void configureViewResolvers(ViewResolverRegistry registry) {
135135
public FreeMarkerConfigurer freeMarkerConfigurer() {
136136
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
137137
configurer.setPreTemplateLoaders(classTemplateLoader);
138-
configurer.setDefaultEncoding(UTF_8.name());
138+
configurer.setDefaultEncoding(UTF_8);
139139
return configurer;
140140
}
141141
}
@@ -158,7 +158,7 @@ public void configureViewResolvers(ViewResolverRegistry registry) {
158158
public FreeMarkerConfigurer freeMarkerConfigurer() {
159159
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
160160
configurer.setPreTemplateLoaders(classTemplateLoader);
161-
configurer.setDefaultEncoding(ISO_8859_1.name());
161+
configurer.setDefaultEncoding(ISO_8859_1);
162162
return configurer;
163163
}
164164

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
21+
import java.nio.charset.Charset;
2122
import java.util.Locale;
2223
import java.util.Map;
2324

@@ -115,13 +116,25 @@ public class FreeMarkerView extends AbstractTemplateView {
115116
* process. See the note in the {@linkplain FreeMarkerView class-level
116117
* documentation} for details.
117118
* @see freemarker.template.Configuration#setDefaultEncoding
119+
* @see #setEncoding(Charset)
118120
* @see #getEncoding()
119121
* @see #setContentType(String)
120122
*/
121123
public void setEncoding(@Nullable String encoding) {
122124
this.encoding = encoding;
123125
}
124126

127+
/**
128+
* Set the encoding used to decode byte sequences to character sequences when
129+
* reading the FreeMarker template file for this view.
130+
* <p>See {@link #setEncoding(String)} for details.
131+
* @since 6.2
132+
* @see java.nio.charset.StandardCharsets
133+
*/
134+
public void setEncoding(@Nullable Charset encoding) {
135+
setEncoding(encoding != null ? encoding.name() : null);
136+
}
137+
125138
/**
126139
* Get the encoding used to decode byte sequences to character sequences
127140
* when reading the FreeMarker template file for this view, or {@code null}
@@ -316,10 +329,10 @@ protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRe
316329
}
317330

318331
/**
319-
* Retrieve the FreeMarker {@link Template} for the given locale, to be
320-
* rendered by this view.
321-
* <p>By default, the template specified by the "url" bean property
322-
* will be retrieved.
332+
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
333+
* the specified locale and using the {@linkplain #setEncoding(String) configured
334+
* encoding} if set.
335+
* <p>By default, the template specified by the "url" bean property will be retrieved.
323336
* @param locale the current locale
324337
* @return the FreeMarker {@code Template} to render
325338
* @throws IOException if the template file could not be retrieved
@@ -333,8 +346,9 @@ protected Template getTemplate(Locale locale) throws IOException {
333346
}
334347

335348
/**
336-
* Retrieve the FreeMarker {@link Template} for the specified name and locale,
337-
* using the {@linkplain #setEncoding(String) configured encoding} if set.
349+
* Retrieve the FreeMarker {@link Template} to be rendered by this view, for
350+
* the specified name and locale and using the {@linkplain #setEncoding(String)
351+
* configured encoding} if set.
338352
* <p>Can be called by subclasses to retrieve a specific template,
339353
* for example to render multiple templates into a single view.
340354
* @param name the file name of the desired template

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.web.servlet.config.annotation;
1818

19-
import java.nio.charset.StandardCharsets;
20-
2119
import org.junit.jupiter.api.BeforeAll;
2220
import org.junit.jupiter.api.Nested;
2321
import org.junit.jupiter.api.Test;
@@ -37,6 +35,7 @@
3735
import org.springframework.web.testfixture.servlet.MockServletConfig;
3836
import org.springframework.web.testfixture.servlet.MockServletContext;
3937

38+
import static java.nio.charset.StandardCharsets.UTF_8;
4039
import static org.assertj.core.api.Assertions.assertThat;
4140
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
4241

@@ -165,7 +164,7 @@ public void configureViewResolvers(ViewResolverRegistry registry) {
165164
public FreeMarkerConfigurer freeMarkerConfigurer() {
166165
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
167166
configurer.setTemplateLoaderPath("/WEB-INF/");
168-
configurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
167+
configurer.setDefaultEncoding(UTF_8);
169168
return configurer;
170169
}
171170
}
@@ -184,7 +183,7 @@ public FreeMarkerViewResolver freeMarkerViewResolver() {
184183
public FreeMarkerConfigurer freeMarkerConfigurer() {
185184
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
186185
configurer.setTemplateLoaderPath("/WEB-INF/");
187-
configurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
186+
configurer.setDefaultEncoding(UTF_8);
188187
return configurer;
189188
}
190189
}

0 commit comments

Comments
 (0)