1、FreeMarker需要添加的Maven依赖:

 <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>

2、使用模板生成HTML代码

2.1 数据模型

 public class User {

     private String username;

     private String password;

     private Integer age;

     private String address;

     //省略setter和getter方法
}

2.2 FreeMarker模板

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet"
href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" />
</head>
<body style="font-family:'Courier New'">
<h3 class="text-center">这是用户${username}的信息页!</h3>
<div class="col-md-6 column">
<table class="table table-bordered">
<tr>
<th>用户名</th>
<th>密码</th>
<th>年龄</th>
<th>地址</th>
</tr>
<tr>
<td>${username}</td>
<td>${password}</td>
<td>${age}</td>
<td>${address}</td>
</tr>
</table>
</div>
</body>
</html>

2.3 使用FreeMarker生成HTML代码

     /**
* 使用模板生成HTML代码
*/
public static void createHtmlFromModel() {
FileWriter out = null;
try {
// 通过FreeMarker的Confuguration读取相应的模板文件
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
// 设置模板路径
configuration.setClassForTemplateLoading(CreateHtmlByFreemarker.class, "/static/ftl");
// 设置默认字体
configuration.setDefaultEncoding("utf-8"); // 获取模板
Template template = configuration.getTemplate("user.ftl");
//设置模型
User user = new User("tom", "hahahah", 28, "上海市"); //设置输出文件
File file = new File("e:/html/result.html");
if(!file.exists()) {
file.createNewFile();
}
//设置输出流
out = new FileWriter(file);
//模板输出静态文件
template.process(user, out);
} catch (TemplateNotFoundException e) {
e.printStackTrace();
} catch (MalformedTemplateNameException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
if(null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

3、使用String作为FreeMarker模板,生成HTML代码

3.1 数据模型使用2.1模型

3.2 模板使用2.2模板

3.3 使用FreeMarker生成HTML代码

 /**
* 把模板读入到String中,然后根据String构造FreeMarker模板
*/
public static void createHtmlFromString() {
BufferedInputStream in = null;
FileWriter out = null;
try {
//模板文件
File file = new File("D:/EclipseLearnSpace/ResearchSpace/Html2Pdf/src/main/resources/static/html/user.html");
//构造输入流
in = new BufferedInputStream(new FileInputStream(file));
int len;
byte[] bytes = new byte[1024];
//模板内容
StringBuilder content = new StringBuilder();
while((len = in.read(bytes)) != -1) {
content.append(new String(bytes, 0, len, "utf-8"));
} //构造Configuration
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
//构造StringTemplateLoader
StringTemplateLoader loader = new StringTemplateLoader();
//添加String模板
loader.putTemplate("test", content.toString());
//把StringTemplateLoader添加到Configuration中
configuration.setTemplateLoader(loader); //构造Model
User user = new User("tom", "kkkkk", 29, "北京山东");
//获取模板
Template template = configuration.getTemplate("test");
//构造输出路
out = new FileWriter("e:/html/result.html");
//生成HTML
template.process(user, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

4、使用String模板,模板中使用List

4.1 数据模型

 public class Classes {

     private String classId; // 班级ID

     private String className; // 班级名称

     private List<User> users;

     public String getClassId() {
return classId;
} //省略settter和getter方法 }

4.2 FreeMarker模板

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>班级信息</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet"
href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" />
<style type="text/css">
.table-align{
margin-left: 25%;
}
</style>
</head>
<body style="font-family:'Courier New'"> <h3 class="text-center">下面是班级ID【${classId}】,班级名称【${className}】的人员信息</h3>
<div class="col-md-6 column table-align">
<table class="table">
<tr>
<th>姓名</th>
<th>密码</th>
<th>年龄</th>
<th>地址</th>
</tr>
<!-- FreeMarker使用List循环 -->
<#list users as user>
<tr>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.age}</td>
<td>${user.address}</td>
</tr>
</#list>
</table>
</div>
</body>
</html>

4.3 使用FreeMarker生成HTML代码

 /**
* 根据String模板生成HTML,模板中存在List循环
*/
public static void createHtmlFromStringList() {
BufferedInputStream in = null;
FileWriter out = null;
try {
//模板文件
File file = new File("D:/EclipseLearnSpace/ResearchSpace/Html2Pdf/src/main/resources/static/html/class.html");
//构造输入流
in = new BufferedInputStream(new FileInputStream(file));
int len;
byte[] bytes = new byte[1024];
//模板内容
StringBuilder content = new StringBuilder();
while((len = in.read(bytes)) != -1) {
content.append(new String(bytes, 0, len, "utf-8"));
} //构造Configuration
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
//构造StringTemplateLoader
StringTemplateLoader loader = new StringTemplateLoader();
//添加String模板
loader.putTemplate("test", content.toString());
//把StringTemplateLoader添加到Configuration中
configuration.setTemplateLoader(loader); //构造Model
Classes classes = new Classes();
classes.setClassId("23");
classes.setClassName("实验一班");
List<User> users = new ArrayList<User>();
User user = new User("tom", "kkkkk", 29, "北京");
users.add(user);
User user2 = new User("Jim", "hhhh", 22, "上海");
users.add(user2);
User user3 = new User("Jerry", "aaaa", 25, "南京");
users.add(user3);
classes.setUsers(users);
//获取模板
Template template = configuration.getTemplate("test");
//构造输出路
out = new FileWriter("e:/html/result.html");
//生成HTML
template.process(classes, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

使用FreeMarker生成静态HTML的更多相关文章

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

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

  2. Freemarker生成静态代码实例

    1.static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...

  3. 使用freemarker生成静态页面

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

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

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

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

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

  6. freeMarker生成静态页面

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

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

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

  8. FreeMarker 乱码解决方案 生成静态html文件

    读取模板的时候有一个编码: Template template = this.tempConfiguration.getTemplate(templatePath,"UTF-8") ...

  9. JAVAEE——宜立方商城10:使用freemarker实现网页静态化、ActiveMq同步生成静态网页、Sso单点登录系统分析

    1. 学习计划 1.使用freemarker实现网页静态化 2.ActiveMq同步生成静态网页 2. 网页静态化 可以使用Freemarker实现网页静态化. 2.1. 什么是freemarker ...

随机推荐

  1. hdu2545 树上战争 (并查集)

    Problem Description 给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜 ...

  2. Appium Android定位元素与操作

    文章写得很好,转载备用 一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录 ...

  3. spring jdbcTemplate源码剖析

    本文浅析 spring jdbcTemplate 源码,主要是学习其设计精髓.模板模式.巧妙的回调 一.jdbcTemplate 类结构 ①.JdbcOperations : 接口定义了方法,如 &l ...

  4. iOS stretchableImageWithLeftCapWidth 图片放大不变形

    转载自:http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html - (UIImage *)stretchableImageWithLe ...

  5. 多边形背景生成工具推荐-Trianglify

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 low poly低多边形(相似于折纸的效果),多边形风格的设计应用越来越多,今天我们就来看一个利用d3.js写成的生成器Triangli ...

  6. 【23】宁以non-member、non-friend替换member函数

    1.non-member方法与member方法没有本质区别,对于编译器来说,都是non-member方法,因为member方法绑定的对象,会被编译器转化为non-member方法的第一个形参.non- ...

  7. Nginx开发从入门到精通

    nginx由于出色的性能,在世界范围内受到了越来越多人的关注,在淘宝内部它更是被广泛的使用,众多的开发以及运维同学都迫切的想要了解nginx模块 的开发以及它的内部原理,但是国内却没有一本关于这方面的 ...

  8. Xdebug+phpstorm配置

    首先,把自己参考的网上材料的连接黏贴出来,是英文的,但是讲解的很详细,有兴趣的同学可以看一下. 1.http://blog.jetbrains.com/webide/2011/02/zero-conf ...

  9. [Effective C++ --015]在资源管理类中提供对原始资源的访问

    引言 资源管理类是防止资源泄漏的有力武器,但是许多APIs直接指涉资源,除非你发誓永不使用这样的APIs,否则只得绕过资源管理对象(resource-managing objects)直接访问原始资源 ...

  10. CSS字体大小设置时的参考(转)

    from:http://blog.sina.com.cn/s/blog_51cd580b0100gg6y.html font-size 设置的绝对关键字: 以下几个绝对字体大小的设置是有效的.当然他们 ...