|
Introducing FreeMarkerWhen you're writing servlets to generate dynamic web pages, how do you keep the HTML separate from your Java code, so that graphic designers can change the HTML without your having to change your classes? FreeMarker solves this problem by encapsulating HTML in templates. FreeMarker can be used for lot more than generating dynamic web pages. You can use it whenever your program needs to generate textual output based on templates. This includes many applications, ranging from generating bulk personalized e-mail messages to dynamic generation of templatized source code. FreeMarker - being a generic template engine - is a viable solution for all of these needs. For now, let's go back to our HTML example: The source code for a template is an HTML document that contains instructions for including dynamically-generated data. These instructions are simple and unobtrusive (so the graphic designers can still do their work), but powerful enough to let you use data structures of arbitrary complexity. It's easy to generate tables of data, and you can use "if/else" or "switch" statements to generate conditional HTML. Control structures (lists, if-else structures, and switch-case structures) can be nested as in any other programming language. A servlet or other Java object can construct an object of class
The data objects passed to a
That is, a method returning a scalar, a hash of scalars, and a list of hashes of scalars. A list of hashes of scalars could be used to represent rows of data from a relational database. After preparing the data model, the servlet can output HTML with a
method call on the // modelRoot is the root node of the data model // and out is a Writer template.process(modelRoot, out); Error messages generated by template syntax errors, and messages from exceptions thrown by objects in the data model, are inserted as comments in the HTML output, unless this behavior is overriden by an event handler. The FreeMarker classes you'll need to use directly are in the package
All instructions in the template language are case-sensitive. Note that FreeMarker is a tool that elegantly solves the problem of separating the presentation layer from the underlying business logic in a web-based application. On the other hand, there are problems typical of this space that it does not address. If you are looking for a complete MVC (model-view-controller) framework that interoperates seamlessly with FreeMarker, you might want to investigate The Niggle Web Application Framework. |
|