博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FreeMarker基础配置
阅读量:4070 次
发布时间:2019-05-25

本文共 1353 字,大约阅读时间需要 4 分钟。

freemarker 版本

官网下载地址 

当前测试所用版本为:

文件结构

1. 模板文件:test02.ftl

  Welcome!  

Welcome ${user}!

Our latest product: ${latestProduct.name}!

2. Java 测试类文件:Test.java

package com.freemarker.test02.base;import freemarker.template.*;import java.util.*;import java.io.*;public class Test {    public static void main(String[] args) throws Exception {        // 创建 Freemarker 配置实例        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);        // 指定模板文件从何处加载的数据源,这里设置成一个文件目录。        cfg.setDirectoryForTemplateLoading(new File("templates"));        cfg.setDefaultEncoding("UTF-8");        // 简单地重新抛出异常; 这应该在大多数生产系统中使用。        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);        // 创建一个数据模型        Map root = new HashMap();        root.put("user", "Big Joe");        Map latest = new HashMap();        root.put("latestProduct", latest);        latest.put("url", "products/greenmouse.html");        latest.put("name", "green mouse");        // 获取模板(使用内部缓存)        Template temp = cfg.getTemplate("test02.ftl");        // 合并数据模型模板        Writer out = new OutputStreamWriter(System.out);        temp.process(root, out);        out.flush();        out.close();        // 注意: ------------        // 为了简单起见,这里压制了异常(在方法签名中声明了异常,译者注),而在正式运行的产品中不要这样做。    }}

运行结果

  Welcome!  

Welcome Big Joe!

Our latest product: green mouse!

你可能感兴趣的文章
ORACLE权限管理调研笔记
查看>>
移进规约冲突一例
查看>>
IA32时钟周期的一些内容
查看>>
SM2椭圆曲线公钥密码算法
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>