Freemarker生成静态代码实例
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生成静态代码实例的更多相关文章
- 使用FreeMarker生成静态HTML
1.FreeMarker需要添加的Maven依赖: <dependency> <groupId>org.freemarker</groupId> <artif ...
- java使用freemarker生成静态html页面
1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...
- Webpack的tapable 为什么要使用 new Funtion 来生成静态代码
为了保持代码的单态(monomorphism). 这涉及到了js引擎优化的一些问题, tapable从1.0.0版本开始就用new Function来生成静态代码最后来来执行, 以确保得到最优执行效率 ...
- 使用freemarker生成静态页面
一 说明 需要在spring mvc项目中加入下列包: <dependency> <groupId>org.freemarker</groupId> <art ...
- springmvc+freemarker生成静态html文件
参考资料: http://mylfd.iteye.com/blog/1896501 http://www.cnblogs.com/xxt19970908/p/5553045.html 个人实践: 1. ...
- springboot2.0结合freemarker生成静态化页面
目录 1. pom.xml配置 2. application.yml配置 3. 使用模板文件静态化 3.1 创建测试类,编写测试方法 3.2 使用模板字符串静态化 使用freemarker将页面生成h ...
- freeMarker生成静态页面
项目结构图 footer.ftl des==>${f.des}<br/> <a href="http://www.baidu.com"> 百度 < ...
- JAVA使用Freemarker生成静态文件中文乱码
1.指定Configuration编码 Configuration freemarkerCfg = new Configuration(); freemarkerCfg.setEncoding(Loc ...
- PHP类库生成pdf代码实例
require_once('./tcpdf/tcpdf.php'); //引入库文件 $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', f ...
随机推荐
- Linux network setting.
Lubuntu network setting. //1. Vi /etc/network/interfaces Add:auto eth0iface eth0 inet dhcp //2. Vi / ...
- 一个简单的Verilog计数器模型
一个简单的Verilog计数器模型 功能说明: 向上计数 向下计数 预装载值 一.代码 1.counter代码(counter.v) module counter( input clk, input ...
- DiskLruCache 硬盘缓存 使用简介
简介 LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时.对此,Google又提供了一套硬盘缓存的解决方案:DiskLru ...
- curl 命令使用
1.开启gzip请求curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间curl -o /dev/nu ...
- ORACLE调度之基于时间的调度(一)【weber出品】
一.调度的概述 这里我看到一篇对调度的概述觉得描述的比我好,但仅限于概述部分,其他部分我觉得我讲的比他好,于是发生以下事情: ************************华丽的转载******** ...
- web项目环境搭建(3):搭建Spring+MyBatis
spring 4.1.7 + MyBatis 3.3 正式开始配置关键内容,这是硬货 一. 新建spring配置文件,起名为 applicationContext.xml,放在src/main/res ...
- 25 读取jar包内log4j.properties文件方法
//读取log4j日志配置文件 InputStream inputStream=ApplicationExecutor.class.getResourceAsStream("/log4j_h ...
- 武汉科技大学ACM :1007: A+B for Input-Output Practice (VII)
Problem Description Your task is to Calculate a + b. Input The input will consist of a series of pai ...
- 【转】setStyleSheet用法
[转自]http://blog.csdn.net/seanyxie/article/details/5925723 使用setStyleSheet来设置图形界面的外观: QT Style Sheets ...
- C++朝花夕拾【更新】
C++拾遗 更新一些平时遇到的小细节: 1.关于类的无参构造函数和带有全部默认参考值的构造函数的区别 书上说的是带有全部默认值的构造函数就是无参构造函数,私以为不以为然,来看下边这个例子: #incl ...