其实生成静态网页的方式有好多种,我昨天看了一下,Freemarker是其中一种,但是Freemarker现在我们都用得比较少了,现在用得ActiveMQ用来发送信息到静态页面,不过想了一下这个小东西,还是想给大家分享一下,我的小小心得。

若项目为Maven项目,那么可以如下

在Pom.xml文件里面添加

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

CreateFreemarkerStatic.java

package com.llmj.DemoTest.Test;

import java.io.File;
import java.io.FileWriter;
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 CreateFreeMarkStatic {
/**
*
* @Description
* @author xebest-pc
* @param name
* @return
*/
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;
}
/**
* 控制台输出
* @Description
* @author xebest-pc
* @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文件
* @Description
* @author xebest-pc
* @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();
}
}
} public static void main(String[] args)
{
Map<String,Object> root=new HashMap<String,Object>();
root.put("username", "zhangsan");//在ftl中要赋值的变量
CreateFreeMarkStatic util= new CreateFreeMarkStatic();
util.fprint("01.ftl", root, "01.html");
} }

建立对应的实体类User.java

package com.llmj.DemoTest.entity;
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.llmj.DemoTest.entity;

public class Group {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

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>

然后最后附上测试类FreemarkTest.java

import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Test; import com.llmj.DemoTest.entity.Group;
import com.llmj.DemoTest.entity.User; public class FreemarkerTest {
@Test
public void test(){
CreateFreeMarkStatic util = new CreateFreeMarkStatic();
Map<String, Object> map = new HashMap<String, Object>(); Group group = new Group();
group.setName("IT"); User user = new User();
user.setId(001);
user.setName("张三");
user.setAge(12);
user.setGroup(group); List<User> users = new ArrayList<User>();
users.add(user);
users.add(user);
users.add(user); map.put("user", user);
//普通EL赋值
//util.fprint("01.ftl", map, "01.html" );
//判断
//util.fprint("03.ftl", map, "03.html");
//遍历
//util.print("05.ftl", map);
//子元素判断
util.print("06.ftl", map);
}
}

这样就可以测试了

Freemarker入门小案例(生成静态网页的其中一种方式)的更多相关文章

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

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

  2. Hibernate的介绍及入门小案例

    1.Hibernate的诞生 在以前使用传统的JDBC开发应用系统时,如果是小型应用系统,并不觉得有什么麻烦,但是对于大型应用系统的开发,使用JDBC就会显得力不从心,例如对几十,几百张包含几十个字段 ...

  3. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  4. spring boot入门小案例

    spring boot 入门小案例搭建 (1) 在Eclipse中新建一个maven project项目,目录结构如下所示: cn.com.rxyb中存放spring boot的启动类,applica ...

  5. 生成freeswitch事件的几种方式

    本文描述了生成freeswitch事件的几种方式,这里记录下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 在freeswitch代码中加入事件 ...

  6. [转] 微信小程序页面间通信的5种方式

    微信小程序页面间通的5种方式 PageModel(页面模型)对小程序而言是很重要的一个概念,从app.json中也可以看到,小程序就是由一个个页面组成的. 如上图,这是一个常见结构的小程序:首页是一个 ...

  7. Spring静态注入的三种方式

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...

  8. 使用Sphinx生成静态网页

    转载来自 http://www.ibm.com/developerworks/cn/opensource/os-sphinx-documentation/ 简介 Sphinx 是一种工具,它允许开发人 ...

  9. React.js入门小案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

随机推荐

  1. C#操作句柄

    1.直接上例子吧:收集系统信息msinfo32时,会有一个弹窗,现在要隐藏该弹窗,首先看没有通过句柄隐藏弹窗的现象 2.收集系统信息导入到一个位置 代码: Process[] msinfo32proc ...

  2. jQuery 如何获取ASP.NET服务器控件的值

    由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,总结有以下3种方法: 服务器控件代码:<asp:TextBox ID="txtUserID ...

  3. Lightoj1081【500棵线段树维护】

    #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N=5e2+10; const ...

  4. 密码破解工具John the Ripper使用说明

    John the Ripper John 包描述 John the Ripper 既功能丰富又运行快速. 它在一个程序中结合了几种破解模式,并且可以根据您的特定需求进行全面地配置(你甚至可以使用支持C ...

  5. (转)java 线程同步

    转自 http://blog.csdn.net/column/details/java-thread.html http://leo-faith.iteye.com/blog/177779 http: ...

  6. Debug和Release版本区别

    Debug和Release版本区别 众所周知,我们尽心iOS开发,在Xocde调试程序时,分为两种方式Debug和Release,在Target的Setting中相信大家应该能看到很多选项分别为Deb ...

  7. C# 可空类型(Nullable)

    C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable< Int32 >,读作& ...

  8. AKOJ-1265-输出二叉树

    链接:https://oj.ahstu.cc/JudgeOnline/problem.php?id=1265 题意: 我们知道二叉树的先序序列和中序序列或者是中序和后序能够唯一确定一颗二叉树.现在给一 ...

  9. 修复在unix系统里的文件打开不能显示正常的颜色问题

    在mac上面看到mysql的配置文件的颜色永远是白色,为了让配置文件的颜色更加分明些,这个时候只需进入到home目录下新建一个.vimrc文件, vim  .vimrc set nu syntax o ...

  10. 1-26HashSet简介

    Set的特点 Set里面存储的元素不能重复,没有索引,存取顺序不一致. package com.monkey1024.set; import java.util.HashSet; /** * Set的 ...