zoukankan      html  css  js  c++  java
  • FreeMarker-简单示例

    以下是简单的FreeMarker示例,直接采用模板 + 数据模型 = 输出的方式。示例中是Application的项目,主要用于展示模板输出HTML文件的功能。

    示例:

    1、引入POM依赖

            <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.26-incubating</version>
            </dependency>

    2、新建test.ftl文件

    <html>
    <head>
      <title>Welcome!</title>
    </head>
    <body>
      <h1>
        Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!
      </h1>
    </body>
    </html>

    3、新建FreeMarkerUtil.java类用于生成通过ftl模板生成html文件

    package com.jsoft.testfreemarker.test1;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URISyntaxException;
    import java.util.Map;
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    public class FreeMarkerUtil {
        /**
         * 获取模板
         * 
         * @param name
         * @return
         */
        public Template getTemplate(String name) {
            try {
                // 通过FreeMarker的Configuration读取相应的ftl
                Configuration cfg = new Configuration();
                // 设定去哪里读取相应的ftl模板文件
                cfg.setClassForTemplateLoading(this.getClass(),"/");
                // 在模板文件目录中找到名称为name的文件
                Template temp = cfg.getTemplate(name);
                return temp;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 输出到控制台
         * 
         * @param name
         *            模板文件名
         * @param root
         */
        public void print(String name, Map<String, Object> root) {
            try {
                // 通过Template可以将模板文件输出到相应的流
                Template temp = this.getTemplate(name);
                temp.process(root, new PrintWriter(System.out));
            } catch (TemplateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 输出到文件
         * 
         * @param name
         * @param root
         * @param outFile
         */
        public void fprint(String name, Map<String, Object> root, String outFile) {
            FileWriter out = null;
            try {
                // 通过一个文件输出流,就可以写到相应的文件中
                try {
                    out = new FileWriter(new File(App.class.getResource("/").toURI().getPath() + outFile));
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Template temp = this.getTemplate(name);
                temp.process(root, out);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    4、生成后的html内容:

    <html>
    <head>
      <title>Welcome!</title>
    </head>
    <body>
      <h1>
        Welcome Big Joe, our beloved leader!
      </h1>
    </body>
    </html>

    测试工程:https://github.com/easonjim/5_java_example/tree/master/freemarker/test1

    总结:

    1、其实这个例子是在Application基础上展示生成的html文件的,但实际项目不应该是这样去做,而是结合Servlet、Spring、Status这些框架进行开发。

  • 相关阅读:
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第4章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第3章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第1,2章 读书笔记(待更新)
    Tkinter的Message组件
    Git 实操/配置/实践
    mysq5.7.32-win安装步骤
    行为型模式之模板方法
    结构型模式之组合模式
    结构型模式之享元模式
    结构型模式之外观模式
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7090785.html
Copyright © 2011-2022 走看看