一些不常更新数据的页面可以使用freemarker生成静态页面,访问的时候直接访问静态页面即可,这样可以减少不少后端的压力。
时间不多,简单记录下这个例子:
项目目录如下:
pom.xml添加freemarker的依赖:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
需要生成页面的模板文件index.ftl:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index page</title>
</head>
<body>
<#list users as user>
username ${user.username}
password ${user.password}
<br />
</#list>
</body>
</html>
需要用到的实体bean User:
package com.nemo.bean;
/**
* Created by Nemo on 2017/9/26.
*/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
操作Freemarker生成静态页面的工具类:
package com.nemo.util;
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;
/**
* Created by Nemo on 2017/9/26.
*/
public class FreemarkerUtil {
private static FreemarkerUtil util;
private static Configuration cfg;
private FreemarkerUtil() {
}
public static FreemarkerUtil getInstance(String pname) {
if(util==null) {
cfg = new Configuration();
cfg.setClassForTemplateLoading(FreemarkerUtil.class, pname);
util = new FreemarkerUtil();
}
return util;
}
private Template getTemplate(String fname) {
try {
return cfg.getTemplate(fname);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 通过标准输出流输出模板的结果
* @param root 数据对象
* @param fname 模板文件
*/
public void sprint(Map root,String fname) {
try {
getTemplate(fname).process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 基于文件的输出
* @param root
* @param fname
* @param outpath
*/
public void fprint(Map root,String fname,String outpath) {
try {
getTemplate(fname).process(root, new FileWriter(outpath));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试类:
package com.nemo.test;
import java.util.*;
import com.nemo.bean.User;
import org.junit.Test;
import com.nemo.util.FreemarkerUtil;
/**
* Created by Nemo on 2017/9/26.
*/
public class TestFreemarker {
/**
* 模板文件存放目录
*/
private FreemarkerUtil util = FreemarkerUtil.getInstance("/ftl");
private Map root = new HashMap();
/**
* 生成文件存放目录
*/
private String fn = "F:/freemarker/";
@Test
public void test0111() {
List users = new ArrayList();
User nemo = new User();
nemo.setUsername("Nemo");
nemo.setPassword("Helloworld");
users.add(nemo);
User echo = new User();
echo.setUsername("Echo");
echo.setPassword("echo");
users.add(echo);
root.put("users",users);
util.sprint(root, "index.ftl");
util.fprint(root, "index.ftl", fn+"index.html");
}
}
执行测试方法,即可生成静态文件。