Skip to content

Commit d133ab6

Browse files
committed
Improve documentation regarding encoding in FreeMarker support
This commit also introduces integration tests to test the status quo regarding encoding. Closes gh-33071
1 parent 5d6e143 commit d133ab6

File tree

17 files changed

+681
-280
lines changed

17 files changed

+681
-280
lines changed

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

+73-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -41,9 +41,11 @@
4141
import org.springframework.util.CollectionUtils;
4242

4343
/**
44-
* Factory that configures a FreeMarker Configuration. Can be used standalone, but
45-
* typically you will either use FreeMarkerConfigurationFactoryBean for preparing a
46-
* Configuration as bean reference, or FreeMarkerConfigurer for web views.
44+
* Factory that configures a FreeMarker {@link Configuration}.
45+
*
46+
* <p>Can be used standalone, but typically you will either use
47+
* {@link FreeMarkerConfigurationFactoryBean} for preparing a {@code Configuration}
48+
* as a bean reference, or {@code FreeMarkerConfigurer} for web views.
4749
*
4850
* <p>The optional "configLocation" property sets the location of a FreeMarker
4951
* properties file, within the current application. FreeMarker properties can be
@@ -52,17 +54,18 @@
5254
* subject to constraints set by FreeMarker.
5355
*
5456
* <p>The "freemarkerVariables" property can be used to specify a Map of
55-
* shared variables that will be applied to the Configuration via the
57+
* shared variables that will be applied to the {@code Configuration} via the
5658
* {@code setAllSharedVariables()} method. Like {@code setSettings()},
5759
* these entries are subject to FreeMarker constraints.
5860
*
5961
* <p>The simplest way to use this class is to specify a "templateLoaderPath";
6062
* FreeMarker does not need any further configuration then.
6163
*
62-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
64+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
6365
*
6466
* @author Darren Davison
6567
* @author Juergen Hoeller
68+
* @author Sam Brannen
6669
* @since 03.03.2004
6770
* @see #setConfigLocation
6871
* @see #setFreemarkerSettings
@@ -107,7 +110,7 @@ public class FreeMarkerConfigurationFactory {
107110

108111
/**
109112
* Set the location of the FreeMarker config file.
110-
* Alternatively, you can specify all setting locally.
113+
* <p>Alternatively, you can specify all settings locally.
111114
* @see #setFreemarkerSettings
112115
* @see #setTemplateLoaderPath
113116
*/
@@ -134,25 +137,33 @@ public void setFreemarkerVariables(Map<String, Object> variables) {
134137
}
135138

136139
/**
137-
* Set the default encoding for the FreeMarker configuration.
138-
* If not specified, FreeMarker will use the platform file encoding.
139-
* <p>Used for template rendering unless there is an explicit encoding specified
140-
* for the rendering process (for example, on Spring's FreeMarkerView).
140+
* Set the default encoding for the FreeMarker {@link Configuration}, which
141+
* is used to decode byte sequences to character sequences when reading template
142+
* files.
143+
* <p>If not specified, FreeMarker will read template files using the platform
144+
* file encoding (defined by the JVM system property {@code file.encoding})
145+
* or {@code "utf-8"} if the platform file encoding is undefined.
146+
* <p>Note that the encoding is not used for template rendering. Instead, an
147+
* explicit encoding must be specified for the rendering process &mdash; for
148+
* example, via Spring's {@code FreeMarkerView} or {@code FreeMarkerViewResolver}.
141149
* @see freemarker.template.Configuration#setDefaultEncoding
142150
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView#setEncoding
151+
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView#setContentType
152+
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setContentType
143153
*/
144154
public void setDefaultEncoding(String defaultEncoding) {
145155
this.defaultEncoding = defaultEncoding;
146156
}
147157

148158
/**
149-
* Set a List of {@code TemplateLoader}s that will be used to search
150-
* for templates. For example, one or more custom loaders such as database
151-
* loaders could be configured and injected here.
152-
* <p>The {@link TemplateLoader TemplateLoaders} specified here will be
153-
* registered <i>before</i> the default template loaders that this factory
154-
* registers (such as loaders for specified "templateLoaderPaths" or any
155-
* loaders registered in {@link #postProcessTemplateLoaders}).
159+
* Set a List of {@link TemplateLoader TemplateLoaders} that will be used to
160+
* search for templates.
161+
* <p>For example, one or more custom loaders such as database loaders could
162+
* be configured and injected here.
163+
* <p>The {@code TemplateLoaders} specified here will be registered <i>before</i>
164+
* the default template loaders that this factory registers (such as loaders
165+
* for specified "templateLoaderPaths" or any loaders registered in
166+
* {@link #postProcessTemplateLoaders}).
156167
* @see #setTemplateLoaderPaths
157168
* @see #postProcessTemplateLoaders
158169
*/
@@ -161,13 +172,14 @@ public void setPreTemplateLoaders(TemplateLoader... preTemplateLoaders) {
161172
}
162173

163174
/**
164-
* Set a List of {@code TemplateLoader}s that will be used to search
165-
* for templates. For example, one or more custom loaders such as database
166-
* loaders can be configured.
167-
* <p>The {@link TemplateLoader TemplateLoaders} specified here will be
168-
* registered <i>after</i> the default template loaders that this factory
169-
* registers (such as loaders for specified "templateLoaderPaths" or any
170-
* loaders registered in {@link #postProcessTemplateLoaders}).
175+
* Set a List of {@link TemplateLoader TemplateLoaders} that will be used to
176+
* search for templates.
177+
* <p>For example, one or more custom loaders such as database loaders could
178+
* be configured and injected here.
179+
* <p>The {@code TemplateLoaders} specified here will be registered <i>after</i>
180+
* the default template loaders that this factory registers (such as loaders
181+
* for specified "templateLoaderPaths" or any loaders registered in
182+
* {@link #postProcessTemplateLoaders}).
171183
* @see #setTemplateLoaderPaths
172184
* @see #postProcessTemplateLoaders
173185
*/
@@ -177,7 +189,7 @@ public void setPostTemplateLoaders(TemplateLoader... postTemplateLoaders) {
177189

178190
/**
179191
* Set the Freemarker template loader path via a Spring resource location.
180-
* See the "templateLoaderPaths" property for details on path handling.
192+
* <p>See the "templateLoaderPaths" property for details on path handling.
181193
* @see #setTemplateLoaderPaths
182194
*/
183195
public void setTemplateLoaderPath(String templateLoaderPath) {
@@ -188,48 +200,49 @@ public void setTemplateLoaderPath(String templateLoaderPath) {
188200
* Set multiple Freemarker template loader paths via Spring resource locations.
189201
* <p>When populated via a String, standard URLs like "file:" and "classpath:"
190202
* pseudo URLs are supported, as understood by ResourceEditor. Allows for
191-
* relative paths when running in an ApplicationContext.
192-
* <p>Will define a path for the default FreeMarker template loader.
193-
* If a specified resource cannot be resolved to a {@code java.io.File},
194-
* a generic SpringTemplateLoader will be used, without modification detection.
195-
* <p>To enforce the use of SpringTemplateLoader, i.e. to not resolve a path
196-
* as file system resource in any case, turn off the "preferFileSystemAccess"
203+
* relative paths when running in an {@code ApplicationContext}.
204+
* <p>Will define a path for the default FreeMarker template loader. If a
205+
* specified resource cannot be resolved to a {@code java.io.File}, a generic
206+
* {@link SpringTemplateLoader} will be used, without modification detection.
207+
* <p>To enforce the use of {@code SpringTemplateLoader}, i.e. to not resolve
208+
* a path as file system resource in any case, turn off the "preferFileSystemAccess"
197209
* flag. See the latter's javadoc for details.
198210
* <p>If you wish to specify your own list of TemplateLoaders, do not set this
199-
* property and instead use {@code setTemplateLoaders(List templateLoaders)}
211+
* property and instead use {@link #setPostTemplateLoaders(TemplateLoader...)}.
200212
* @see org.springframework.core.io.ResourceEditor
201213
* @see org.springframework.context.ApplicationContext#getResource
202214
* @see freemarker.template.Configuration#setDirectoryForTemplateLoading
203215
* @see SpringTemplateLoader
216+
* @see #setPreferFileSystemAccess(boolean)
204217
*/
205218
public void setTemplateLoaderPaths(String... templateLoaderPaths) {
206219
this.templateLoaderPaths = templateLoaderPaths;
207220
}
208221

209222
/**
210-
* Set the Spring ResourceLoader to use for loading FreeMarker template files.
211-
* The default is DefaultResourceLoader. Will get overridden by the
212-
* ApplicationContext if running in a context.
223+
* Set the {@link ResourceLoader} to use for loading FreeMarker template files.
224+
* <p>The default is {@link DefaultResourceLoader}. Will get overridden by the
225+
* {@code ApplicationContext} if running in a context.
213226
* @see org.springframework.core.io.DefaultResourceLoader
214227
*/
215228
public void setResourceLoader(ResourceLoader resourceLoader) {
216229
this.resourceLoader = resourceLoader;
217230
}
218231

219232
/**
220-
* Return the Spring ResourceLoader to use for loading FreeMarker template files.
233+
* Return the {@link ResourceLoader} to use for loading FreeMarker template files.
221234
*/
222235
protected ResourceLoader getResourceLoader() {
223236
return this.resourceLoader;
224237
}
225238

226239
/**
227240
* Set whether to prefer file system access for template loading.
228-
* File system access enables hot detection of template changes.
241+
* <p>File system access enables hot detection of template changes.
229242
* <p>If this is enabled, FreeMarkerConfigurationFactory will try to resolve
230243
* the specified "templateLoaderPath" as file system resource (which will work
231244
* for expanded class path resources and ServletContext resources too).
232-
* <p>Default is "true". Turn this off to always load via SpringTemplateLoader
245+
* <p>Default is "true". Turn this off to always load via {@link SpringTemplateLoader}
233246
* (i.e. as stream, without hot detection of template changes), which might
234247
* be necessary if some of your templates reside in an expanded classes
235248
* directory while others reside in jar files.
@@ -248,8 +261,8 @@ protected boolean isPreferFileSystemAccess() {
248261

249262

250263
/**
251-
* Prepare the FreeMarker Configuration and return it.
252-
* @return the FreeMarker Configuration object
264+
* Prepare the FreeMarker {@link Configuration} and return it.
265+
* @return the FreeMarker {@code Configuration} object
253266
* @throws IOException if the config file wasn't found
254267
* @throws TemplateException on FreeMarker initialization failure
255268
*/
@@ -314,11 +327,12 @@ public Configuration createConfiguration() throws IOException, TemplateException
314327
}
315328

316329
/**
317-
* Return a new Configuration object. Subclasses can override this for custom
318-
* initialization (e.g. specifying a FreeMarker compatibility level which is a
319-
* new feature in FreeMarker 2.3.21), or for using a mock object for testing.
320-
* <p>Called by {@code createConfiguration()}.
321-
* @return the Configuration object
330+
* Return a new {@link Configuration} object.
331+
* <p>Subclasses can override this for custom initialization &mdash; for example,
332+
* to specify a FreeMarker compatibility level (which is a new feature in
333+
* FreeMarker 2.3.21), or to use a mock object for testing.
334+
* <p>Called by {@link #createConfiguration()}.
335+
* @return the {@code Configuration} object
322336
* @throws IOException if a config file wasn't found
323337
* @throws TemplateException on FreeMarker initialization failure
324338
* @see #createConfiguration()
@@ -328,11 +342,11 @@ protected Configuration newConfiguration() throws IOException, TemplateException
328342
}
329343

330344
/**
331-
* Determine a FreeMarker TemplateLoader for the given path.
332-
* <p>Default implementation creates either a FileTemplateLoader or
333-
* a SpringTemplateLoader.
345+
* Determine a FreeMarker {@link TemplateLoader} for the given path.
346+
* <p>Default implementation creates either a {@link FileTemplateLoader} or
347+
* a {@link SpringTemplateLoader}.
334348
* @param templateLoaderPath the path to load templates from
335-
* @return an appropriate TemplateLoader
349+
* @return an appropriate {@code TemplateLoader}
336350
* @see freemarker.cache.FileTemplateLoader
337351
* @see SpringTemplateLoader
338352
*/
@@ -366,9 +380,9 @@ protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
366380

367381
/**
368382
* To be overridden by subclasses that want to register custom
369-
* TemplateLoader instances after this factory created its default
383+
* {@link TemplateLoader} instances after this factory created its default
370384
* template loaders.
371-
* <p>Called by {@code createConfiguration()}. Note that specified
385+
* <p>Called by {@link #createConfiguration()}. Note that specified
372386
* "postTemplateLoaders" will be registered <i>after</i> any loaders
373387
* registered by this callback; as a consequence, they are <i>not</i>
374388
* included in the given List.
@@ -381,10 +395,10 @@ protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders)
381395
}
382396

383397
/**
384-
* Return a TemplateLoader based on the given TemplateLoader list.
385-
* If more than one TemplateLoader has been registered, a FreeMarker
386-
* MultiTemplateLoader needs to be created.
387-
* @param templateLoaders the final List of TemplateLoader instances
398+
* Return a {@link TemplateLoader} based on the given {@code TemplateLoader} list.
399+
* <p>If more than one TemplateLoader has been registered, a FreeMarker
400+
* {@link MultiTemplateLoader} will be created.
401+
* @param templateLoaders the final List of {@code TemplateLoader} instances
388402
* @return the aggregate TemplateLoader
389403
*/
390404
@Nullable
@@ -404,10 +418,10 @@ protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templat
404418

405419
/**
406420
* To be overridden by subclasses that want to perform custom
407-
* post-processing of the Configuration object after this factory
421+
* post-processing of the {@link Configuration} object after this factory
408422
* performed its default initialization.
409-
* <p>Called by {@code createConfiguration()}.
410-
* @param config the current Configuration object
423+
* <p>Called by {@link #createConfiguration()}.
424+
* @param config the current {@code Configuration} object
411425
* @throws IOException if a config file wasn't found
412426
* @throws TemplateException on FreeMarker initialization failure
413427
* @see #createConfiguration()

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,25 @@
2727
import org.springframework.lang.Nullable;
2828

2929
/**
30-
* Factory bean that creates a FreeMarker Configuration and provides it as
31-
* bean reference. This bean is intended for any kind of usage of FreeMarker
32-
* in application code, e.g. for generating email content. For web views,
33-
* FreeMarkerConfigurer is used to set up a FreeMarkerConfigurationFactory.
34-
* <p>
35-
* The simplest way to use this class is to specify just a "templateLoaderPath";
30+
* Factory bean that creates a FreeMarker {@link Configuration} and provides it
31+
* as a bean reference.
32+
*
33+
* <p>This bean is intended for any kind of usage of FreeMarker in application
34+
* code &mdash; for example, for generating email content. For web views,
35+
* {@code FreeMarkerConfigurer} is used to set up a {@link FreeMarkerConfigurationFactory}.
36+
*
37+
* <p>The simplest way to use this class is to specify just a "templateLoaderPath";
3638
* you do not need any further configuration then. For example, in a web
3739
* application context:
3840
*
3941
* <pre class="code"> &lt;bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"&gt;
4042
* &lt;property name="templateLoaderPath" value="/WEB-INF/freemarker/"/&gt;
4143
* &lt;/bean&gt;</pre>
42-
43-
* See the base class FreeMarkerConfigurationFactory for configuration details.
4444
*
45-
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
45+
* <p>See the {@link FreeMarkerConfigurationFactory} base class for configuration
46+
* details.
47+
*
48+
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
4649
*
4750
* @author Darren Davison
4851
* @since 03.03.2004

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

+6-5
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.
@@ -24,7 +24,8 @@
2424

2525
/**
2626
* Utility class for working with FreeMarker.
27-
* Provides convenience methods to process a FreeMarker template with a model.
27+
*
28+
* <p>Provides convenience methods to process a FreeMarker template with a model.
2829
*
2930
* @author Juergen Hoeller
3031
* @since 14.03.2004
@@ -33,12 +34,12 @@ public abstract class FreeMarkerTemplateUtils {
3334

3435
/**
3536
* Process the specified FreeMarker template with the given model and write
36-
* the result to the given Writer.
37-
* <p>When using this method to prepare a text for a mail to be sent with Spring's
37+
* the result to a String.
38+
* <p>When using this method to prepare text for a mail to be sent with Spring's
3839
* mail support, consider wrapping IO/TemplateException in MailPreparationException.
3940
* @param model the model object, typically a Map that contains model names
4041
* as keys and model objects as values
41-
* @return the result as String
42+
* @return the result as a String
4243
* @throws IOException if the template wasn't found or couldn't be read
4344
* @throws freemarker.template.TemplateException if rendering failed
4445
* @see org.springframework.mail.MailPreparationException

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -29,9 +29,11 @@
2929
import org.springframework.lang.Nullable;
3030

3131
/**
32-
* FreeMarker {@link TemplateLoader} adapter that loads via a Spring {@link ResourceLoader}.
33-
* Used by {@link FreeMarkerConfigurationFactory} for any resource loader path that cannot
34-
* be resolved to a {@link java.io.File}.
32+
* FreeMarker {@link TemplateLoader} adapter that loads template files via a
33+
* Spring {@link ResourceLoader}.
34+
*
35+
* <p>Used by {@link FreeMarkerConfigurationFactory} for any resource loader path
36+
* that cannot be resolved to a {@link java.io.File}.
3537
*
3638
* @author Juergen Hoeller
3739
* @since 14.03.2004
@@ -48,7 +50,7 @@ public class SpringTemplateLoader implements TemplateLoader {
4850

4951

5052
/**
51-
* Create a new SpringTemplateLoader.
53+
* Create a new {@code SpringTemplateLoader}.
5254
* @param resourceLoader the Spring ResourceLoader to use
5355
* @param templateLoaderPath the template loader path to use
5456
*/

0 commit comments

Comments
 (0)