FreeMarker concept introduction (1), freemarker concept Introduction
1. What is FreeMarker?
It is a tool that generates text output based on templates and is written in java. It has nothing to do with specific containers and does not know HTTP or Servlet. Therefore, it can be applied in non-Web application environments. Just like a factory where raw materials (such as data and text) are handed over to FreeMarker, it will assemble these materials to generate a product we need.
It is a Java package and class library for java programmers. It is a template language and has its own syntax, but it runs independently from the servlet container. Therefore, it is said that it is a tool and has nothing to do with a specific container, allow programmers to embed it into our system.
2. How to work
The following figure is taken from the FreeMarker official website. This figure clearly describes how it generates a text.
It requires three parts, just like the example of the factory mentioned above. Raw materials, factories and products are required.
FreeMarker's official website shows that FreeMarker has programming capabilities, but usually the Java program prepares the data to be displayed (raw materials), and then the FreeMarker factory processes the data, during processing, a file is used as a reference. The prepared data is displayed in the template to generate the desired product.
1. Raw Materials
Includes templates and data. Data is what we display. templates, as the name suggests, are a standard that can be written as interfaces that we often call. A specification defined.
2. Factory
FreeMarker is a template engine that assembles data according to the template.
3. Products
Files generated based on the template.
So from the above explanation, we can understand that what we need to do is to prepare raw materials, that is, data and templates. Other jobs, Freemarker, are all done for me.
3. Some simple common syntaxes
Although these syntaxes are simple, they are the most commonly used and easy to understand. They can be quickly mastered after you use them all over again.
1. Obtain the model value.
The syntax is almost the same as the el expression: $ {bean}, $ {bean. property}
2. built-in functions.
Null Value processing: $ {bean! 'I am a null'}, $ {bean !}, $ {Bean. property! 'I am a null'}, $ {(bean. property )! 'I am a null '}
HtmlEscape: $ {bean? Html}
Date Format: $ {bean? String ('yyyy-MM-dd ')}
There are also many easy-to-use built-in functions. You can refer to the official documentation.
3. built-in labels.
Judgment:
<# If true>
<# Else>
</# If>
Loop:
<# List array as bean>
$ {Bean. property}
</# List>
4. Summary
The latest project has a function: Put the queried data and all other files together to generate a word file. I found some tools on the Internet that can complete this function. Freemarker is one of them. I learned a little and found that this tool is quite useful, so I sorted out some things. This blog is just a brief introduction to FreeMarker, the next blog will use this tool to implement two small examples, and will have a deep understanding of this tool.