Freemarker The simplest example of a program _java

Source: Internet
Author: User

Freemarker The simplest example of a program

Freemarker-2.3.18.tar.gz

Http://cdnetworks-kr-1.dl.sourceforge.net/project/freemarker/freemarker/2.3.18/freemarker-2.3.18.tar.gz


Freemarker-2.3.13.jar:

Link: http://pan.baidu.com/s/1eQVl9Zk Password: izs5

1. Create template objects by string, and perform interpolation processing

After execution, the console outputs the results:

Import freemarker.template.Template; 
Import Java.io.OutputStreamWriter; 
Import Java.io.StringReader; 
Import Java.util.HashMap; 
Import Java.util.Map; 
/** * Freemarker Simplest example * * @author leizhimin 11-11-17 Morning 10:32 * * Public
 class Test2 {public  
static void main (Stri Ng[] args) 
throws exception{   //Create a Template object   
Template t = new Template (null, new StringReader ("Username: ${user}; URL: ${url}; Name: ${name} "), null);  
 Create the interpolated map map   
= new HashMap ();   
Map.put ("User", "Lavasoft");   
Map.put ("url", "http://www.baidu.com/");  
 Map.put ("name", "Baidu");  
 Executes the interpolation and outputs to the specified output stream  
 t.process (map, new OutputStreamWriter (System.out));  }

User name: Lavasoft; url:http://www.baidu.com/;
Name: Baidu Process finished with exit code 0

2, through the file to create template objects, and perform interpolation operations

 import freemarker.template.Configuration; 
Import Freemarker.template.Template; 
Import Java.io.File; 
Import Java.io.OutputStreamWriter; 
Import Java.util.HashMap; 
Import Java.util.Map; 
/** * Freemarker Simplest example * * @author leizhimin 11-11-14 pm 2:44 * * Public class Test {private Configuration cfg;
 Template Configuration Object public void init () throws Exception {//Initialize Freemarker configuration//Create a Configuration instance cfg = new Configuration (); Sets the template folder location cfg.setdirectoryfortemplateloading (new File ("G:\\testprojects\\freemarkertest\\src") freemarker); 
 The public void process () throws Exception {//constructs the map map map of the populated data = new HashMap (); Map.put ("User", "Lavasoft"); 
 Map.put ("url", "http://www.baidu.com/"); Map.put ("name", "Baidu"); 
Create a Template object Template t = cfg.gettemplate ("TEST.FTL"); Performs an interpolation operation on the stencil and outputs the t.process (map, new OutputStreamWriter (System.out)) to the established output stream;
public static void Main (string[] args) throws Exception {Test HF = new test (); Hf.init (); hf.process ();}} 

Create a template file Test.ftl

 
 

After execution, the console outputs the following results:

 
 


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.