FreeMarker学习

需求驱动:

  • 测试数据生成
  • 项目中有单据打印可以通过动态替换参数生成pdf,然后传给打印机去打印,可以通过freeMarker来实现这种需求

                                        

                              FreeMarker实现图

1、下载FreeMarker

官网下载:

https://freemarker.apache.org/freemarkerdownload.html

或者用maven添加依赖形式下载

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker-gae</artifactId>
  <version>2.3.30</version>
</dependency>

2、新建一个项目来完成FreeMarkerDemo

   2、1把依赖加入工程项目中

3、创建模板文件

demo01.ftl

<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  	<table>
  		<caption>${caption}</caption>
  		《tr》
	  		<li>姓名:</li>
	  		<li>爱好:</li>
	  		<li>性别:</li>
	  		<li>地址:</li>
  		</tr>
  		<!list  users in user>
  		《tr》
	  		<li>${user.name}</li>
	  		<li>${user.hobby}</li>
	  		<li>${user.male}</li>
	  		<li>${user.address}</li>
  		</tr>
  		<#list>
  	</table>
</body>
</html>

4、写java代码去替换模板中的可替换参数

package cn.student.freeMarker;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;

public class FreeMarkerTest {
    public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
        //创建配置对象 
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        //设置读取模板路径
        cfg.setDirectoryForTemplateLoading(new File("./"));
        //设置编码集
        cfg.setDefaultEncoding("UTF-8");
        //异常处理
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);
        cfg.setWrapUncheckedExceptions(true);

        //准备数据
        Map root = new HashMap();
        root.put("caption", "个人兴趣爱好统计");
        List<Student> studentList = new ArrayList<>();
        Student student1 = new Student("张三","篮球","男","xxxx");
        studentList.add(student1);
        Student student2 = new Student("李四","足球","男","xxxx");
        studentList.add(student2);
        Student student3 = new Student("小丽","读书","女","xxxx");
        studentList.add(student3);
        Student student4 = new Student("王五","篮球","男","xxxx");
        studentList.add(student4);
        root.put("students", studentList);

        //获得模板
        Template temp = cfg.getTemplate("demo01.ftl");

        //产生输出流
        Writer out = new FileWriter("demo.html");
        temp.process(root, out);
        out.flush();
        out.close();
    }

}

实体类:Student

package cn.student.freeMarker;

public class Student {
    private String name;
    
    private String hobby;
    
    private String male;
    
    private String address;
    
    public Student(){
        
    }

    public Student(String name, String hobby, String male, String address) {
        super();
        this.name = name;
        this.hobby = hobby;
        this.male = male;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getMale() {
        return male;
    }

    public void setMale(String male) {
        this.male = male;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    

}

5、最后生成的文件

<html>
<head>
  <title>Welcome!</title>
</head>
<body>
      <table>
          <caption>个人兴趣爱好统计</caption>
          <tr>
              <li>姓名:</li>
              <li>爱好:</li>
              <li>性别:</li>
              <li>地址:</li>
          </tr>
            <tr>
                  <li>张三</li>
                  <li>篮球</li>
                  <li>男</li>
                  <li>xxxx</li>
            </tr>
            <tr>
                  <li>李四</li>
                  <li>足球</li>
                  <li>男</li>
                  <li>xxxx</li>
            </tr>
            <tr>
                  <li>小丽</li>
                  <li>读书</li>
                  <li>女</li>
                  <li>xxxx</li>
            </tr>
            <tr>
                  <li>王五</li>
                  <li>篮球</li>
                  <li>男</li>
                  <li>xxxx</li>
            </tr>
      </table>
</body>
</html>

 

原文地址:https://www.cnblogs.com/lhicp/p/14008629.html