1、功能描述:使用java代码,根据指定的ftl模板文件,生成html静态页面文件

 

freemarker模板是直接可以与SSM这种java web项目、或springboot 整合的,不做前后分离。跟jsp差不多。

 

先描述两种使用方式:

  1、java代码中直接运行main方法,根据ftl模板文件,生成html文件

  2、java web配置freemarker,像jsp一样,做一个单体项目。

 

 

先来第一种:1、java代码中直接运行main方法,根据ftl模板文件,生成html文件

 

  1.1、导入freemarker的maven依赖:我用的是springboot,4.0以上版本,如果项目是spring 4.0版本以下,用freemarker 2.3以上可能会报错,改低版本用freemarker 2.2便行

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

 

  1.2、在D:/test001目录下,放一个hello.ftl模板,内容如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World222!</title>
</head>
<body>

    <p>
        welcome
    </p>
    <p>
        ${hello}
    </p>

    <p>性别:
        <#if sex==0>
                  女
           <#elseif sex==1>
                  男
           <#else>
                  保密
        </#if>
    </p>

    <h4>我的好友:</h4>
    <#list friends as item>
           姓名:${item.name} , 年龄${item.age}
       <br>
    </#list>
    

</body>
</html>

 

  1.3、建一个FreemarkerUtil.java工具类直接进行测试看看

package com.jzproject.common.util.freeMarker;

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

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FreeMarkerDemo2 {


    public static Map getDataModel(){
        Map resultMap = new HashMap();

        resultMap.put("hello", "第一个显示数据:hello!");

        resultMap.put("sex", 1); //性别


//        list列表数据
        List<Map<String, Object>> friends = new ArrayList<Map<String, Object>>();
        Map<String, Object> friend = new HashMap<String, Object>();
        friend.put("name", "xbq");
        friend.put("age", 22);
        friends.add(friend);
        friend = new HashMap<String, Object>();
        friend.put("name", "July");
        friend.put("age", 18);
        friends.add(friend);
        resultMap.put("friends", friends);





        return resultMap;
    }


    public static void main(String[] args) {


        try{
            // 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
            Configuration configuration = new Configuration(Configuration.getVersion());
            // 第二步:设置模板文件所在的路径,一般都在web项目WEB-INF下的ftl文件夹中
//            configuration.setDirectoryForTemplateLoading(new File("D:/web/src/main/webapp/WEB-INF/ftl"));
            configuration.setDirectoryForTemplateLoading(new File("D:/test001"));

            // 第三步:设置模板文件使用的字符集。一般就是utf-8.
            configuration.setDefaultEncoding("utf-8");
            // =================================================第四步:加载一个模板,创建一个模板对象。
            Template template = configuration.getTemplate("hello.ftl");


            // =======================================第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
//            Map dataModel = new HashMap<>();
//            //向数据集中添加数据
//            dataModel.put("hello", "this is my first freemarker test.");

            Map dataModel = getDataModel();//获取页面要展示的数据map对象



            // =======================================第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名,一般生成的文件都统一放在一个文件夹中,方便查找
            Writer out = new FileWriter(new File("D:/test001/hello.html"));
            // 第七步:调用模板对象的process方法输出文件。
            template.process(dataModel, out);
            // 第八步:关闭流。
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }



    }






}

 

  生成hello.html文件成功,浏览器打开如下:

java 套打 java套打 freemaker_java

 

 

 

 

 

============================================第二种:把freemarker做springboot的页面展示

  2.1、导入freemarker与springboot的插件包

<!--springboot应用freemarker,像jsp一样直接跳转到.ftl或.html模板页面,并传数据显示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

 

  2.2、然后直接在项目的resources/templates/freemarker目录下建一个freemarker.html文件,内容如下:

java 套打 java套打 freemaker_spring_02

 

 

  freemarker.html文件内容:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World!</title>
</head>
<body>
<center>
    <p>
        welcome ${name} to freemarker! 333html
    </p>

    <p>性别:
        <#if sex==0>
        女
        <#elseif sex==1>
        男
        <#else>
        保密
    </#if>
    </p>

    <h4>我的好友:</h4>
    <#list friends as item>
    姓名:${item.name} , 年龄${item.age}
    <br>
</#list>
</center>
</body>
</html>

 

 

  2.3、直接用controller类直接跳到此freemarker.html页面,如:

java 套打 java套打 freemaker_java 套打_03

 

 

 

  2.4、打开此项目路径访问:localhost:8080/project/freemarkerTest1