Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use FreeMarker

Shulou Source: shulou.com Published: 2022-06-01 05:22:33 10月03日 Update

This article mainly introduces the relevant knowledge of "how to use FreeMarker". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use FreeMarker" can help you solve the problem.

First, what is the static page

When visiting details pages such as news, event items, etc., the path can be xx [id]. Html, the server dynamically generates html web pages according to the request id. The next time you visit the data, you do not need to query the data, and directly return the html static page-reduce the data base to each other, and improve the query ability.

2. Use the technology Freemarker

Here, because I am using Struts2, the default is to use freemarker to implement a custom tag template, and the project imports struts2 and has already imported the freemarker jar package.

2.1What is Freemarke

FreeMarker is a template engine: a general tool based on templates and data to be changed and used to generate output text (HTML pages, e-mails, configuration files, source code, etc.). It is not for end users, but a Java class library, a component that programmers can embed in the products they develop.

Template file + Java data object = output (text in any format)

Freemarker template file, usually with an extension. Ftl (use. Html .jsp is fine)

2.2 install the freemarker eclipse editing plug-in

Copy "freemarker_eclipseplugin" to eclipse's dropins folder and restart the development tool

2.2.1 Editing the template file for freemarker

Template files are usually placed under WEB-INF or classes

III. Simple introduction to Freemarker

3.1Create a simple Freemarker template file

${variable}, you need to specify the variable in the program and merge it into the template file for output.

3.3 write a test class

Package cn.hj.freemarker

Import java.io.File

Import java.io.IOException

Import java.io.PrintWriter

Import java.util.HashMap

Import java.util.Map

Import org.junit.Test

Import freemarker.template.Configuration

Import freemarker.template.Template

Import freemarker.template.TemplateException

Public class FreemarkerTest {

@ Test

Public void testOutput () throws IOException, TemplateException {

/ / configuration object, configuration template location

Configuration configuration=new Configuration (

Configuration.VERSION_2_3_22)

Configuration.setDirectoryForTemplateLoading (new File (

"src/main/webapp/WEB-INF/templates"))

/ / get the template object

Template template=configuration.getTemplate ("hello.ftl")

/ / dynamic data object

Map paramterMap=new HashMap ()

ParamterMap.put ("title", "getting started with Freemarker")

ParamterMap.put ("msg", "Hello, this is the first Freemarker case!" )

/ / merge output

Template.process (paramterMap, new PrintWriter (System.out))

}

}

This is the end of the introduction to "how to use FreeMarker". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Templates files data objects output knowledge pages configuration dynamic variables tools text cases programs web pages industry static development generation different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Microsoft Shulou Information Docker