当前位置: 首页 > news >正文

Beetl工具类 BeetlUtils

pom.xml

        <!-- beetl模板引擎 --><dependency><groupId>com.ibeetl</groupId><artifactId>beetl-core</artifactId><version>3.19.1.RELEASE</version></dependency>

BeetlUtils

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;/*** Beetl工具类*/
public final class BeetlUtils {private BeetlUtils() { };private static GroupTemplate groupTemplate;private static Object block = new Object();/*** 以懒汉模式获取 GroupTemplate 对象* @return GroupTemplate*/public static GroupTemplate getGroupTemplate() {if (groupTemplate == null) {synchronized (block) {if (groupTemplate == null) {try {ClasspathResourceLoader webAppResourceLoader = new ClasspathResourceLoader("/");Configuration config = Configuration.defaultConfiguration();// Beetl的核心GroupTemplategroupTemplate = new GroupTemplate(webAppResourceLoader, config);} catch (IOException e) {e.printStackTrace();}}}}return groupTemplate;}/*** 创建新文件* @param map 参数* @param templatePath 模板路径* @param storePath 存储路径*/public static void createFile(Map<String, Object> map, String templatePath, String storePath) {OutputStream out = null;try {File file = new File(storePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}if (!file.exists()) {file.createNewFile();}GroupTemplate groupTemplate = BeetlUtils.getGroupTemplate();Template template = groupTemplate.getTemplate(templatePath);template.fastBinding(map);out = new FileOutputStream(storePath);template.renderTo(out);} catch (IOException e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}}/*** 创建HTML字符串* @param map 参数* @param templatePath 模板路径*/public static String createHTML(Map<String, Object> map, String templatePath) {GroupTemplate groupTemplate = BeetlUtils.getGroupTemplate();Template template = groupTemplate.getTemplate(templatePath);template.fastBinding(map);// 渲染字符串return template.render();}public static void main(String[] args) {Map<String, Object> map = new HashMap<>();map.put("title", "This is a test template Email.");map.put("body", "Hello, beetl!");String html = createHTML(map, "/template/hello.html.btl");System.out.println(html);}
}

hello.html.btl

<!-- beetl示例 -->
<html><head> <title>${title}</title> </head> <body> <h1>${body}</h1> </body> 
</html>

运行

<!-- beetl示例 -->
<html><head> <title>This is a test template Email.</title> </head> <body> <h1>Hello, beetl!</h1> </body> 
</html>

相关文章:

  • 临沂网站建设有哪些网络营销属于哪个专业
  • 网站建设与维护试卷分析域名注册流程和费用
  • 绵阳网站建设高端品牌1688关键词怎么优化
  • 影楼行业网站营销网络是什么意思
  • 广州网站建设信科便宜免费行情软件网站大全
  • 有中文网站 怎么做英文网站快速优化seo
  • 【swagger】Swagger的简单使用
  • 探索国产高精度嵌入式仿真教学新纪元——基于深圳航天科技创新研究院嵌入式仿真实验教学平台的ADC实验实践
  • 科技与人类贪欲
  • 华为云Flexus+DeepSeek征文|华为云ModelArts搭建Dify-LLM应用开发平台(AI智能选股大模型)
  • 智能化监管:微算法科技(NASDAQ:MLGO)比特币社区分类器助力加密货币市场规范发展
  • uniapp 和原生插件交互
  • vue3修改html中title标签 给 浏览器标签页 动态赋值 闪烁
  • 【2-入门与调试设置】1.坐标辅助器与轨道控制器
  • 【论文阅读】--Instruction Backdoor Attacks Against Customized LLMs
  • CTF:PHP 多关卡绕过挑战
  • 在vue3中,如何修改ant-deaign-vue tooltip的样式
  • 第4篇:响应处理——返回数据给客户端(Gin文件下载,JSON,XML等返回)
  • 2024考研数一真题及答案
  • 独立站安全收款实战:AB站隔离与风控
  • HDC 2025丨华为云AI原生中间件,构建应用运行的领先架构
  • swiftUI iOS16和iOS15兼容
  • 2025最新 WSL(Windows Subsystem for Linux)安装教程 (保姆级,图文讲解,带安装包)
  • 超声波清洗机相对于传统清洗方法有哪些优势?
  • 【25软考网工】第十章 网络规划与设计(2)网络规划与分析、网络结构与功能
  • 爬虫简单实操2——以贴吧为例练习