Use freemarker to merge values in the data model into the Template File

Source: Internet
Author: User

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 );

        }

   }

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.