Create a configuration instance

First you have to create a freemarker.template.Configuration instance and adjust its settings. A Configuration instance is a central place to store the application level settings of FreeMarker. Also, it deals with the creation and caching of pre-parsed templates.

Probably you will do it only once at the beginning of the application (possibly servlet) life-cycle:

Configuration cfg = new Configuration();

// Specify the data source where the template files come from. Here I set a
// plain directory for it, but non-file-system are possible too:
cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));

// Specify how templates will see the data-model. This is an advanced topic...
// for now just use this:
cfg.setObjectWrapper(new DefaultObjectWrapper());

// Set your preferred charset template files are stored in. UTF-8 is
// a good choice in most applications:
cfg.setDefaultEncoding("UTF-8");

// Sets how errors will appear. Here we assume we are developing HTML pages.
// For production systems TemplateExceptionHandler.RETHROW_HANDLER is better.
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);

// At least in new projects, specify that you want the fixes that aren't
// 100% backward compatible too (these are very low-risk changes as far as the
// 1st and 2nd version number remains):
cfg.setIncompatibleImprovements(new Version(2, 3, 20));  // FreeMarker 2.3.20  

From now you should use this single configuration instance (i.e., its a singleton). Note however that if a system has multiple independent components that use FreeMarker, then of course they will use their own private Configuration instances. Do not needlessly re-create configuration instances; it's expensive as you lose the caches.

FreeMarker Manual -- For FreeMarker 2.3.20
HTML generated: 2013-06-27 20:54:33 GMT
Edited with XMLMind XML Editor
Here!