博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
freemarker 学习一 入门小例子
阅读量:4628 次
发布时间:2019-06-09

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

分3步

1. jar包

  freemarker-2.3.19.jar

2. 代码

package pr.cgl.freemarker; import freemarker.template.Configuration; import freemarker.template.Template; import java.io.File; import java.io.OutputStreamWriter; import java.io.StringReader; import java.util.HashMap; import java.util.Map;
public class FreemarkerTest {
public static void main(String[] args) throws Exception{
//1.初始化配置 Configuration cfg = new Configuration(); //2.创建插值的model数据对象 Map map = new HashMap(); map.put("user", "lavasoft"); map.put("url", "http://www.baidu.com/"); map.put("name", "百度"); //3.加载模板文件 File templateDir = new File(FreemarkerTest.class.getResource("").getPath()); cfg.setDirectoryForTemplateLoading(templateDir); Template tt = cfg.getTemplate("index.ftl"); //4.生成目标页面文件并输出 tt.process(map, new OutputStreamWriter(System.out)); } } 3. 运行结果

<!DOCTYPE html>

<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
</head>
<body>  

  Hello, lavasoft,请访问<a href="http://www.baidu.com/">百度</a>

</body>
</html>

 

转载于:https://www.cnblogs.com/cglWorkBook/p/4894741.html

你可能感兴趣的文章
JavaScript内存优化
查看>>
BZOJ1059: [ZJOI2007]矩阵游戏(二分图匹配)
查看>>
P3385 【模板】负环
查看>>
URI、URL 和 URN的区别
查看>>
根据表达式序列(前缀、中缀、后缀)构建表达式树
查看>>
mysql性能优化
查看>>
【SqlServer系列】语法定义符号解析
查看>>
Color Length UVA - 1625
查看>>
TLS/SSL
查看>>
zoj2319Beautiful People Dp
查看>>
图片加载 背景色块问题
查看>>
Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
查看>>
搭建git服务器
查看>>
iOS之UIDynamic UI动力学使用步骤
查看>>
poj 2498 动态规划
查看>>
Windows Phone 7中使用PhoneApplicationService类保存应用程序状态
查看>>
MySql数据库的下载和安装卸载
查看>>
JDBC接口核心的API
查看>>
用Python编写WordCount程序任务
查看>>
AC日记——传纸条 洛谷 P1006
查看>>