1. What is Freemarker
FreeMarker(Volecity,Beetl) is a template engine: a generic tool that is based on templates and data to be changed and used to generate output text (HTML web pages, e-mail, configuration files, source code, and so on). It's not for end users, it's a Java class library, a component that programmers can embed in the products they develop.
The template is written as FreeMarker Template Language (FTL). It is a simple, specialized language, not a mature programming language like PHP. That means preparing the data for display in a real programming language, such as database queries and business operations, and then the template displays the prepared data. In a template, you can focus on how the data is presented, while outside the template, you can focus on what data is being displayed.
This is often referred to as the MVC (Model View Controller) mode and is a particularly popular mode for dynamic web pages. It helps separate web designers (HTML designers) from developers (Java programmers). Designers don't have to face the complex logic in the template, but they can also modify the style of the page without a programmer to modify or recompile the code.
FreeMarker was originally designed to generate HTML pages in a Web development framework in MVC mode and was not bound to a Servlet or HTML or anything Web related. It can also be used in non-Web application environments.
FreeMarker is free and released based on the Apache license version 2.0.
2. Application scenarios for Freemarker
-
Page Statization
That is, dynamic web pages are displayed in a static form. Why do you want to static the page of your website? For example, for the commodity details page of e-commerce (B2C) website, there are at least a few million commodities, each commodity has a lot of information. If you do page static processing, it can reduce the pressure on the server and database, help search engines to collect, improve the stability of your website, and improve the access speed of your website. Principle: Users send requests to the server, which dynamically generates corresponding html pages based on the requested information. The next request does not need to query the database again, but simply returns the last generated html page.
Note: Page statization must be done on pages where data changes are infrequent (e.g. details of a news item, details of a commodity.) -
Code Generator (Improve Development Efficiency)
Using freemarker, you can implement your own code generator based on your company's technical architecture, avoiding duplicate code writing, and thus improving the efficiency of developers in their daily development work. Automatically generate Model files, DAO files, Mapper files for MyBatis, Service files, Controller files, etc. based on database tables, including basic add/delete checks, paging, search queries, etc. interface code automatically generated -
Send activation mail to user after user registration
-
Send a reminder message to the user after the user changes the password, etc.
-
. . .
3. Use of Freemarker
This example uses freemarker to generate a static html page
- Create a new maven project, and in pom. Introducing freemarker dependencies into XML
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency>
- Create a new resource directory under the project, a new template directory under the resource directory to store the template files, and a new test under the template directory. FTL template file
<html> <head></head> <body> Welcome, ${username} You are today the first ${user.loginCount}Log on to this website again Last logon time was ${user.lastLoginTime?string("yyyy/MM/dd HH:mm:ss")} </body> </html>
- Writing test classes
public class FreemarkerTest { public static void main(String[] args) throws IOException { Writer writer = null; try { //Create a configuration object Configuration cfg = new Configuration(); //Set the directory where the template is located cfg.setClassForTemplateLoading(FreemarkerTest.class,"/template"); //Set Character Set cfg.setDefaultEncoding("utf-8"); //Load Template File Template template = cfg.getTemplate("test.ftl", "utf-8"); //Create a data model Map dataMap = new HashMap(); dataMap.put("username","Zhang San"); Map userMap = new HashMap(); userMap.put("loginCount",3); userMap.put("lastLoginTime",new Date()); dataMap.put("user",userMap); //Create a Writer object writer = new OutputStreamWriter( new FileOutputStream("C:\\Users\\zhaolh\\Desktop\\test.html")); //Call the process method of the template to populate the data model in the template file and output to the specified file template.process(dataMap,writer); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } finally { writer.close(); } } }
- Open the generated html file