Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use FreeMarker

2025-08-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report