一、什么是FreeMaker?

  FreeMaker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出,可以实现网页静态化。FreeMaker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。

  目前企业中主要用FreeMaker做静态页面或是页面展示。

二、FreeMaker的使用方法。

  把FreeMaker的jar包添加到工程中。

  Maven工程添加依赖。

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

原理图:

 

1、创建一个Configuration对象,构造参数是FreeMaker对应的版本号。

2、设置模板文件所在的路径。

3、设置模板文件使用的字符集,一般是utf-8.

4、加载一个模板,创建一个模板对象。

5、创建一个模板使用的数据集,可以是Pojo,也可以是Map,一般是Map。

6、创建一个Writer对象,一般是创建FileWriter对象,指定生成的文件名。

7、调用模板对象的 process 方法输出文件。

8、关闭流。

    @Test
    public void genFile() throws Exception {
        // 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 第二步:设置模板文件所在的路径。
        configuration.setDirectoryForTemplateLoading(new File("D:/workspacest/term/sf-item-web/src/main/webapp/WEB-INF/ftl"));
        // 第三步:设置模板文件使用的字符集。一般就是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.");
        // 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
        Writer out = new FileWriter(new File("D:/temp/term/out/hello.html"));
        // 第七步:调用模板对象的process方法输出文件。
        template.process(dataModel, out);
        // 第八步:关闭流。
        out.close();
    }

 三、模板的语法。

1、访问Map中的key。${key}

2、访问pojo中的属性。

  Student对象。学号、姓名、年龄。 ${key.property}

<lable>ID:</lable>${student.id}<br>

 

 

 

3、选集合中的数据。

  <#list studentList as student>

    ${student.id}/${studnet.name}

  </#list>

<#list studentList as student>
    <tr>
        <td>${student.id}</td>
        <td>${student.name}</td>
        <td>${student.age}</td>
    </tr>
<#list>

4、取循环中的下标。

  <#list studentList as student>

         ${student_index}

  </#list>

<#list studentList as student>
    <tr>
        <td>${student.index}</td>
        <td>${student.id}</td>
        <td>${student.name}</td>
        <td>${student.age}</td>
    </tr>
<#list>

 

5、判断。

  <#if student_index % 2 == 0>

  <#else>

  </#if>

<#list studentList as student>
    <#if student.index % 2 == 0>
        <tr bgcolor="blue">
    <#else>
         <tr bgcolor="red">
    <#if>
        <td>${student.index}</td>
        <td>${student.id}</td>
        <td>${student.name}</td>
        <td>${student.age}</td>
    </tr>
<#list>

6、日期类型格式化。

当前日期:${date?date}<br>
当前时间:${date?time}<br>
当前日期和时间:${date?datetime}<br>
自定义日期格式:${date?string("yyyy-MM-dd HH:mm:ss")}<br>

7、Null值的处理。

null值处理一:${myValue!"myValue为null"}<br>
nu值得处理二:
<#if myValue??>
    myValue不为null时。
<#else>
    myValue为null时。
</#if>

8、Include 标签。

  <#include “模板名称”/>

null值处理一:${myValue!"myValue为null"}<br>
nu值得处理二:
<#if myValue??>
    myValue不为null时。
<#else>
    myValue为null时。
</#if>
<#include "hello.ftl" />

三、FreeMaker整合Spring。

1、引入FreeMaker的依赖。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

2、Spring配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
</beans>

3.、web.xml配置文件中配置加载Spring配置文件。<parma-value>classpath:spring/*.xml</parma-value>

<servlet>
    <servlet-name>项目名称</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet<servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*.xml</param-value>
    </init-param>
</servlet>

4、编写Controller类进行测试。

4.1、从Spring容器中获得 FreeMakerConfigurer 对象。

4.2、从FreeMakerConfigurer对象中获得Configuration对象。

4.3、使用Configuration对象获得Template对象。

4.4、创建数据集。

4.5、创建输出文件的 Writer 对象。

4.6、调用模板对象的process方法生成文件。

4.7、关闭流。

@Controller
public class HtmlGenController {
    
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @RequestMapping("/genhtml")
    @ResponseBody
    public String genHtml()throws Exception {
        // 1、从spring容器中获得FreeMarkerConfigurer对象。
        // 2、从FreeMarkerConfigurer对象中获得Configuration对象。
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        // 3、使用Configuration对象获得Template对象。
        Template template = configuration.getTemplate("hello.ftl");
        // 4、创建数据集
        Map dataModel = new HashMap<>();
        dataModel.put("hello", "1000");
        // 5、创建输出文件的Writer对象。
        Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));
        // 6、调用模板对象的process方法,生成文件。
        template.process(dataModel, out);
        // 7、关闭流。
        out.close();
        return "OK";
    }
}

原文地址:http://www.cnblogs.com/sfwu/p/16885711.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性