1、一些网站需要静态页面,可以通过freemarker批量生成新闻类或者网站活动详情页面,放到静态网站服务器上,不用访问数据库,可以减少部分的数据库压力。
2、现在制作pdf账单或者word文档,使用freemarker也是一个非常好的选择。

1、编写ftl文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>billing报价单</title>
</head>
<body>
<div class="billing-content">
<div class="left-content">
<ul>
<li>账户:</li>
<li>${title}</li>
<li>客户名称:</li>
<li>${title}</li>
<li>收件人:${title}</li>
<li>${title}</li>
</ul>
</div>
<div class="right-content">
<ul>
<#list amounts as amount >
<li>¥${amount}</li>
</#list>
</ul>
</div>
</div>
</body>
</html>

2、搭建环境,修改pom

<?xml version="1.0" encoding="UTF-8"?>

<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>com.xxxxx.www</groupId>
<artifactId>freemarkDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>freemarkDemo Maven Webapp</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>

<build>
<finalName>freemarkDemo</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
</plugins>
</build>
</project>

3、 编写servelet 类

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* @program: freemarkDemo
* @description: freemarkerbilling
* @author: sunyuhua
* @create: 2021-07-27 22:17
**/
@WebServlet("/billing")
public class FreeMarkerBiiling extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//实例化模板对象
Configuration configuration=new Configuration(Configuration.VERSION_2_3_23);
//设置加载模板上下文以及加载模板的路径(模板存放路径)
configuration.setServletContextForTemplateLoading(getServletContext(),"/template");
//设置模板的编码方式
configuration.setDefaultEncoding("UTF-8");
//加载模板文件,获取模板对象
Template template=configuration.getTemplate("billing.ftl");
//设置数据模型
Map<String,Object> map=new HashMap<String,Object>();
map.put("title","账单计费");
ArrayList<String> amounts=new ArrayList<String>();
amounts.add("10.22");
amounts.add("11.22");
amounts.add("12.22");
amounts.add("15.22");
map.put("amounts",amounts);

//获取项目的根目录
String basePath=req.getServletContext().getRealPath("/");
//设置html的存放路径
File htmlFile=new File(basePath+"/html");
//判断文件(目录)是否存在
if(!htmlFile.exists()){
htmlFile.mkdir();
}
//得到生成的文件名(生成随机不重复的文件名)
String fileName =System.currentTimeMillis()+".html";
//创建html文件
File file=new File(htmlFile,fileName);
//获取文件输出流
FileWriter writer=new FileWriter(file);
//生成html,将数据填充到对应的模板中
try {
template.process(map,writer);
} catch (TemplateException e) {
e.printStackTrace();
}finally {
writer.flush();
writer.close();
}
}
}

4、配置jetty

FreeMarker+server3.0 做生成html的模板的方法_java

5、运行并查看生成的html

http://localhost:9090/billing
http://localhost:9090/html/1627482296420.html

FreeMarker+server3.0 做生成html的模板的方法_html_02