Freemarker 生成静态HTML文件

Freemarker需要的jar包:


Maven依赖:

<dependencies>
	<dependency>
    	<groupId>freemarker</groupId>
     	<artifactId>freemarker</artifactId>
     	<version>2.3.9</version>
	</dependency>
</dependencies>

package cn.oo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class GenerHtmlForFreemarker {
	public static void main(String[] args) {
		doRelease();
	}

	private static final String DEF_ENCODING = "UTF-8";// HTML文件UTF-8编码格式

	public static void doRelease() {
		try {
			Template template = getFreeMarkerTemplate("src/main/resources/ftl", "index.ftl");//ftl模板文件位置
			Writer out = getWriterStream("src/main/resources/html/blog.html");//html生成文件位置
			Map<String, Object> context = initFreeMarkerContext();
			template.process(context, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Map<String, Object> initFreeMarkerContext() throws Exception {
		Map<String, Object> context = new HashMap<String, Object>();
		try {
			List<String> blog_list = new ArrayList<String>();
			blog_list.add("HTML");
			blog_list.add("CSS");
			blog_list.add("Javascript");
			blog_list.add("jQuery");
			blog_list.add("Freemarker");
			blog_list.add("JavaSE");
			blog_list.add("JSP");
			blog_list.add("Servlet");
			blog_list.add("Linux");
			blog_list.add("Maven");
			blog_list.add("MySQL");
			blog_list.add("Oracle");
			blog_list.add("JDBC");
			blog_list.add("Redis");
			blog_list.add("Spring");
			blog_list.add("MyBatis");
			blog_list.add("Spring MVC");
			blog_list.add("Spring Boot");
			blog_list.add("Spring Cloud");
			context.put("name", "lucheng");
			context.put("blog_addr", "http://blog.csdn.net/phone13144830339");
			context.put("blog_list", blog_list);
			context.put("last_modif", new Timestamp(System.currentTimeMillis()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return context;
	}

	public static Template getFreeMarkerTemplate(String freemarkerTemplateSourceDirectory,
			String freemarkerTemplateName) throws Exception {
		try {
			Configuration config = new Configuration();
			config.setDirectoryForTemplateLoading(new File(freemarkerTemplateSourceDirectory));
			config.setObjectWrapper(new DefaultObjectWrapper());
			Template template = config.getTemplate(freemarkerTemplateName, DEF_ENCODING);
			return template;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Writer getWriterStream(String freemarkerFileDestnation) throws Exception {
		try {
			FileOutputStream fos = new FileOutputStream(freemarkerFileDestnation);
			Writer out = new OutputStreamWriter(fos, DEF_ENCODING);
			return out;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

生成的HTML文件代码:

<html>    
    <head>    
        <title>lucheng的博客</title>    
        <meta content="text/html;UTF-8" />  
        <style>  
            body{  
                font-family: 楷体;  
            }  
        </style>  
    </head>    
    <body>    
        <h1>欢迎访问 lucheng 的博客</h1>    
        <h2><a href="http://blog.csdn.net/phone13144830339">URL: http://blog.csdn.net/phone13144830339</a></h2>    
        <h3>博文列表如下:</h3>    
                HTML<br/>  
                CSS<br/>  
                Javascript<br/>  
                jQuery<br/>  
                Freemarker<br/>  
                JavaSE<br/>  
                JSP<br/>  
                Servlet<br/>  
                Linux<br/>  
                Maven<br/>  
                MySQL<br/>  
                Oracle<br/>  
                JDBC<br/>  
                Redis<br/>  
                Spring<br/>  
                MyBatis<br/>  
                Spring MVC<br/>  
                Spring Boot<br/>  
                Spring Cloud<br/>  
        <h2>Creat Time 2018-5-4 14:47:58</h2>    
    </body>    
</html>


猜你喜欢

转载自blog.csdn.net/phone13144830339/article/details/78947508