API / Apache FreeMarker Interview questions
Explain the execution flow of template processing in FreeMarker?
Processing a template always follows the same sequence, whether it happens once in a script or thousands of times per second in a web app:
- An application-wide
Configurationis created (or reused) with aTemplateLoaderand formatting settings. configuration.getTemplate("name.ftl", locale)is called. If that template is already in the cache and still fresh, the cached compiledTemplate(an AST) is returned; otherwise the loader reads the source, FreeMarker parses it into an AST, and the result is cached.- Application code builds a data model, typically a
Mapor POJO, and wraps it with anObjectWrapperso FreeMarker can traverse it. template.process(dataModel, writer)is called. This creates a fresh, per-callEnvironment, walks the AST, evaluates each interpolation and directive against the data model, and writes text to theWriteras it goes.
The Template object itself is reusable and thread-safe once compiled; only the Environment created for each process() call carries per-request state, which is what makes it safe to serve many concurrent requests from one cached template.
More Related questions...