freemarker中使用String字符串作为模板
在日常开发中,我们有时候需要发送短信、邮件等通知,但是这些通知的内容通常都是动态的,而且可能会发生变动,为了程序的灵活性,我们通常会将通知的内容配置在页面上,然后后台通过渲染这些模板,来获取具体的内容。而 freemarker 正好可以帮助我们来完整模板的渲染这一步。
需求:
1、给定一个字符串模板,渲染出内容
2、修改这个字符串模板,然后再次渲染
实现要点:
1、模板的加载器需要使用 StringTemplateLoader
2、模板不可使用 Configuration.getTemplate,而应该使用 new Template
3、StringTemplateLoader 上的一段注释
完整代码如下:
@Test
public void test001() throws Exception {
String templateName = "hello-template";
String templateValue = "hello,${name}";
Configuration configuration = configuration();
processTemplate(configuration, templateName, templateValue);
// -------------------- 进行模板的修改 ------------------------
templateValue = "hello,${name},我今年,${age}岁.";
processTemplate(configuration, templateName, templateValue);
}
/**
* 解析模板
*
* @param configuration
* @param templateName
* @throws IOException
* @throws TemplateException
*/
private void processTemplate(Configuration configuration, String templateName, String templateValue) throws IOException, TemplateException {
Map<String, Object> root = new HashMap<>(4);
root.put("name", "你好");
root.put("age", 25);
StringWriter stringWriter = new StringWriter();
Template template = new Template(templateName, templateValue, configuration);
template.process(root, stringWriter);
System.out.println(stringWriter.toString());
}
/**
* 配置 freemarker configuration
*
* @return
*/
private Configuration configuration() {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
StringTemplateLoader templateLoader = new StringTemplateLoader();
configuration.setTemplateLoader(templateLoader);
configuration.setDefaultEncoding("UTF-8");
return configuration;
}
执行结果:
freemarker中使用String字符串作为模板的更多相关文章
- freemarker中的split字符串分割
freemarker中的split字符串分割 1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串 ...
- freemarker中的split字符串分割(十六)
1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串分割--> <#list &quo ...
- 判断集合中存在String字符串 或 判断集合中不存在String字符串
一.使用场景 用于集合中有多个相近的字符,无法使用包含判断 如: 这里如果我想判断以上集合中是否包含"信封件-DE"就会被"信封件-DE2"影响到 毕竟:&qu ...
- Java中的String字符串及其常用方法
字符串(String) 文章目录 字符串(String) 直接定义字符串 常用方法 字符串长度 toLowerCase() & toUpperCase()方法 trim()方法去除空格 判空 ...
- java中的string字符串中的trim函数的作用
去掉字符串首尾空格 防止不必要的空格导致错误public class test{ public static void main(String[] args) { String str = " ...
- android中获取string字符串的方法
比如在arrays.xml里: <!--leo added for KYLIN-496--> <string-array name="reboot_item"&g ...
- C++中去掉string字符串中的\r\n等
string imagedata;imagedata = “dudau\r\ndadafca\r\n” CString Image; Image = imagedata.c_str(); Image. ...
- C实现string字符串
在C中实现string字符串,使用typedef将string定义为char *. #include <stdio.h> #include <stdlib.h> #includ ...
- ES6, Angular,React和ABAP中的String Template(字符串模板)
String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...
随机推荐
- bean的作用域和生命周期
一.Bean作用域 二.生命周期 其中,这个类实现各种接口重写各种方法,会按bean的声明周期按序执行: 其中,自定义的初始化和自定义销毁的方法不是实现接口重写,而是成员方法,并且在装配bean即在x ...
- k8s架构与组件详解
没有那么多花里胡哨,直接进行一个K8s架构与组件的学习. 一.K8s架构 k8s系统在设计是遵循c-s架构的,也就是我们图中apiserver与其余组件的交互.在生产中通常会有多个Master以实现K ...
- 美团分布式定时调度框架XXL-Job基本使用
一:XXL JOB 基本使用 1.官方中文文档:https://www.xuxueli.com/xxl-job/ 2.基本环境: 2.1:git下载项目, 执行xxl-job数据库初始化脚本 2.2: ...
- Jmeter系列(19)- 常用配置文件
JMeter.properties :跟Jmeter配置相关的配置信息都在这边,比如:Jmeter GUI页面的语言.日志级别设置等 User.properties:用户自定义相关的所有变量,会复写J ...
- php 设计模式 --桥接模式
php抽象类和接口的区别 https://www.cnblogs.com/vinter/p/8716685.html 什么时候适合使用 --- 多个角色配合工作:抽象角色对应具体角色: <?ph ...
- Charles抓包工具断点修改返回内容
在测试过程中,往往需要让服务器返回指定的内容,测试一些特殊情况.例如列表内容为空.数据异常的情况等.如果通过操作服务器配合构造相应的数据会比较麻烦,甚至不好构造数据.此时,可以使用Charles的断点 ...
- python杀死Windows后台程序
检测 "sogou-gui.exe" 的进程可用tasklist命令 tasklist /FI "IMAGENAME eq sogou-gui.exe" FI: ...
- 定要过python二级选择题第一套
1. 2.https://zhuanlan.zhihu.com/p/199883725 树,队列,二叉树,树的基本回忆 二叉树: 分叉为俩个; 一个是右子树一个是左子树 队列:先进先出 柞:后进先出 ...
- HTML 网页开发、CSS 基础语法——三. HTML概念
1.纯文本格式 纯文本格式,就是没有任何文本修饰的,没有任何粗体,下划线,斜体,图形,符号,或者特殊字符,及特殊打印格式的文本,只保存文本,不保存其格式设置. ①格式对比 纯文本格式 富文本格式 最常 ...
- MacOS Typora集成SM.SM图床 实现自动上传图片
MacOS Typora集成SM.SM图床 实现自动上传图片 此为PicGo-Core (Command line) (OpenSource)配置方法 参照官网 https://support.typ ...