使用Freemarker实现网页静态化

1.1. 什么是freemarker

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

 

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

 

1.2. Freemarker的使用方法

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

Maven工程添加依赖


  1. <dependency>
  2. <groupId>org.freemarker</groupId>
  3. <artifactId>freemarker</artifactId>
  4. <version>2.3.23</version>
  5. </dependency>

原理:

使用Freemarker实现网页静态化

使用步骤:

第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。

第二步:设置模板文件所在的路径。

第三步:设置模板文件使用的字符集。一般就是utf-8.

第四步:加载一个模板,创建一个模板对象。

第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。

第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。

第七步:调用模板对象的process方法输出文件。

第八步:关闭流。


  1. @Test
  2. public void genFile() throws Exception {
  3. // 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
  4. Configuration configuration = new Configuration(Configuration.getVersion());
  5. // 第二步:设置模板文件所在的路径。
  6. configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/term197/taotao-item-web/src/main/webapp/WEB-INF/ftl"));
  7. // 第三步:设置模板文件使用的字符集。一般就是utf-8.
  8. configuration.setDefaultEncoding("utf-8");
  9. // 第四步:加载一个模板,创建一个模板对象。
  10. Template template = configuration.getTemplate("hello.ftl");
  11. // 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
  12. Map dataModel = new HashMap<>();
  13. //向数据集中添加数据
  14. dataModel.put("hello", "this is my first freemarker test.");
  15. // 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
  16. Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html"));
  17. // 第七步:调用模板对象的process方法输出文件。
  18. template.process(dataModel, out);
  19. // 第八步:关闭流。
  20. out.close();
  21. }

1.3. 模板的语法

1.3.1.    访问map中的key

${key}

 

1.3.2.    访问pojo中的属性

Student对象。学号、姓名、年龄

 

${key.property}

 使用Freemarker实现网页静态化


1.3.3.    取集合中的数据

<#list studentList asstudent>

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

</#list>

循环使用格式:

<#list 要循环的数据as循环后的数据>

</#list>

使用Freemarker实现网页静态化 

使用Freemarker实现网页静态化


1.3.4.    取循环中的下标

<#list studentList as student>

       ${student_index}

</#list>


使用Freemarker实现网页静态化

1.3.5.    判断

<#if student_index % 2 == 0>

<#else>

</#if>

 使用Freemarker实现网页静态化

 

1.3.6.    日期类型格式化

直接取值:${date}(date是属性名)如果传来的是一个Date型数据会报错

${date?date} 2016-9-13

${date?time} 17:53:55

${date?datetime} 2016-9-13 17:53:55

使用Freemarker实现网页静态化

1.3.7.    Null值的处理

如果直接取一个不存在的值(值为null)时会报异常

${aaa}

处理: ${aaa!”默认值”}或者${aaa! }代表空字符串

 

使用Freemarker实现网页静态化

 

1.3.8.    Include标签

<#include “模板名称”>

(相当于jstl中的包含)

 使用Freemarker实现网页静态化

1.4. Freemarker整合spring

引入jar包:

Freemarker的jar包

 使用Freemarker实现网页静态化

1.4.1.    创建整合spring的配置文件


  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  8. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  10. <beanid="freemarkerConfig"
  11. class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  12. <propertyname="templateLoaderPath"value="/WEB-INF/ftl/"/>
  13. <propertyname="defaultEncoding"value="UTF-8"/>
  14. </bean>
  15. </beans>

需要编写一Controller进行测试

 

1.4.2.    Controller

请求的url:/genhtml

参数:无

返回值:ok (String, 需要使用@ResponseBody)

业务逻辑:

1、从spring容器中获得FreeMarkerConfigurer对象。

2、从FreeMarkerConfigurer对象中获得Configuration对象。

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

4、创建数据集

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

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

7、关闭流。

 

加载配置文件:

使用Freemarker实现网页静态化


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

转载自https://blog.csdn.net/qq_22075041/article/details/77433397