Introduction to the concept of Freemarker (I.)

Source: Internet
Author: User

1. What is Freemarker?

It is a tool that generates a generic tool for text output based on a template, written in Java. It is independent of the specific container, does not know the HTTP or servlet, so he can be applied in a non-web application environment. like a factory, raw materials (such as data and text) to the Freemarker factory, it will assemble these materials together to generate a product we need.

It is a Java package, a class library for Java programmers. It is a template language in itself and has its own syntax, but it runs independently from the servlet container, so it is a tool that is not related to a specific container, allowing programmers to embed it in our system.

2. How to Work

The following figure is cut off from the Freemarker official website, which clearly describes how it generates a text.

It's a three-part, like the example of the factory mentioned above. Requires raw materials, factories and products.


From Freemarker's official web site know that Freemarker itself has the programming ability, but usually by the Java program prepares the data which needs to display (the raw material), then by the Freemarker this factory carries on the processing, when processing has a document as the reference, Displaying prepared data through a template ultimately generates the products we need.

1. Raw Materials

Include templates and data. Data is what we display, the template is the name of a specification, can be written as we often say the interface. Defines a specification.

2. Factory

Freemarker This template engine, assemble the data according to the template.

3. Products

The resulting file according to the template.

So from the above explanation we can understand that what we need to do is to prepare raw materials, data and templates, and all the other work freemarker have done for me.

3. Some simple and common syntax

Although these grammars are simple, they are the most commonly used, simple and easy to understand, and can be mastered very quickly once in a while.

1. Gets the value of the model.

Syntax and El expressions are almost the same: ${bean},${bean.property}

2, built-in functions.

Null Handling: ${bean! ' I am the empty value '},${bean!},${bean.property! ' I am the empty value '},${(bean.property)! ' I am a null value '}

Htmlescape:${bean?html}

Date formatting: ${bean?string (' Yyyy-mm-dd ')}

There are also many useful built-in functions that can be consulted on official documents.

3, built-in label.

Judge:

< #if true>

< #else >

</#if >

Cycle:

< #list array as bean>

${bean.property}

</#list >

4. Summary

There is a feature in the recent project: Put the queried data and all other files together to generate a Word file. I looked up the web and found some tools to do this. Freemarker is one of them. A little bit about the discovery of this tool is very useful, so organized a few things, this blog is simply a simple introduction to freemarker this tool, the next time the blog will use this tool to implement two small examples, while the tool in-depth understanding.

Introduction to the concept of Freemarker (I.)

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.