springboot整合freemarker 您所在的位置:网站首页 springboot整合freemarker实现代码生成器 springboot整合freemarker

springboot整合freemarker

2023-05-09 21:44| 来源: 网络整理| 查看: 265

前言

本篇文章主要介绍的是springboot整合freemarker填充ftl模板文件,生成新的文件(如html),以及freemarker的语法。

GitHub源码链接位于文章底部。

freemarker介绍

freemarker是一款模板引擎,它基于模板来生成文本输出。这里的文本包括但不限于html页面,word,各种源代码文本......

工作原理

模板:就是一份已经写好了基本内容,有着固定格式的文档,其中空出或者用占 位符标识的内容,由使用者来填充,不同的使用者给出的数据是不同的。在模板 中的占位符,在模板运行时,由模板引擎来解析模板,并采用动态数据替换占位 符部分的内容。

freemarker的应用方向有两个,一是基于ftl文件,将内容填充到ftl文件中,就可以使用制作ftl模板的文本的方式进行访问和显示了,比如使用html文本制作了一个ftl模板,我们使用代码填充数据进ftl模板,那么我们就能以访问html的方式去打开这个文件了;另一种方式则是直接生成对应的文件,比如生成xxx.html的文件。

应用场景: 淘宝中的商品数不胜数,在商品的详情页这一块,如果全都以真实的html页面显示,那么有多少个商品就得有多少个页面了,何况还有增删改的情况。所以使用一个固定的ftl模板,填充数据,这样一个文件就能显示无数个页面的内容了。

再比如一些政府单位的项目,每天需要发送一些word文档给领导,此时只需要通过程序将数据填充进ftl模板,然后生成一个个的xxx.word文件就行了。

ftl指令 1.一些常见的符号说明:

${}插值; 只能输出数值、日期或者字符串,其它类型不能输出。 在ftl页面中添加如下代码:

${name}; ${message}

在freemarker接口里添加数据如下: 这一段代码在页面输出的就是: =或者 gte:判断左边值是否大于等于右边值 5 解释成 FTL 标签的结 束字符,当然,也可以使用括号来避免这种情况,如:y)>

springboot整合freemarker

首先来看一下项目结构:

创建一个springboot项目

pom文件中添加依赖 org.springframework.boot spring-boot-parent 2.1.3.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-test application.yml中添加freemarker的相关配置 server: port: 8080 spring: freemarker: #设置编码格式 charset: utf-8 #设置文件后缀 suffix: .ftl #设置ftl文件路径 template-loader-path: classpath:/templates #关闭缓存,及时刷新,上线生产环境需要改为true cache: false

在resources目录中添加templates文件夹因为这在yml中配置了。

template文件夹中添加两个ftl模板文件,待会儿会用到,因为是创建html的文件,所以直接新建一个html文件,再将后缀改为ftl就行了。

index.ftl代码:

Freemarker 测试 ${name}; ${message} 联系人:${linkman} 电话:${info.tel};性别:${info.sex} bool为true bool为false 索引:${fruit_index}, 水果:${fruit.name}; 价格:${fruit.price} 总共${goodsList?size}条记录 ${goodsList[1].name} ${goodsList?first.name} id为:${jsonObj.id};text为:${jsonObj.text}

当前日期:${today?date} 当前时间:${today?time} 当前日期+时间:${today?datetime} 格式化显示当前日期时间:${today?string('yyyy年MM月dd日 HH:mm:ss')} ${number} ${number?c}

${strs!"strs空值的默认显示值"} str变量存在 strs变量不存在

header.ftl代码:

嵌入页面 嵌入页面

在freemarker接口中添加代码,返回freemarker的index.ftl视图

@RequestMapping("/freemarkerIndex") public String freemarker(Map dataModel) { dataModel.put("name", "张三"); dataModel.put("message", "hello world"); List list = new ArrayList(); Map map1 = new HashMap(); map1.put("name", "苹果"); map1.put("price", 4.5); Map map2 = new HashMap(); map2.put("name", "香蕉"); map2.put("price", 6.3); list.add(map1); list.add(map2); dataModel.put("goodsList", list); dataModel.put("today", new Date()); dataModel.put("number", 123456789L); return "index"; }

ps:这里dataModel的map只能从方法参数中获取,如果是自己new的去存储数据,填充到模板中,会报空值异常。 启动程序,通过localhost:8080/freemarkerIndex 访问该接口,得到页面如下: 上面这个接口是直接返回视图, 接下来是通过同样的数据,生成index.html静态文件。 因为这里是不用返回的,如果做成接口的话,虽然能成功生成文件,但是会报404,所以这里用的是单元测试,实际开发中,如果是返回的json格式,而不是视图,就可以做成接口。

@Test public void createFileByFreemarker() throws IOException, TemplateException { //创建配置对象 Configuration configuration = new Configuration(Configuration.getVersion()); //设置默认生成文件编码 configuration.setDefaultEncoding("utf-8"); //设置模板路径 configuration.setClassForTemplateLoading(this.getClass(), "/templates"); //获取模板 Template template = configuration.getTemplate("index.ftl"); //加载数据 Map dataModel =new HashMap(); dataModel.put("name", "张三"); dataModel.put("message", "hello world"); List list = new ArrayList(); Map map1 = new HashMap(); map1.put("name", "苹果"); map1.put("price", 4.5); Map map2 = new HashMap(); map2.put("name", "香蕉"); map2.put("price", 6.3); list.add(map1); list.add(map2); dataModel.put("goodsList", list); dataModel.put("today", new Date()); dataModel.put("number", 123456789L); //创建输出对象,将文件输出到D盘根目录下 FileWriter fileWriter = new FileWriter("D:/index.html"); //渲染模板和数据 template.process(dataModel, fileWriter); //关闭输出 fileWriter.close(); }

本文GitHub源码:https://github.com/lixianguo5097/springboot/tree/master/springboot-freemarker

CSDN:https://blog.csdn.net/qq_27682773 简书:https://www.jianshu.com/u/e99381e6886e 博客园:https://www.cnblogs.com/lixianguo



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有