Examples of using Freemarker in Java Engineering

Source: Internet
Author: User

New Java project, introducing Freemarker.jar, the version of this project: freemarker-2.3.20 version

The Java Engineering catalog is as follows:

TEST.FTL file

HTML code
    1. Name: ${name}
    2. Age: ${age}

Test class

Java code
  1. Package com.freemarker;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import Java.io.OutputStreamWriter;
  5. Import Java.io.Writer;
  6. Import Java.util.HashMap;
  7. Import Java.util.Map;
  8. Import freemarker.template.Configuration;
  9. Import Freemarker.template.Template;
  10. Import freemarker.template.TemplateException;
  11. Public class Test {
  12. public static void Main (string[] args) throws IOException, templateexception {
  13. //1. Creating a configuration instance cofiguration
  14. Configuration cfg = new configuration ();
  15. //2. Setting the template file directory
  16. //(1) directory under SRC directory (template under SRC)
  17. //cfg.setdirectoryfortemplateloading (New File ("Src/template"));
  18. //(2) Full path (template under SRC)
  19. //cfg.setdirectoryfortemplateloading (New File (
  20. //"d:/cpic-env/workspace/javafreemarker/src/template"));
  21. //cfg.setdirectoryfortemplateloading (New File ("Src/template"));
  22. //(3) catalogue under the Engineering catalogue (Template/main under Engineering)--recommended
  23. Cfg.setdirectoryfortemplateloading (new File ("Template/main"));
  24. //cfg.setobjectwrapper (New Defaultobjectwrapper ());
  25. //Get Templates (template)
  26. Template template = Cfg.gettemplate ("TEST.FTL");
  27. //Build Data Model (MAP)
  28. map<string, string> root = new hashmap<string, string> ();
  29. Root.put ("name", "CXL");
  30. Root.put ("age", "25");
  31. //Get output stream (specified to console (standard output))
  32. Writer out = new OutputStreamWriter (System.out);
  33. //stringwriter out = new StringWriter ();
  34. //system.out.println (out.tostring ());
  35. //Data and template merging (data + template = output)
  36. Template.process (root, out);
  37. Out.flush ();
  38. }
  39. }

Operation Result:

Java code
    1. Name:cxl
    2. Age:


Examples of using Freemarker in Java Engineering

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.