spring-framework / org.springframework.ui.freemarker / FreeMarkerConfigurationFactory

FreeMarkerConfigurationFactory

open class FreeMarkerConfigurationFactory

Factory that configures a FreeMarker Configuration. Can be used standalone, but typically you will either use FreeMarkerConfigurationFactoryBean for preparing a Configuration as bean reference, or FreeMarkerConfigurer for web views.

The optional "configLocation" property sets the location of a FreeMarker properties file, within the current application. FreeMarker properties can be overridden via "freemarkerSettings". All of these properties will be set by calling FreeMarker's Configuration.setSettings() method and are subject to constraints set by FreeMarker.

The "freemarkerVariables" property can be used to specify a Map of shared variables that will be applied to the Configuration via the setAllSharedVariables() method. Like setSettings(), these entries are subject to FreeMarker constraints.

The simplest way to use this class is to specify a "templateLoaderPath"; FreeMarker does not need any further configuration then.

Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.

Author
Darren Davison

Author
Juergen Hoeller

Since
03.03.2004

See Also
#setConfigLocation#setFreemarkerSettings#setFreemarkerVariables#setTemplateLoaderPath#createConfigurationFreeMarkerConfigurationFactoryBeanorg.springframework.web.servlet.view.freemarker.FreeMarkerConfigurerfreemarker.template.Configuration

Constructors

<init>

FreeMarkerConfigurationFactory()

Factory that configures a FreeMarker Configuration. Can be used standalone, but typically you will either use FreeMarkerConfigurationFactoryBean for preparing a Configuration as bean reference, or FreeMarkerConfigurer for web views.

The optional "configLocation" property sets the location of a FreeMarker properties file, within the current application. FreeMarker properties can be overridden via "freemarkerSettings". All of these properties will be set by calling FreeMarker's Configuration.setSettings() method and are subject to constraints set by FreeMarker.

The "freemarkerVariables" property can be used to specify a Map of shared variables that will be applied to the Configuration via the setAllSharedVariables() method. Like setSettings(), these entries are subject to FreeMarker constraints.

The simplest way to use this class is to specify a "templateLoaderPath"; FreeMarker does not need any further configuration then.

Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.

Functions

createConfiguration

open fun createConfiguration(): Configuration

Prepare the FreeMarker Configuration and return it.

setConfigLocation

open fun setConfigLocation(resource: Resource): Unit

Set the location of the FreeMarker config file. Alternatively, you can specify all setting locally.

setDefaultEncoding

open fun setDefaultEncoding(defaultEncoding: String): Unit

Set the default encoding for the FreeMarker configuration. If not specified, FreeMarker will use the platform file encoding.

Used for template rendering unless there is an explicit encoding specified for the rendering process (for example, on Spring's FreeMarkerView).

setFreemarkerSettings

open fun setFreemarkerSettings(settings: Properties): Unit

Set properties that contain well-known FreeMarker keys which will be passed to FreeMarker's Configuration.setSettings method.

setFreemarkerVariables

open fun setFreemarkerVariables(variables: MutableMap<String, Any>): Unit

Set a Map that contains well-known FreeMarker objects which will be passed to FreeMarker's Configuration.setAllSharedVariables() method.

setPostTemplateLoaders

open fun setPostTemplateLoaders(vararg postTemplateLoaders: TemplateLoader): Unit

Set a List of TemplateLoaders that will be used to search for templates. For example, one or more custom loaders such as database loaders can be configured.

The TemplateLoader specified here will be registered after the default template loaders that this factory registers (such as loaders for specified "templateLoaderPaths" or any loaders registered in #postProcessTemplateLoaders).

setPreTemplateLoaders

open fun setPreTemplateLoaders(vararg preTemplateLoaders: TemplateLoader): Unit

Set a List of TemplateLoaders that will be used to search for templates. For example, one or more custom loaders such as database loaders could be configured and injected here.

The TemplateLoader specified here will be registered before the default template loaders that this factory registers (such as loaders for specified "templateLoaderPaths" or any loaders registered in #postProcessTemplateLoaders).

setPreferFileSystemAccess

open fun setPreferFileSystemAccess(preferFileSystemAccess: Boolean): Unit

Set whether to prefer file system access for template loading. File system access enables hot detection of template changes.

If this is enabled, FreeMarkerConfigurationFactory will try to resolve the specified "templateLoaderPath" as file system resource (which will work for expanded class path resources and ServletContext resources too).

Default is "true". Turn this off to always load via SpringTemplateLoader (i.e. as stream, without hot detection of template changes), which might be necessary if some of your templates reside in an expanded classes directory while others reside in jar files.

setResourceLoader

open fun setResourceLoader(resourceLoader: ResourceLoader): Unit

Set the Spring ResourceLoader to use for loading FreeMarker template files. The default is DefaultResourceLoader. Will get overridden by the ApplicationContext if running in a context.

setTemplateLoaderPath

open fun setTemplateLoaderPath(templateLoaderPath: String): Unit

Set the Freemarker template loader path via a Spring resource location. See the "templateLoaderPaths" property for details on path handling.

setTemplateLoaderPaths

open fun setTemplateLoaderPaths(vararg templateLoaderPaths: String): Unit

Set multiple Freemarker template loader paths via Spring resource locations.

When populated via a String, standard URLs like "file:" and "classpath:" pseudo URLs are supported, as understood by ResourceEditor. Allows for relative paths when running in an ApplicationContext.

Will define a path for the default FreeMarker template loader. If a specified resource cannot be resolved to a java.io.File, a generic SpringTemplateLoader will be used, without modification detection.

To enforce the use of SpringTemplateLoader, i.e. to not resolve a path as file system resource in any case, turn off the "preferFileSystemAccess" flag. See the latter's javadoc for details.

If you wish to specify your own list of TemplateLoaders, do not set this property and instead use setTemplateLoaders(List templateLoaders)

Inheritors

FreeMarkerConfigurationFactoryBean

open class FreeMarkerConfigurationFactoryBean : FreeMarkerConfigurationFactory, FactoryBean<Configuration>, InitializingBean, ResourceLoaderAware

Factory bean that creates a FreeMarker Configuration and provides it as bean reference. This bean is intended for any kind of usage of FreeMarker in application code, e.g. for generating email content. For web views, FreeMarkerConfigurer is used to set up a FreeMarkerConfigurationFactory. The simplest way to use this class is to specify just a "templateLoaderPath"; you do not need any further configuration then. For example, in a web application context:

 <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> </bean>
See the base class FreeMarkerConfigurationFactory for configuration details.

Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.

FreeMarkerConfigurer

open class FreeMarkerConfigurer : FreeMarkerConfigurationFactory, FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware

JavaBean to configure FreeMarker for web usage, via the "configLocation" and/or "freemarkerSettings" and/or "templateLoaderPath" properties. The simplest way to use this class is to specify just a "templateLoaderPath"; you do not need any further configuration then.

 <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"><value>/WEB-INF/freemarker/</value></property> </bean>
This bean must be included in the application context of any application using Spring's FreeMarkerView for web MVC. It exists purely to configure FreeMarker. It is not meant to be referenced by application components but just internally by FreeMarkerView. Implements FreeMarkerConfig to be found by FreeMarkerView without depending on the bean name the configurer. Each DispatcherServlet can define its own FreeMarkerConfigurer if desired.

Note that you can also refer to a preconfigured FreeMarker Configuration instance, for example one set up by FreeMarkerConfigurationFactoryBean, via the "configuration" property. This allows to share a FreeMarker Configuration for web and email usage, for example.

This configurer registers a template loader for this package, allowing to reference the "spring.ftl" macro library (contained in this package and thus in spring.jar) like this:

 <#import "/spring.ftl" as spring/> <@spring.bind "person.age"/> age is ${spring.status.value}
Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.