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 阮一峰老师有一个专 ...
随机推荐
- epoll代码框架
epoll代码实现框架: #define MAX_EVENTS 10 struct epoll_event ev, events[MAX_EVENTS]; int listen_sock, conn_ ...
- Identity用户管理入门四(修改、删除用户)
修改用户不能修改Id及用户名所以创建视图模型时需要去除,新增用户跟修改用户基本视图一直,所以不再做演示 一.新建UpdateUserViewModel视图模型 using System.Compone ...
- Spring Cloud Eureka 之服务端自我注册
Eureka服务端实现了一种自我注册机制,涉及配置项: eureka.client.register-with-eureka spring.application.name Eureka Server ...
- SQLmap的基本命令
Sqlmap sqlmap是一个自动化的SQL注入工具,其主要功能是扫描,发现并利用给定的URL进行SQL注入.目前支持的数据库有MySql.Oracle.Access.PostageSQL.SQL ...
- java线程day-02
1.什么是线程 * 线程是程序执行的一条路径, 一个进程中可以包含多条线程 * 多线程并发执行可以提高程序的效率, 可以同时完成多项工作 2.多线程的应用场景 * 红蜘蛛同时共享屏幕给多个电脑 * 迅 ...
- 【PHP数据结构】链表的相关逻辑操作
链表的操作相对顺序表(数组)来说就复杂了许多.因为 PHP 确实已经为我们解决了很多数组操作上的问题,所以我们可以很方便的操作数组,也就不用为数组定义很多的逻辑操作.比如在 C 中,数组是有长度限制的 ...
- TP5 windows中执行定时任务
1 首先先写个自定义命令文件 比如 Test 2 在网站根目录下建立文件 crond.bat ,内容:(把你在cmd上操作流程写一遍) D: cd workspace\wamp\tp5 D:\PHPW ...
- Jmeter系列(27)- 常用逻辑控制器(6) | 如果(if)控制器If Controller
如果(if)控制器(If Controller) 在实际工作中,当使用JMeter做性能脚本或者接口脚本时,当遇到需要对不同的条件做不同的操作时,我们可以使用JMeter中if控制器来实现 if控制器 ...
- 深入HTML5第三天
表单form属性:method="get|post" action="url": 特性:不写样式是没有样式的 input:属性:type="text| ...
- win10系统移动热点使用技巧
win10系统是自动移动热点功能,在平时测试的时候,有时需要进行手机抓包,需要手机和电脑处于同一网络当中,这时可以开启热点使用. 如何开启移动热点? 直接搜索"移动热点" 但是如果 ...