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

Case Analysis of FreeMarker

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you the example analysis of FreeMarker, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1.jar package: freemarker-2.3.19.jar, copy jar to the lib directory

two。 Create a new Web project: TestFreeMarker

Create a new ftl folder under the web directory

Create a new template file ftl03.ftl under ftl

Insert title here$ {hello}, cfg.setDirectoryForTemplateLoading (new File ("/ ftl")); Test

3.Java test class FreeMarkerDemo:

Package com.freemarker.test; import java.io.File;import java.io.PrintWriter;import java.io.StringWriter;import java.util.HashMap;import java.util.Map; import freemarker.template.Configuration;import freemarker.template.Template; public class FreeMarkerDemo {public static void main (String [] args) {try {/ / read the corresponding ftl Configuration cfg = new Configuration () through the Configuration of Freemaker / / set where to read the corresponding ftl template file cfg.setDirectoryForTemplateLoading (new File ("D:/workspace/TestFreeMarker/WebContent/ftl")); / / find the template file named name in the template file directory Template template = cfg.getTemplate ("ftl03.ftl"); Map root = new HashMap () Root.put ("hello", "Hello FreeMarker!"); root.put ("files", "li"); / / output StringWriter stringWriter = new StringWriter () in the console as a string; template.process (root, stringWriter); String resultStr = stringWriter.toString (); System.out.println (resultStr) / / output as a file to the specified file PrintWriter pt = new PrintWriter (new File ("D:/pt.txt")); template.process (root, pt);} catch (Exception e) {e.printStackTrace ();}

Console output result resultStr:

Insert title hereHello FreeMarker Magazine cfg.setDirectoryForTemplateLoading (new File ("/ ftl")); Test

At the same time, the pt.txt file is generated on disk D, which is the same as resultStr.

The above is the case study of FreeMarker. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report