The procedure is as follows:
① Create a configuration instance, which is responsible for managing the template loading path of freemarker and generating template instances.
② Use the configuration instance to generate a template instance and specify the template file to be used.
③ Fill the data model. The data model is a map object.
④ Call the process method of the template instance to complete the merge.
According to the above steps, the following provides a Java program that uses freemarker to create the output. The code of this program is as follows:
Public class hellofreemarker
{
// Manages the configuration instance of the freemarker Template File
Private configuration CFG;
// Initialize the configuration instance
Public void Init () throws exception
{
// Initialize freemarker Configuration
// Create a configuration instance
CFG = new configuration ();
// Set the location of the freemarker Template File
Cfg. setdirectoryfortemplateloading (new file ("templates "));
}
// The merge processing method
Public void process () throws exception
{
// Create a data model
Map root = new hashmap ();
Root. Put ("name", "freemarker! ");
Root. Put ("MSG", "you have completed the first freemarker instance! ");
// Use the configuration instance to load the specified Template
Template T = cfg. gettemplate ("test. Flt ");
// Process Merging
T. Process (root, new outputstreamwriter (system. Out ));
}
Public static void main (string [] ARGs) throws exception
{
Hellofreemarker HF = new hellofreemarker ();
HF. INIT ();
HF. Process ();
}
}
The code above creates a map instance root, which serves as the data model of the template file and stores two
Key-value pairs, where the first is name and the second is MSG. Both keys have corresponding values. These two values will be filled in the corresponding interpolation place in the template.
Although freemarker can be used in Java, freemarker is usually used to generate HTML pages.
The following describes how to use freemarker in a web application and use servlet to merge templates and data models. The following is the code in the servlet application:
Public class helloservlet extends httpservlet
{
// Manages the configuration instance of the freemarker Template File
Private configuration CFG;
// Initialize the configuration instance
Public void Init () throws exception
{
// Initialize freemarker Configuration
// Create a configuration instance
CFG = new configuration ();
// Set the location of the freemarker Template File
Cfg. setservletcontextfortemplateloading (getservletcontext (), "WEB-INF/templates ");
}
// Generate user response
Public void Service (httpservletrequest request, httpservletresponse response) throws
Servletexception, ioexception
{
// Create a data model
Map root = new hashmap ();
Root. Put ("message", "Hello freemarker! ");
// Use the configuration instance to load the specified template, that is, obtain the template file.
Template T = cfg. gettemplate ("test. Flt ");
// Start preparing to generate the output
// Use the charset of the template file as the charset of this page
// Use text/html and mime-type
Response. setcontenttype ("text/html; charset =" + T. getencoding ());
Writer out = response. getwriter ();
// Merge data models and templates and output the results to the out
Try
{
T. Process (root, out );
}
Catch (templateexception E)
{
Throw new servletexception ("error in processing template", e );
}
}
}