使用freemarker将页面生成html文件,本节测试html文件生成的方法:

1、使用模板文件静态化

定义模板文件,使用freemarker静态化程序生成html文件。

2、使用模板字符串静态化

定义模板字符串,使用freemarker静态化程序生成html文件。

1. pom.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-freemarker</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.projectlombok</groupId>
  30. <artifactId>lombok</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.commons</groupId>
  34. <artifactId>commons-io</artifactId>
  35. <version>1.3.2</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. <exclusions>
  42. <exclusion>
  43. <groupId>org.junit.vintage</groupId>
  44. <artifactId>junit-vintage-engine</artifactId>
  45. </exclusion>
  46. </exclusions>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

2. application.yml配置

  1. server:
  2. port: 8088
  3. spring:
  4. application:
  5. name: test-freemarker
  6. # freemarker配置
  7. freemarker:
  8. cache: false #关闭模板缓存,方便测试
  9. settings:
  10. template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
  11. template-loader-path: classpath:/templates
  12. charset: UTF-8
  13. check-template-location: true
  14. suffix: .ftl
  15. content-type: text/html
  16. expose-request-attributes: true
  17. expose-session-attributes: true
  18. request-context-attribute: request

3. 使用模板文件静态化

3.1 创建测试类,编写测试方法

  1. package com.example.demo;
  2. import freemarker.template.Configuration;
  3. import freemarker.template.Template;
  4. import freemarker.template.TemplateException;
  5. import org.apache.commons.io.IOUtils;
  6. import org.junit.jupiter.api.Test;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * @author john
  17. * @date 2019/12/20 - 19:09
  18. */
  19. @SpringBootTest
  20. public class TestFreemarkerHtml {
  21. //基于模板生成静态化文件
  22. @Test
  23. public void testGenerateHtml() throws IOException, TemplateException {
  24. //创建配置类
  25. Configuration configuration = new Configuration(Configuration.getVersion());
  26. //设置模板路径
  27. String classpath = this.getClass().getResource("/").getPath();
  28. configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));
  29. //设置字符集
  30. //configuration.setDefaultEncoding("UTF‐8");
  31. //加载模板
  32. Template template = configuration.getTemplate("test1.ftl");
  33. //数据模型
  34. Map<String, Object> map = new HashMap<>();
  35. map.put("name", "john");
  36. //静态化
  37. String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
  38. //静态化内容
  39. System.out.println(content);
  40. InputStream inputStream = IOUtils.toInputStream(content);
  41. //输出文件
  42. FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test/test1.html"));
  43. int copy = IOUtils.copy(inputStream, fileOutputStream);
  44. }
  45. }

编写模板

  1. <html>
  2. <head>
  3. <title>hello world!</title>
  4. </head>
  5. <body>
  6. ${name}<br/>
  7. <#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
  8. <#assign data=text?eval />
  9. 开户行:${data.bank} 账号:${data.account}
  10. </body>
  11. </html>

生成文件

3.2 使用模板字符串静态化

  1. package com.example.demo;
  2. import freemarker.cache.StringTemplateLoader;
  3. import freemarker.template.Configuration;
  4. import freemarker.template.Template;
  5. import freemarker.template.TemplateException;
  6. import org.apache.commons.io.IOUtils;
  7. import org.junit.jupiter.api.Test;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * @author john
  18. * @date 2019/12/20 - 19:09
  19. */
  20. @SpringBootTest
  21. public class TestFreemarkerHtml {
  22. //基于模板字符串生成静态化文件
  23. @Test
  24. public void testGenerateHtmlByString() throws IOException, TemplateException {
  25. //创建配置类
  26. Configuration configuration = new Configuration(Configuration.getVersion());
  27. //模板内容,这里测试时使用简单的字符串作为模板
  28. String templateString = "" +
  29. "<html>\n" +
  30. " <head></head>\n" +
  31. " <body>\n" +
  32. " 名称:${name}\n" +
  33. " </body>\n" +
  34. "</html>";
  35. //模板加载器
  36. StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
  37. stringTemplateLoader.putTemplate("template", templateString);
  38. configuration.setTemplateLoader(stringTemplateLoader);
  39. //得到模板
  40. Template template = configuration.getTemplate("template", "utf‐8");
  41. //数据模型
  42. Map<String, Object> map = new HashMap<>();
  43. map.put("name", "john");
  44. //静态化
  45. String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
  46. //静态化内容
  47. System.out.println(content);
  48. InputStream inputStream = IOUtils.toInputStream(content);
  49. //输出文件
  50. FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test/test2.html"));
  51. IOUtils.copy(inputStream, fileOutputStream);
  52. }
  53. }

springboot2.0结合freemarker生成静态化页面的更多相关文章

  1. 利用PHP的ob函数实现生成静态化页面

    之前用过一些开源的CMS管理系统,当时就很好奇后台中的生成HTML静态文件是怎么实现的.今天和同事讨论了下,没想到同事之前做过这类的生成静态页面的功能,果断向他请教了下. 经他讲解后,才知道其实生成静 ...

  2. java使用freemarker生成静态html页面

    1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  3. DJANGO-天天生鲜项目从0到1-007-首页静态化与缓存

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...

  4. SpringBoot2中,怎么生成静态文档

    SpringBoot2中,怎么生成静态文档 在实际开发过程中,我们通过swagger就可以生成我们的接口文档,这个文档就可以提供给前端人员开发使用的.但是,有时候,我们需要把我们的接口文档,提供给第三 ...

  5. JSP生成静态Html页面

    [转载]JSP生成静态Html页面 在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewr ...

  6. 网页静态化解决方案:Freemarker生成简单html页面

    FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不仅 ...

  7. 使用freemarker生成静态页面

    一 说明 需要在spring mvc项目中加入下列包: <dependency> <groupId>org.freemarker</groupId> <art ...

  8. 首页自动生成静态化html

    由于平台老是出现间歇性502.排查发现,php死进程过多耗费大量系统资源. 除了优化代码之外,静态化可以减少php进程.缓解一下服务器压力. 把首页生成html后,出现问题频率下降.所以需要做首页静态 ...

  9. Freemarker生成静态代码实例

    1.static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...

随机推荐

  1. [Linux]ubuntu更改国内源

    转自: https://blog.csdn.net/qq_35451572/article/details/79516563 推荐快速更新国内源 https://blog.csdn.net/qq_35 ...

  2. Spring Boot中的事务管理 隔离级别

    在声明事务时,只需要通过value属性指定配置的事务管理器名即可,例如:@Transactional(value="transactionManagerPrimary"). 除了指 ...

  3. requests_html使用asyncio

    import asyncio import functools from concurrent.futures.thread import ThreadPoolExecutor from reques ...

  4. 微擎转移服务器后,出现 require()错误,解决方案

    微擎中切换服务器后出现该问题,有可能是导致配置的问题

  5. [学习]sentinel中的DatatSource(一) ReadableDataSource

    sentinel是今年阿里开源的高可用防护的流量管理框架. git地址:https://github.com/alibaba/Sentinel wiki:https://github.com/alib ...

  6. Video Captioning 综述

    1.Unsupervised learning of video representations using LSTMs 方法:从先前的帧编码预测未来帧序列 相似于Sequence to sequen ...

  7. jQuery九类选择器

    目的:通过选择器,能定位web页面(HTML/JSP/XML)中的任何标签, 注意:项目中,通常是多种选择器一起使用 基本选择器 <html> <head> <meta ...

  8. kettle mogodb output详解

    以下主要来自官网文档,原文:https://wiki.pentaho.com/display/EAI/MongoDB+Output Configure Connection Tab 1 Host na ...

  9. 20180817周在ubuntu上面使用kettle一些总结

    1 ubuntu上面安装mysql用户名和密码问题: ubuntu上面安装MySQL的时候,如果是自动安装,没设置密码的话,那么用户名不是root. 比如在ubuntu用文本工具打开:gedit /e ...

  10. 关于MacBook Pro外接4K/60HZ显示器的问题

    我踩过的坑 MacBook Pro 外接 4K/60HZ显示器[显示器自带HDMI2.0支持4k] 拓展坞不支持4K/60HZ,最后导致只能支持 30HZ,鼠标移动明显延迟. 总结如下: DVI线类长 ...