博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Freemarker 最简单的例子程序
阅读量:6895 次
发布时间:2019-06-27

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

 
freemarker-2.3.18.tar.gz
 
 
1、通过String来创建模版对象,并执行插值处理
 
import freemarker.template.Template; 


import java.io.OutputStreamWriter; 

import java.io.StringReader; 

import java.util.HashMap; 

import java.util.Map; 


/** 
* Freemarker最简单的例子 
* @author leizhimin 11-11-17 上午10:32 
*/
 

public 
class Test2 { 

        
public 
static 
void main(String[] args) 
throws Exception{ 

                
//创建一个模版对象 

                Template t = 
new Template(
null
new StringReader(
"用户名:${user};URL:    ${url};姓名:  ${name}"), 
null); 

                
//创建插值的Map 

                Map map = 
new HashMap(); 

                map.put(
"user"
"lavasoft"); 

                map.put(
"url"
"http://www.baidu.com/"); 
                map.put("name""百度"); 
                //执行插值,并输出到指定的输出流中 
                t.process(map, new OutputStreamWriter(System.out)); 
        } 
}
 
执行后,控制台输出结果:
用户名:lavasoft;URL:    http://www.baidu.com/;姓名:  百度 

Process finished with exit code 0
 
 
2、通过文件来创建模版对象,并执行插值操作
 
import freemarker.template.Configuration; 

import freemarker.template.Template; 


import java.io.File; 

import java.io.OutputStreamWriter; 

import java.util.HashMap; 

import java.util.Map; 


/** 
* Freemarker最简单的例子 
* @author leizhimin 11-11-14 下午2:44 
*/
 

public 
class Test { 

        
private Configuration cfg;            
//模版配置对象 


        
public 
void init() 
throws Exception { 

                
//初始化FreeMarker配置 

                
//创建一个Configuration实例 

                cfg = 
new Configuration(); 

                
//设置FreeMarker的模版文件夹位置 

                cfg.setDirectoryForTemplateLoading(
new File(
"G:\\testprojects\\freemarkertest\\src")); 

        } 


        
public 
void process() 
throws Exception { 

                
//构造填充数据的Map 

                Map map = 
new HashMap(); 

                map.put(
"user"
"lavasoft"); 

                map.put(
"url"
"http://www.baidu.com/"); 
                map.put("name""百度"); 
                //创建模版对象 
                Template t = cfg.getTemplate("test.ftl"); 
                //在模版上执行插值操作,并输出到制定的输出流中 
                t.process(map, new OutputStreamWriter(System.out)); 
        } 
        public static void main(String[] args) throws Exception { 
                Test hf = new Test(); 
                hf.init(); 
                hf.process(); 
        } 
}
 
创建模版文件test.ftl
<
html
> 

<
head
> 

    
<
title
>Welcome!
</title> 

</head> 

<
body
> 

    
<
h1
>Welcome ${user}!
</h1> 

    
<
p
>Our latest product: 

    
<
a 
href
="${url}"
>${name}
</a>

</body> 

</html> 


尊敬的用户你好: 

用户名:${user}; 

URL:    ${url}; 

姓名:  ${name}
 
执行后,控制台输出结果如下:
<html> 

<head> 

    <title>Welcome!</title> 

</head> 

<body> 

    <h1>Welcome lavasoft!</h1> 

    <p>Our latest product: 

    <a href="http://www.baidu.com/">百度</a>! 

</body> 

</html> 


尊敬的用户你好: 

用户名:lavasoft; 

URL:    http://www.baidu.com/; 

姓名:  百度 

Process finished with exit code 0
 
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/716825,如需转载请自行联系原作者
你可能感兴趣的文章
在Windows Server 2016 Core模式下安装Windows Admin Center
查看>>
kill killall pkill 的区别
查看>>
shell编程之选择结构
查看>>
MDT捕捉镜像提示错误
查看>>
Linux 文件搜索
查看>>
我的友情链接
查看>>
Content-Disposition 响应头
查看>>
给maven配置proxy
查看>>
C++ Lesson4 构造函数与析构函数之一
查看>>
MSsql里执行insert时报错“identity_insert off”的解决办法
查看>>
percona-toolkit工具检查MySQL复制一致性及修复
查看>>
ESXi命令行关闭虚拟机
查看>>
java try() catch
查看>>
MSTP基本概念
查看>>
Cacti 使用问题
查看>>
nginx 正则表达式匹配入门篇
查看>>
百度地图官方API
查看>>
Centos系统配置国内163网易yum源shell自动安装脚本
查看>>
拉普拉斯变换 cvLaplace
查看>>
c++ 形参与实参值不一样
查看>>