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 {
@Autowired
private 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>