org.springframework.ui.freemarker
Class FreeMarkerConfigurationFactory

java.lang.Object
  extended byorg.springframework.ui.freemarker.FreeMarkerConfigurationFactory
Direct Known Subclasses:
FreeMarkerConfigurationFactoryBean, FreeMarkerConfigurer

public class FreeMarkerConfigurationFactory
extends Object

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.

Since:
3/3/2004
Author:
Darren Davison, Juergen Hoeller
See Also:
setConfigLocation(org.springframework.core.io.Resource), setFreemarkerSettings(java.util.Properties), setFreemarkerVariables(java.util.Map), setTemplateLoaderPath(java.lang.String), createConfiguration()

Field Summary
protected  Log logger
           
 
Constructor Summary
FreeMarkerConfigurationFactory()
           
 
Method Summary
 Configuration createConfiguration()
          Prepare the FreeMarker Configuration and return it.
protected  Configuration newConfiguration()
          Return a new Configuration object.
protected  void postProcessConfiguration(Configuration config)
          To be implemented by subclasses that want to to perform custom post-processing of the Configuration object after this FactoryBean performed its default initialization.
 void setConfigLocation(Resource resource)
          Set the location of the FreeMarker config file.
 void setFreemarkerSettings(Properties settings)
          Set properties that contain well-known FreeMarker keys which will be passed to FreeMarker's Configuration.setSettings method.
 void setFreemarkerVariables(Map variables)
          Set a Map that contains well-known FreeMarker objects which will be passed to FreeMarker's Configuration.setAllSharedVariables() method.
 void setPreferFileSystemAccess(boolean preferFileSystemAccess)
          Set whether to prefer file system access for template loading.
 void setResourceLoader(ResourceLoader resourceLoader)
          Set the Spring ResourceLoader to use for loading FreeMarker template files.
 void setTemplateLoaderPath(String templateLoaderPath)
          Set the Freemarker template loader path via a Spring resource location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

FreeMarkerConfigurationFactory

public FreeMarkerConfigurationFactory()
Method Detail

setConfigLocation

public void setConfigLocation(Resource resource)
Set the location of the FreeMarker config file. Alternatively, you can specify all setting locally.

See Also:
setFreemarkerSettings(java.util.Properties), setTemplateLoaderPath(java.lang.String)

setFreemarkerSettings

public void setFreemarkerSettings(Properties settings)
Set properties that contain well-known FreeMarker keys which will be passed to FreeMarker's Configuration.setSettings method.

See Also:
Configurable.setSettings(java.util.Properties)

setFreemarkerVariables

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

See Also:
Configuration.setAllSharedVariables(freemarker.template.TemplateHashModelEx)

setTemplateLoaderPath

public void setTemplateLoaderPath(String templateLoaderPath)
Set the Freemarker template loader path via a Spring resource location.

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 the 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.

See Also:
ResourceEditor, ResourceLoader.getResource(java.lang.String), Configuration.setDirectoryForTemplateLoading(java.io.File), SpringTemplateLoader

setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
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.

See Also:
DefaultResourceLoader

setPreferFileSystemAccess

public void setPreferFileSystemAccess(boolean preferFileSystemAccess)
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.

See Also:
setTemplateLoaderPath(java.lang.String)

createConfiguration

public Configuration createConfiguration()
                                  throws IOException,
                                         TemplateException
Prepare the FreeMarker Configuration and return it.

Returns:
the FreeMarker Configuration object
Throws:
IOException - if the config file wasn't found
TemplateException - on FreeMarker initialization failure

newConfiguration

protected Configuration newConfiguration()
                                  throws IOException,
                                         TemplateException
Return a new Configuration object. Subclasses can override this for custom initialization, or for using a mock object for testing. Called by createConfiguration.

Returns:
the Configuration object
Throws:
IOException - if a config file wasn't found
TemplateException - on FreeMarker initialization failure
See Also:
createConfiguration()

postProcessConfiguration

protected void postProcessConfiguration(Configuration config)
                                 throws IOException,
                                        TemplateException
To be implemented by subclasses that want to to perform custom post-processing of the Configuration object after this FactoryBean performed its default initialization. Called by createConfiguration.

Parameters:
config - the current Configuration object
Throws:
IOException - if a config file wasn't found
TemplateException - on FreeMarker initialization failure
See Also:
createConfiguration()


Copyright (C) 2003-2004 The Spring Framework Project.