freemarker 的学习(一)

闲话不多说 ,看看代码 本示例为测试示例 具体各位可根据自己的需求来定,

首先需要相关的jar  ,当然自己来下载哦  其实只需要一个jar  freemarker-2.3.28.jar

然后你需要在你的项目中创建一个文件夹 存放模板相关的文件 以 ftl 结尾

关于 ftl 文件 需要自己用 word 来进行修改 创建 word 后 另存为 xml 文件 然后 修改为 ftl 文件 其中的 参数变量 使用 "${XXXX}" 来表示 有点像 el 表达式

package com.freemarker.learn.java.example;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import freemarker.template.utility.StringUtil;

/**
 * 目前只支持 doc  docx 不支持
 *
 */
public class FreemarkerTest {
	
	
	public void test(){
		 //设置输入参数
        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("AJMC", "测试案件");
        dataMap.put("AJCJRQ", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        dataMap.put("JYAQ", "测试市场产生伤害是水水水水水水水水");
        try {
        	System.out.println(this.getClass());
            //创建模板配置相关  
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            //设置编码格式
            configuration.setDefaultEncoding("utf-8");
            //设置模板读取路径 此方式为读取当前文件夹下的目录
            //第一种方法在磁盘的文件系统上设置了一个明确的目录, 它确定了从哪里加载模板。不要说可能,File 参数肯定是一个存在的目录。否则,将会抛出异常
//            configuration.setDirectoryForTemplateLoading(new File("src/main/java/com/freemarker/learn/java/templates/"));
            //第二种调用方法使用了一个 Class 类型的参数和一个前缀。这是让你来指定什么时候通过相同的机制来加载模板, 不过是用Java的 ClassLoader 来加载类。
            //这就意味着传入的class参数会被 Class.getResource() 用来调用方法来找到模板。参数 prefix 是给模板的名称来加前缀的。
            //在实际运行的环境中, 类加载机制是首选用来加载模板的方法,通常情况下,从类路径下加载文件的这种机制, 要比从文件系统的特定目录位置加载安全而且简单。
            //在最终的应用程序中, 所有代码都使用 .jar 文件打包也是不错的, 这样用户就可以直接执行包含所有资源的 .jar 文件了。
            configuration.setClassForTemplateLoading(this.getClass(), "/templates/");
            
            //设置需要读取的模板加载模板 此文件名为 定义模板的名称
            Template template = configuration.getTemplate("case.ftl");
            //使用 writer 流 写出
            Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:\\"+System.currentTimeMillis()+".doc")), "utf-8"));
            template.process(dataMap, writer);
//            writer.close();
            System.out.println("成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
    public static void main( String[] args ) {
        System.out.println( "Hello World!" );
        System.out.println(FreemarkerTest.class.getClassLoader());
       
        FreemarkerTest freemarkerTest = new FreemarkerTest();
        freemarkerTest.test();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值