Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Case Analysis of FreeMarker

Shulou Source: shulou.com Published: 2022-06-02 02:15:33 10月03日 Update

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.

Tags: File content template directory test output example case analysis analysis form skill console knowledge control same concise concise at the same time name character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno macOS Linux vpn