天天看点

FreeMaker 代替 JSP 作为页面渲染

FreeMaker 代替 JSP 作为页面渲染

1 flt页面

<html>

<head>

<title></title>

</head>

<body>

<h1>${person.title}</h1>

<h2>${person.time}</h2>      

<p>${person.name}</p>

</body>

</html>

2 pom文件

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-freemarker</artifactId>

        </dependency>

            <groupId>commons-lang</groupId>

            <artifactId>commons-lang</artifactId>

            <version>2.6</version>

3 java 文件

package test;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.HashMap;

import java.util.Locale;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;

/**

 * 测试FreeMarker生成HTML模板

 * @author LiangGzone

 * @version 2014-01-08

 */

public class FreeMarkerTest {

    public static void main(String[] args) throws IOException {

        FreeMarkerTest test = new FreeMarkerTest();    

        PersonBean person = new PersonBean();

        person.setName("杨一凡");

        person.setTime("2019");

        person.setTitle("标题测试");

        Map map = new HashMap();        

       // person.loadData();

        map.put("person", person);

        boolean flag = test.createHTMLFile("C:/Users/hyk/Desktop/杨一凡/template.ftl","d:/liang/template.html", map); 

        System.out.print("==="+flag);

    }    

    /**

     * 根据ftl模板文件,生成静态HTML文件

     * @param ftlPath    FTL模板文件路径,例如["c:/liang/template.ftl"]

     * @param filePath    生成HMTL文件路径,例如["d:/liang/lianggzone.html"]

     * @param data        Map数据

     * @return

     */

    public boolean createHTMLFile(String ftlPath, String filePath, Map data) {

        Writer out = null;

        //获取HMTL文件目录

        String fileDirectory = StringUtils.substringBeforeLast(filePath, "/");

        //获取HMTL文件名

        String fileName = StringUtils.substringAfterLast(filePath, "/");

        String ftlDirectory = StringUtils.substringBeforeLast(ftlPath, "/");

        String ftlName = StringUtils.substringAfterLast(ftlPath, "/");        

        try {

            //文件递归创建生成文件目录

            File realDirectory = new File(fileDirectory);

            if (!realDirectory.exists()) {

                realDirectory.mkdirs();

            }

            //step1 获取freemarker的配置

            Configuration freemarkerCfg = new Configuration();

            //step2 设置freemarker模板所放置的位置(文件夹)

            freemarkerCfg.setDirectoryForTemplateLoading(new File(ftlDirectory));

            //step3 设置freemarker模板编码

            freemarkerCfg.setEncoding(Locale.getDefault(), "utf-8");

            //step4 找到对应freemarker模板并实例化

            Template template = freemarkerCfg.getTemplate(ftlName); 

            //step5 初始化一个IO流

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath)), "utf-8")); 

            //step6 模板渲染出所要的内容

            template.process(data, out);

        } catch (TemplateException e) {

            e.printStackTrace();

            return false;

        } catch (IOException e) {

        } finally{

            try {

                out.flush();

                out.close();

            } catch (IOException e) {

                e.printStackTrace();

        }

        return true;

    }

}