- Freemarker是什么?
FreeMarker 是一款模板引擎:即一种基于模板、用来生成输出文本(任何来自于 HTML格式的文本用来自动生成源代码)的通用工具。它是为 Java 程序员提供的一个开发包,或者说是一个类库。
FreeMarker 不是 Web 开发的应用程序框架。它是一个适用于 Web 应用程序框架中的组件,但是 FreeMarker 引擎本身并不知道 HTTP 协议或 Java Servlet 的存在。它仅仅来生成文本内容。 - Freemarker能做什么?
使用 FreeMarker 作为视图层的组件,是为了给诸如 Struts 这样的 Model 2 应用框架提供现成的解决方案。比如代替Jsp,生成静态的Html等等; - 如何获取Freemarker资源包以及说明书?
Freemarker 目前最新2.3.20Jar包下载地址:http://jaist.dl.sourceforge.net/project/freemarker/freemarker/2.3.20/freemarker-2.3.20.tar.gz
Freemarker 中文说明书下载地址: http://nchc.dl.sourceforge.net/project/freemarker/chinese-manual/FreeMarker_Manual_zh_CN.pdf - 搭建简易Demo
(1)这里我使用maven来构建,pom.xml如下;如果使用传统的Eclipse Project,请将下载到的jar包加到lib下去;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.wxp.freemarker</groupId> <artifactId>freemarker01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>freemarker01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- Freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> </dependencies> </project>
(2)接下来,我们来创建Freemarker根据数据和模型生成Html页面的工具类
package org.wxp.freemarker; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class FreemarkerUtil { public Template getTemplate(String name) { Template temp = null; try { // 通过Freemarker的Configuration读取相应的Ftl Configuration cfg = new Configuration(); // 设定去哪里读取相应的ftl模板 cfg.setClassForTemplateLoading(this.getClass(), "/ftl"); // 在模板文件目录中寻找名称为name的模板文件 temp = cfg.getTemplate(name); } catch (IOException e) { e.printStackTrace(); } return temp; } /** * 控制台输出文件内容 * @param name * @param rootMap */ public void print(String name, Map<String, Object> rootMap) { try { // 通过Template类可以将模板文件输出到相应的文件 Template temp = this.getTemplate(name); temp.process(rootMap, new PrintWriter(System.out)); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 将替换后的模板内容输出到文件 * @param name * @param rootMap * @param outFile */ public void fprint(String name, Map<String, Object> rootMap, String outFile) { FileWriter out = null; try { out = new FileWriter(new File("D:\\freemarker\\ftl\\html\\" + outFile)); Template template = this.getTemplate(name); template.process(rootMap, out); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != out) try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
(3)然后,来创建Freemarker的模板文件,名为01.ftl;(其实可以看到,也就是在ftl文件中写html,除过Freemarker自己的标签)
<!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> 你好,${username} </body> </html>
(4)最后,来写一个测试类;
package org.wxp.freemarker; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; import org.wxp.freemarker.model.User; public class TestFreemarker { private FreemarkerUtil freemarkerUtil; private Map<String, Object> rootMap = null; @Before public void setUp() { freemarkerUtil = new FreemarkerUtil(); rootMap = new HashMap<String, Object>(); } @Test public void test01() { // 填充数据 rootMap.put("username", "王三毛"); // 打印到控制台 freemarkerUtil.print("01.ftl", rootMap); // 输出到文件 freemarkerUtil.fprint("01.ftl", rootMap, "01.html"); } }
(5)来看看生成的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> 你好,王三毛 </body> </html>
Freemarker由浅入深01-环境搭建、测试
最新推荐文章于 2024-08-07 09:38:48 发布