Freemarker入门案例

首先需要到freemarker官方下载freemarker的jar包,导入到项目中,如:freemarker-2.3.19.jar

1、先建个freemarker的工具类,FreemarkerUtil.java

package com.ljq.fm;

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) {
try {
// 通过Freemaker的Configuration读取相应的ftl
Configuration cfg = new Configuration();
// 设定去哪里读取相应的ftl模板文件
cfg.setClassForTemplateLoading(this.getClass(), "/ftl");
// 在模板文件目录中找到名称为name的文件
Template temp = cfg.getTemplate(name);
return temp;
} catch (IOException e) {
e.printStackTrace();
}
return null;
} /**
* 控制台输出
*
* @param name
* @param root
*/
public void print(String name, Map<String, Object> root) {
try {
// 通过Template可以将模板文件输出到相应的流
Template temp = this.getTemplate(name);
temp.process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 输出HTML文件
*
* @param name
* @param root
* @param outFile
*/
public void fprint(String name, Map<String, Object> root, String outFile) {
FileWriter out = null;
try {
// 通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径
out = new FileWriter(new File("E:/workspace/freemarkprj/page/" + outFile));
Template temp = this.getTemplate(name);
temp.process(root, out);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

2 、在src目录下建个ftl包,用于存放ftl模板文件,this.getClass() 就是根据当前类的路径获取模板文件位置
01.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head> <body>
<h1>你好${username}</h1>
</body>
</html>

02.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<h1>你好: ${username}</h1>
</body>
</html>

03.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${user.id}-----${user.name}-----${user.age}</h1>
<#if user.age lt 12>
${user.name}还是一个小孩
<#elseif user.age lt 18>
${user.name}快成年
<#else>
${user.name}已经成年
</#if>
</body>
</html>

04.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<#list users as user>
${user.id}---------${user.name}-------${user.age}<br/>
</#list>
</body>
</html>

05.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<hr/>
<#list users as user>
${user.id}---------${user.name}-------${user.age}<br/>
</#list>
</body>
</html>

06.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
${user.id}-------${user.name}------${user.group!} <#-- !后为空就不输出 -->
<#--${user.group.name!}--><#-- 按照以上的方式加! freemarker仅仅只会判断group.name是不是空值 -->
${(user.group.name)!"1234"} ${(a.b)!"没有a.b元素"} <#--
!:指定缺失变量的默认值
??:判断某个变量是否存在,返回boolean值
-->
<#if (a.b)??> <#--if后不用加$-->
不为空
<#else>
为空
</#if>
</body>
</html>

实体类User.java

package com.ljq.fm;

import java.io.Serializable;

@SuppressWarnings("serial")
public class User implements Serializable {
private int id;
private String name;
private int age;
private Group group; public Group getGroup() {
return group;
} public void setGroup(Group group) {
this.group = group;
} public User() {
} public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

实体类Group.java

package com.ljq.fm;

/**
*
*
* @author 林计钦
* @version 1.0 2013-10-25 下午02:36:09
*/
public class Group {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

3、再建个Junit的测试类 FreemarkerTest.java

package com.ljq.fm;

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) {
try {
// 通过Freemaker的Configuration读取相应的ftl
Configuration cfg = new Configuration();
// 设定去哪里读取相应的ftl模板文件
cfg.setClassForTemplateLoading(this.getClass(), "/ftl");
// 在模板文件目录中找到名称为name的文件
Template temp = cfg.getTemplate(name);
return temp;
} catch (IOException e) {
e.printStackTrace();
}
return null;
} /**
* 控制台输出
*
* @param name
* @param root
*/
public void print(String name, Map<String, Object> root) {
try {
// 通过Template可以将模板文件输出到相应的流
Template temp = this.getTemplate(name);
temp.process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 输出HTML文件
*
* @param name
* @param root
* @param outFile
*/
public void fprint(String name, Map<String, Object> root, String outFile) {
FileWriter out = null;
try {
// 通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径
out = new FileWriter(new File("E:/workspace/freemarkprj/page/" + outFile));
Template temp = this.getTemplate(name);
temp.process(root, out);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Freemarker入门案例的更多相关文章

  1. SpringBoot快速入门(解析+入门案例源码实现)

    这里写目录标题 SpringBoot入门 一.SpringBoot 概念 二.JavaConfig 入门 1. JavaConfig 概念 2. 项目准备 三.常用注解 四.SpringBoot 入门 ...

  2. SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)

    SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...

  3. SpringMvc核心流程以及入门案例的搭建

    1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...

  4. Struts2第一个入门案例

      一.如何获取Struts2,以及Struts2资源包的目录结构的了解    Struts的官方地址为http://struts.apache.org 在他的主页当中,我们可以通过左侧的Apache ...

  5. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  6. Hibernate入门案例及增删改查

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  7. Quartz应用实践入门案例二(基于java工程)

    在web应用程序中添加定时任务,Quartz的简单介绍可以参看博文<Quartz应用实践入门案例一(基于Web应用)> .其实一旦学会了如何应用开源框架就应该很容易将这中框架应用与自己的任 ...

  8. Quartz应用实践入门案例一(基于Web环境)

    Quartz是一个完全由java编写的开源作业调度框架,正是因为这个框架整合了许多额外的功能,所以在使用上就显得相当容易.只是需要简单的配置一下就能轻松的使用任务调度了.在Quartz中,真正执行的j ...

  9. MyBatis入门案例 增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

随机推荐

  1. cocos2d-x 那些常犯的错误

    Label::_label; if(_label==NULL){ //初始化_label的代码 } //指针默认值不等于NULL,需要赋初始值Label::_label=NULL; string st ...

  2. CodeForces 500 A. New Year Transportation

    Description New Year is coming in Line World! In this world, there are n cells numbered by integers ...

  3. jq的遍历节点

    1.child()方法 该方法用于取得匹配元素的子元素集合 2.next() 该方法用于取得匹配元素后面紧邻的同辈元素, 3.prev() 该方法用于取得匹配元素前面紧邻的同辈元素 4.sibling ...

  4. 安卓开发之探秘蓝牙隐藏API(转)

    源:http://www.cnblogs.com/xiaochao1234/p/3793172.html 上次讲解Android的蓝牙基本用法,这次讲得深入些,探讨下蓝牙方面的隐藏API.用过Andr ...

  5. C# 经典入门12章-System.Collections.Generic命名空间

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtUAAAAsCAIAAAAl09PEAAAgAElEQVR4nOx95Vscyd7285cMPrg7Aw ...

  6. java.net.URLEncode编码 与 URLDecode解码问题

    1.java内部加密和解密 String mytext = java.net.URLEncoder.encode("中国", "utf-8"); String ...

  7. C语言实现md5函数代码

    网上找到的实现md5函数代码,包括一个头文件md5.h和一个源文件md5.c,用下面的测试代码test.c测试通过,各文件依次如下: 头文件md5.h #ifndef MD5_H #define MD ...

  8. 磁盘寻道时间算法之----------------SCAN算法和最短寻道时间优先调度算法

    若干个等待访问磁盘者依次要访问的柱面编号为:80,40,74,14,60,31,61,假设每移动一个柱面需要4毫秒时间,移动到当前位于35号柱面,且当前的移动方向向柱面号增加的方向.请计算: (1)若 ...

  9. Linux基本命令之用户系统相关命令

    1.格式说明 [simon@localhost simon]$ [simon@localhost ~]$ 这两种方式表示相同.simon是指定用户,localhost是计算机名字,如果不设置默认为lo ...

  10. IDEA github的应用

    1.下载并安装一个git 一直点下一步就可以 2.去github官网注册一个账号 https://github.com/ 2.1点击Sign up 进入注册页面 2.2填写注册信息,点击Create ...