1、static.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
描述:${description}
获取名字的多少:${names?size}
名字的遍历:
<#list names as name>
这是第${name_index}个人,他是${name}
<#if name=="李逍遥">
他的武器是轩辕剑
<#elseif name=="赵灵儿">
她的武器是玉剑
<#else>
她的武器是紫金剑
</#if>
</#list> 武器的遍历:
<#list weaponMap?keys as key>
key--->${key},武器名是${weaponMap[key]}
</#list> ========================================================
<#include "include.html"> </body>
</html>

2、include.html

include code

3、测试类MyFreemarker.java

 package com.test.freemarker;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template; public class MyFreemarker { public static void main(String[] args) throws Exception{
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("templates"));
cfg.setDefaultEncoding("UTF-8");
cfg.setObjectWrapper(new DefaultObjectWrapper()); Map<String,Object> rootMap = new HashMap<String,Object>();
rootMap.put("description", "来生成静态页面"); List<String> names = new ArrayList<String>();
names.add("李逍遥");
names.add("赵灵儿");
names.add("月如"); rootMap.put("names", names); Map<String,Object> weaponMap = new HashMap<String, Object>();
weaponMap.put("first", "轩辕剑");
weaponMap.put("second", "玉剑");
weaponMap.put("third", "紫金剑"); rootMap.put("weaponMap", weaponMap); Template template = cfg.getTemplate("static.html");
Writer writer = new OutputStreamWriter(new FileOutputStream("success.html"));
template.process(rootMap , writer);
System.out.println("静态页面生成了~~");
} }

结果:生成的页面success.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
描述:来生成静态页面
获取名字的多少:
名字的遍历:
这是第0个人,他是李逍遥
他的武器是轩辕剑
这是第1个人,他是赵灵儿
她的武器是玉剑
这是第2个人,他是月如
她的武器是紫金剑 武器的遍历:
key--->third,武器名是紫金剑
key--->first,武器名是轩辕剑
key--->second,武器名是玉剑 ========================================================
include code
</body>
</html>

Freemarker生成静态代码实例的更多相关文章

  1. 使用FreeMarker生成静态HTML

    1.FreeMarker需要添加的Maven依赖: <dependency> <groupId>org.freemarker</groupId> <artif ...

  2. java使用freemarker生成静态html页面

    1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  3. Webpack的tapable 为什么要使用 new Funtion 来生成静态代码

    为了保持代码的单态(monomorphism). 这涉及到了js引擎优化的一些问题, tapable从1.0.0版本开始就用new Function来生成静态代码最后来来执行, 以确保得到最优执行效率 ...

  4. 使用freemarker生成静态页面

    一 说明 需要在spring mvc项目中加入下列包: <dependency> <groupId>org.freemarker</groupId> <art ...

  5. springmvc+freemarker生成静态html文件

    参考资料: http://mylfd.iteye.com/blog/1896501 http://www.cnblogs.com/xxt19970908/p/5553045.html 个人实践: 1. ...

  6. springboot2.0结合freemarker生成静态化页面

    目录 1. pom.xml配置 2. application.yml配置 3. 使用模板文件静态化 3.1 创建测试类,编写测试方法 3.2 使用模板字符串静态化 使用freemarker将页面生成h ...

  7. freeMarker生成静态页面

    项目结构图 footer.ftl des==>${f.des}<br/> <a href="http://www.baidu.com"> 百度 < ...

  8. JAVA使用Freemarker生成静态文件中文乱码

    1.指定Configuration编码 Configuration freemarkerCfg = new Configuration(); freemarkerCfg.setEncoding(Loc ...

  9. PHP类库生成pdf代码实例

    require_once('./tcpdf/tcpdf.php'); //引入库文件        $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', f ...

随机推荐

  1. Atlas mysql的读写分离和负载均衡<转>

    mysql的读写分离和负载均衡 http://my.oschina.net/superbigfu/blog/178134

  2. 通过源码安装最新版Git

    下载源码 到Git的Github主页上下载最新的源码到本地,解压并进入目录. 编译安装 cd 你的git源码目录 autoconf ./configure make 第一个报错 报错内容: usr/b ...

  3. wildcard

    [rusky@rhel7 test]$ lstest1  test123  test2  test317  test33  test335  test336  test44  testtest[rus ...

  4. jdbc连接数据库和jdbc-odbc桥接方式连接数据库

    //这种方式为jdbc直接连接,需要添加jar文件 1 package com.howe2; import java.sql.*; public class test2 { public static ...

  5. TreeView绑定无限层级关系类

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind_TV(TreeView1.Nodes); ...

  6. HTML5和CSS3实例教程[总结一]

    关于onclick的行为与内容分离 通过链接触发弹出窗口方式 (不推荐使用此方法!!!) <a href='#' onclcik = "window.open('holiday_pay ...

  7. 黑马程序员-out和ref

    C# 方法参数关键字:ref.out 当希望方法返回多个值时,声明 out方法很有用.使用 out参数的方法仍然可以将变量用作返回类型(请参见 return),但它还可以将一个或多个对象作为 out参 ...

  8. 设计模式:模版模式(Template Pattern)

    android中的Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益. 模版模式 又叫模板方法模式,在一个方法中定义一个算法的 ...

  9. 你好,C++(30)“大事化小,小事化了”5.4.3 工资程序成长记:函数

    5.4.3 工资程序成长记:函数 自从上次小陈“程序员”的工资程序得到老板的夸奖,口头许诺给他涨工资以后,老板再也没有找过他,涨工资的事自然也就没有下文了.这天,老板又突然召他去办公室.这下可把小陈高 ...

  10. X-Y Problem

    X-Y Problem 对于X-Y Problem的意思如下: 1)有人想解决问题X2)他觉得Y可能是解决X问题的方法3)但是他不知道Y应该怎么做4)于是他去问别人Y应该怎么做? 简而言之,没有去问怎 ...