日期:2014-05-17  浏览次数:20648 次

Freemarker将文件写到Html中
转载地址:http://www.shuonar.com/nar_referenceone.html?id=10说哪儿网

在跟着说客学习freemarker(一)中,我们仅仅将数据输出到控制台,那么,我们如何将文件输出到html中,这是我们这章节所讨论的,下面开始学习的旅行。有的同学要求本社区把第章节中完整的类代码贴出来,那么本社区就分享一下吧,在第一章节完整的类如下:
package org.shen.freemark;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TestFreemark {
private Template getTemplate(String name){
//通过Freemaker的Configuration读取相应的ftl
Configuration cfg = new Configuration();
Template tmp;
try {
//设定去哪里读取相应的ftl模板文件
cfg.setClassForTemplateLoading(this.getClass(), "/ftl");
//在模板文件目录中找到名称为name的文件
tmp = cfg.getTemplate(name);
return tmp;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private void print(String name, Map<String, Object> root){
Template tmp = this.getTemplate(name);
try {
//通过Template可以将模板文件输出到相应的流
tmp.process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("username", "shuonar");
TestFreemark tf = new TestFreemark();
tf.print("01.ftl", root);
}
}

以上就是第一章节的完整类,那么开始学习freemarker将文件读取到html中。
在上面代码中,这段代码:tmp.process(root, new PrintWriter(System.out));是把我们需要的流输出到控制,我们需要输出到控制台就让他输出到控制台,输出到控制台是System.out。那么我们传一个文件输出流输出到文件,那么我们再加一个方法,在E盘下加一个文件夹ftl(E:\ftl),然后下面是代码:
public void fprint(String name, Map<String, Object> root, String outFile){
FileWriter out = null;
//通过一个文件输出流,就可以写到相应的文件中
try {
out = new FileWriter(new File("E:\\ftl\\" + outFile));
Template tmp = this.getTemplate(name);
tmp.process(root, out);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上面代码注意一点,需要将输出流关闭。下面就是我们的测试代码:
public static void main(String[] args) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("username", "shuonar");
TestFreemark tf = new TestFreemark();
tf.print("01.ftl", root);
//输出到html中
tf.fprint("01.ftl", root, "o1.html");
}
然后点击运行,在E盘下面的ftl文件夹中存在01.html,点击运行,在html中我们看到输出的内容,不过这里是乱码,因为我们还没有设置编码方式,如何设置呢?
我们在木板目录下,即项目的ftl文件夹下建立一个html文件,名字是02.html,然后去掉<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">文档声明头,然后把html的后缀名字更改为html确定原先的html的编码方式是UTF-8,然后书写木板,代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>说哪儿网(www.shuonar.com)</title>
</head>
<body>
<h1>你好,${username}!</h1>
</body>
</html>
然后修改测试代码:
public static void main(String[] args) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("username", "shuonar");
TestFreemark tf = new TestFreemark();
tf.print("01.ftl", root);
//输出到html中
tf.fprint("02.ftl", root, "01.html");
}
然后运行点击01.html,乱码得以解决。
为了应广大热爱说哪儿网的网友要求,社区建立了一个freemarker交流群,希望大家加入交流freemarker技术。交流QQ 群是 :150712883,欢迎大家加入。
下面是第二章节的类的整体代码:
package org.shen.freemark;


import java.io.File;
import jav