您的位置:首页 > Web前端 > HTML

使用freemarker生成html

2015-04-15 11:34 351 查看
http://herryhaixiao.iteye.com/blog/677524

由于freemarker这个技术很久很久就有了,注释我就没写得很详细了,相信大家都看得懂。下面就直接上代码以及一些代码上的解释了
ShowCourseView 类负责一些属性的封装,

package test;

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.Locale;
import java.util.Map;

import pojo.ShowCourseView;

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

public class FreeMarkerTest {
private Configuration config;

public Configuration getConfig() {
return config;
}

/**
* 注意:setEncoding这个方法一定要设置国家及其编码,不然在flt中的中文在生成html后会变成乱码
*
* @param filePath
*            文件路径
* @throws Exception
*/
public void init(String filePath) throws Exception {
config = new Configuration();
config.setDirectoryForTemplateLoading(new File(filePath));
config.setEncoding(Locale.CHINA, "utf-8");
}

/**
* 通过flt文件用html文件展示课程数据
*
* @param filePath
*            flt文件路径
* @param templateFile
*            flt模板文件
* @param list
*            要生成html的集合数据
* @param charset
*            flt生成数据的编码格式
* @param htmlFile
*            通过flt生成html的文件
* @throws Exception
*/
public void showCourse(String filePath, String templateFile,
List<ShowCourseView> list, String charset, String htmlFile)
throws Exception {
init(filePath);
Map<String, Object> root = new HashMap<String, Object>();
Template temp = getConfig().getTemplate(templateFile);
root.put("courseList", list);
Writer out = new OutputStreamWriter(new FileOutputStream(htmlFile),
charset);
temp.process(root, out);
}

public static void main(String[] args) throws Exception {
FreeMarkerTest test = new FreeMarkerTest();
ShowCourseView view1 = new ShowCourseView();
view1.setCategoryName("categoryName1");
view1.setCourseCode("code1");
view1.setName("name1");

ShowCourseView view2 = new ShowCourseView();
view2.setCategoryName("categoryName2");
view2.setCourseCode("code2");
view2.setName("name2");
ShowCourseView view3 = new ShowCourseView();
view3.setCategoryName("categoryName3");
view3.setCourseCode("code3");
view3.setName("name3");
List<ShowCourseView> list = new ArrayList<ShowCourseView>();
list.add(view1);
list.add(view2);
list.add(view3);
test.showCourse("E:/myProject/freemarker&html/src", "course.ftl", list,
"utf-8", "course.html");
}
}


  

package pojo;

public class ShowCourseView {
private String name;
private String courseCode;
private String categoryName = null;

public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
}


  course.flt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<table border="0">
<#list courseList as row>
<tr>
<td>
<#if row.categoryName?exists>
[<font color="red">${row.categoryName }</font>]
</#if>
<a href="/shlll/platform/course/CourseBrowse.do?courseCode=${row.courseCode}">${row.name}</a>
</td>
</tr>
</#list>
</table>
<div align="right">
${showMore?default("更多...")}
</div>
</body>
</html>


  我就说下模板文件中用到的几个标签,一个是<#list>标签,用过struts2的人就知道在jsp中是有个list迭代标签的,这个和那个迭代标签大同小异,关键是这个语法不一样,在标签中有个“courseList”,这个courseList就是对应我在FreemarkerTest类中的showCourse方法中的Map中放置的一个key,而这个key又是一个List集合,通过这个list迭代出每一个CourseShowView对象;<#if>这个标签和自定义标签中的if标签性质是一样的,只是它的判断语法不同,?exists从翻译来讲,这个就是存在,加个问号表示是否存在,存在返回true,不存在返回false,row.categoryName?exists判断ShowCourseView中的categoryName是否有值,有值就显示categoryName,没有就不显示;其余的语法比如取值问题,对象取值问题,这个是大家关注的比较多的问题,取值怎么取呢,其实这个很简单,这个和EL表达式中的取值方法是一样的;${}这样就可以取值了;最后在<div>中有个函数,default这个是什么意思?我来给大家讲讲,表示showMore如果为空的话就显示default中的值。
在flt文件中我有2个中文字体,这就是为什么我在FreeMarkerTest类中的init方法中,为什么要设置config的编码方式了。因为在生成html的时候,如果你没有配置config的编码方式,它就会用它默认的编码进行编码,所以导致你在html中看到的中文就会变成乱码了(这里指的乱码是指flt中的中文,不是后台生成的中文,切记)。
好了,以上就是我研究freemarker用到的知识,欢迎各位拍砖,写的不好别见怪,写这个也是因为网上具体的代码不多,没有一个比较完整的,倒是对freemarker的说明还是蛮多的,当然我这个也是个比较简单的例子,如果需要加深了解,还是需要自己动手去写代码的,我只是给大家起一个引导的作用,(*^__^*) 嘻嘻……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: