Java template engine FreeMarker

Source: Internet
Author: User

Concept
FreeMarker is a template engine, a common tool for generating text output based on templates, which is written in Java only. It is an SDK provided for Java programmers. It is not intended for end users, but an application provided for programmers to embed the products they develop.

Introduction
So what kind of tool is FreeMarker? FreeMarker is actually designed to generate HTML Web pages, especially through the implementation of Java Servlet applications based on the MVC pattern. The dynamic page design concept using the MVC pattern allows you to separate front-end designers from programmers. Each person performs his/her duties and plays his/her best role.
Web designers can rewrite the page display effect without being influenced by the programmer's code compilation, because the logic of the program and the page design have been separated. The page template code will not be affected by the complex program code. This idea of separation is useful even for projects where programmers and page designers are the same person, because separation keeps code concise and easy to maintain.
Although FreeMarker also has some programming capabilities, the data to be displayed is usually prepared by the Java program, and the page is generated by FreeMarker to display the prepared data (such as) through the template ).
VcTjtcSy + sa31tCjrMfhwb + examples/examples + tdjOxLz + oaLK/b7dv + examples/examples + x7 + examples + CgrD/examples/J0tTKudPDuLTU07HttO/Tables/vX1r/examples + authorization + 9fWs + XNuwo8YnI + response/response ++ response/response + Cgo8dWw + response/OjrEphdmG21M/zzai5/response + response/zo6i907/response + response /qrei1d/records + Cgo8dWw + records/4go8YnI + records/records + Cgo8dWw + records + rzKu6 + 6zbG + tdi7rwrK/records + records + expires/expires + Cgq3x1VT19a3 + expires/D + expires/expires + Wz/rrN1rG + expires/expires + Cgo8YnI + Cgo8aDE + 08 XKxjwvaDE + Cgo8dWw + cjxsaT6/users + qt6K5/users + a1xm?n23x7oju + users/bPM1tC + users/users + 3dTa0rPD5tbQtcSx7c/users + users/users + yv2 + users + examples/examples + examples/examples + qt6K5/examples + 1zbOjrLHIyOdPQbmk1/fB98 + examples/examples + examples/W0sC + examples/LzTw/fItwrS1M3508NKU1DP1Mq + yv2 + cores +/cores + cores/R + cores/uxL/cores/ycTc1eK49rmk1/e + cores/ycTcvNO088Sz0ru3vb + cores/fOqr3nw + a/strong + oaLNvM/strong + a1xMbky/u/ycrTu6 + weight/weight + Cgo8YnI + Cgo8aDE + weight + Cgo8dWw + vOjrL/JxNy74b + weight a686OsyOe5 + release/release + release/rsdzD4rTtzvO + release + NDQxdC2zqOsyOe5 + 7bUw7/release/vK/release + release/release + gz9s/release/uxL + release/wrzPwgo8YnI + release + bP2wLSjrLfWz + 3Su8/release + sLrCjxwcmUgY2xhc3M9 "brush: java; "> import java. io. bufferedWriter; import java. io. file; import java. io. fileWriter; import java. io. IOException; import java. io. printWriter; import java. util. hashMap; import java. util. map; import freemarker. template. configuration; import freemarker. template. defaultObjectWrapper; import freemarker. template. template; import freemarker. template. templateException; import freemarker. template. templateExceptionHandler; import junit. framework. testCase; public class FreemarkerTest extends TestCase {private String dir = "E :/... /OA/TestTotal/src/com/bjsxt/oa/freemarker "; public void testFreemarker () {Configuration cfg = new Configuration (); try {// Where to load the template file cfg. setDirectoryForTemplateLoading (new File (dir); // defines the template location. Load the template from the class path relative to the path of FreemarkerManager. // cfg. setTemplateLoader (new ClassTemplateLoader (FreemarkerManager. class, "templates") // sets the object wrapper cfg. setObjectWrapper (new DefaultObjectWrapper (); // sets the exception processor cfg. setTemplateExceptionHandler (TemplateExceptionHandler. IGNORE_HANDLER); // defines the data model Map root = new HashMap (); root. put ("abc", "World, hello"); // use freemarker to explain the Template. First, you need to obtain the Template object template = cfg. getTemplate ("test. ftl "); // output after template interpretation is complete PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter (dir +"/out.txt "))); try {// explain the template. process (root, out);} catch (TemplateException e) {e. printStackTrace () ;}} catch (IOException e) {e. printStackTrace ();}}}

The following is the Defined Template test. ftl.

Test. flt code

First test program: $ {abc}


The final running result is as follows:

Output The out.txt file. The content in out.txt is as follows:
First test program: Hello, world



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.