freemarker是什么

百度百科:

FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。

FreeMarker是免费的,基于Apache许可证2.0版本发布。其模板编写为FreeMarker Template Language(FTL),属于简单、专用的语言。需要准备数据在真实编程语言中来显示,比如数据库查询和业务运算, 之后模板显示已经准备好的数据。在模板中,主要用于如何展现数据, 而在模板之外注意于要展示什么数据 [1] 。

依赖

使用spring boot开发,增加如下依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

controller:

package com.monkey.freemarker.controller;import com.monkey.freemarker.entity.Item;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.xml.ws.RequestWrapper;
import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;/*** @author :XXX* @date :Created in 2021/11/18 0018 8:34* @description:* @modified By:* @version: v1.0*/
@RestController
public class ItemController {@Autowiredprivate Configuration configuration;@RequestMapping("create")public String createHtml() throws Exception {// 获取模板Template template = configuration.getTemplate("item.tpl");// 数据填充模板Map<String, Object> dataModel = new HashMap<>();dataModel.put("item", new Item("iphone 13", new BigDecimal(10000), 10000));String path = "D:/tplHtml";File file = new File(path + "/item-" + 100 + ".html");if (file.exists()) {System.out.println("文件已存在");} else {file.createNewFile();BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));template.process(dataModel, bufferedWriter);}return "create success";}
}

entity:

package com.monkey.freemarker.entity;import java.math.BigDecimal;/*** @author :XXX* @date :Created in 2021/11/18 0018 8:34* @description:商品* @modified By:* @version: v1.0*/
public class Item {private String goodName;private BigDecimal goodPrice;private int evaluateCount;public Item(String goodName, BigDecimal goodPrice, int evaluateCount){this.evaluateCount = evaluateCount;this.goodName = goodName;this.goodPrice = goodPrice;}public String getGoodName() {return goodName;}public void setGoodName(String goodName) {this.goodName = goodName;}public BigDecimal getGoodPrice() {return goodPrice;}public void setGoodPrice(BigDecimal goodPrice) {this.goodPrice = goodPrice;}public int getEvaluateCount() {return evaluateCount;}public void setEvaluateCount(int evaluateCount) {this.evaluateCount = evaluateCount;}
}

tpl:

<h1>商品详情页</h1>
商品展示<p>商品名称:${item.goodName }</p><p>商品价格:${item.goodPrice }</p><p>商品评价数:${item.evaluateCount }</p>

工程目录结构:

效果图:

freemarker模板最小案例实现相关推荐

  1. freemarker模板导出word循环图片表格详细教程

    前言:表哥之前已经过一篇freemarker模板导出带表格word详细教程  freemarker模板导出带表格word详细教程_Java大表哥的博客-CSDN博客,为什么现在又要写一篇呢. 因为我这 ...

  2. Freemarker模板引擎学习,生成html里的动态表格,可合并单元格

    需求:现有html模板,需动态填充数据,并且包含表格,表格大小不固定,根据数据多少确定表格大小. 解析:两种方案: 1.java代码实现:将模板文件读出为StringBuffer,找到特定位置,循环生 ...

  3. freemarker模板导出带表格word详细教程

    前言:另外一篇是手机端的word导出,需要兼容性,不然在安卓手机会乱码 freemarker模板导出word循环图片表格详细教程_Java大表哥的博客-CSDN博客  并且另存为模版格式不同数据绑定方 ...

  4. Spring 4 使用Freemarker模板发送邮件添加附件

    前言 Spring对Java的邮件发送提供了很好的支持,提供了超级简单的API,大大简化了Java邮件发送功能的开发. Spring对Email的支持是基于JavaMail API开发的,所以,我们在 ...

  5. freemarker模板文件中文本域(textarea)的高度自适应实现

    2019独角兽企业重金招聘Python工程师标准>>> freemarker模板文件中实现文本域(textarea)的高度自适应实现. 从网上找的大部分办法中很多方法兼容性并不好,要 ...

  6. JFinal配合Shiro权限控制在FreeMarker模板引擎中控制到按钮粒度的使用

    实现在FreeMarker模板中控制对应按钮的显示隐藏主要用到了Shiro中的hasRole, hasAnyRoles, hasPermission以及Authenticated等方法,我们可以实现T ...

  7. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  8. Struts2解析FreeMarker模板中变量的顺序

    http://zhxing.iteye.com/blog/391560 关于FreeMarker 的基础学习请参考文档..这里主要记录 FreeMarker 的一些问题.. 1.FreeMarker  ...

  9. FreeMarker模板文件的组成(2)

    1.FreeMarker模板文件主要由如下4个部分组成:        1.1文本:直接输出的部分        1.2注释:<#-- ... -->格式部分,不会输出        1. ...

最新文章

  1. iOS - Bundle 资源文件包
  2. python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
  3. Linux系统服务(systemctl)的使用
  4. 阿里云媒体转码MTS使用教程
  5. org.apache.flink.table.catalog.exceptions.CatalogException: Failed to create Hive Metastore client
  6. oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证
  7. python学习第三十二节(进程间通信、进程池、协程)
  8. Kernel中如何操作CPU及外设寄存器
  9. BugkuCTF-WEB题eval
  10. 在线服务器和客户端聊天,实验三、客户端和服务器能实现简单的聊天功能
  11. 猎豹浏览器怎么查看历史记录 猎豹浏览器历史记录查看教程
  12. python字符编码解码,文件编码解码。
  13. 【声辐射】——不同坐标系下的格林函数
  14. [Xpand] Error 1 Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
  15. 性能退化评估 matlab,LED驱动电源性能退化参数监测及寿命预测方法研究
  16. thinking in java 4
  17. 绝佳的3Dmax渲染技巧,这些精美的效果让人称赞不已!速看
  18. 处理HttpWebRequest访问https有安全证书的问题
  19. OpenHarmony成长计划学生挑战赛7天打卡活动介绍
  20. 寒假训练八(优先队列)2020.02.14(7题)

热门文章

  1. SAP实施顾问参与主数据搜集的四大好处
  2. 一个现金流量表的代码
  3. ABAP源程序---发票校验明细表
  4. Function Two
  5. SAP中的Currency Converting Factor
  6. 理念高大上的智慧社区,要落地还得俯下身解决四个现实问题
  7. python 动态导入类_从动态导入模块中类的字符串名动态实例化?
  8. mysql转化为GaussDB,GaussDB(openGauss)宣布开源,性能超越 MySQL 与 PostgreSQL
  9. openlayers地图旋转_OpenLayers教程二:实现简单的地图显示
  10. linux apt qt下载,Linux如何安装 apt-get 软件管理工具