spring-framework / org.springframework.web.servlet.view.freemarker / FreeMarkerConfigurer

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.

Author
Darren Davison

Author
Rob Harrop

Since
03.03.2004

See Also
#setConfigLocation#setFreemarkerSettings#setTemplateLoaderPath#setConfigurationorg.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBeanFreeMarkerView

Constructors

<init>

FreeMarkerConfigurer()

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.

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

Initialize FreeMarkerConfigurationFactory's Configuration if not overridden by a preconfigured FreeMarker Configuation.

Sets up a ClassTemplateLoader to use for loading Spring macros.

getConfiguration

open fun getConfiguration(): Configuration

Return the Configuration object wrapped by this bean.

getTaglibFactory

open fun getTaglibFactory(): TaglibFactory

Return the TaglibFactory object wrapped by this bean.

setConfiguration

open fun setConfiguration(configuration: Configuration): Unit

Set a preconfigured Configuration to use for the FreeMarker web config, e.g. a shared one for web and email usage, set up via FreeMarkerConfigurationFactoryBean. If this is not set, FreeMarkerConfigurationFactory's properties (inherited by this class) have to be specified.

setServletContext

open fun setServletContext(servletContext: ServletContext): Unit

Initialize the TaglibFactory for the given ServletContext.

Inherited 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)