在日常开发中,我们有时候需要发送短信、邮件等通知,但是这些通知的内容通常都是动态的,而且可能会发生变动,为了程序的灵活性,我们通常会将通知的内容配置在页面上,然后后台通过渲染这些模板,来获取具体的内容。而 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字符串作为模板的更多相关文章

  1. freemarker中的split字符串分割

    freemarker中的split字符串分割 1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串 ...

  2. freemarker中的split字符串分割(十六)

    1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串分割--> <#list &quo ...

  3. 判断集合中存在String字符串 或 判断集合中不存在String字符串

    一.使用场景 用于集合中有多个相近的字符,无法使用包含判断 如: 这里如果我想判断以上集合中是否包含"信封件-DE"就会被"信封件-DE2"影响到 毕竟:&qu ...

  4. Java中的String字符串及其常用方法

    字符串(String) 文章目录 字符串(String) 直接定义字符串 常用方法 字符串长度 toLowerCase() & toUpperCase()方法 trim()方法去除空格 判空 ...

  5. java中的string字符串中的trim函数的作用

    去掉字符串首尾空格 防止不必要的空格导致错误public class test{ public static void main(String[] args) { String str = " ...

  6. android中获取string字符串的方法

    比如在arrays.xml里: <!--leo added for KYLIN-496--> <string-array name="reboot_item"&g ...

  7. C++中去掉string字符串中的\r\n等

    string imagedata;imagedata = “dudau\r\ndadafca\r\n” CString Image; Image = imagedata.c_str(); Image. ...

  8. C实现string字符串

    在C中实现string字符串,使用typedef将string定义为char *. #include <stdio.h> #include <stdlib.h> #includ ...

  9. ES6, Angular,React和ABAP中的String Template(字符串模板)

    String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...

随机推荐

  1. Hive的分桶表

    [分桶概述] Hive表分区的实质是分目录(将超大表的数据按指定标准细分到指定目录),且分区的字段不属于Hive表中存在的字段:分桶的实质是分文件(将超大文件的数据按指定标准细分到分桶文件),且分桶的 ...

  2. Delphi使用AcroPDF ActiveX显示PDF文件

    效果展示 调用方式 放入窗体即可使用,不想安装太多组件,可使用纯代码方式调用 interface ..... var AcroPDF: TAcroPDF; .... implementation .. ...

  3. 不使用注解和使用注解的web-service-dao结构

    一.未使用注解的web-service-dao结构 1.action类源码 其中,service作为一个成员属性,采用的是层层调用,service类中dao作为一个成员属性,再成员方法中调用: 2.b ...

  4. freeswitch python模块

    概述 freeswitch支持多种语言的业务开发,包括C/C++,java,python,js,lua,Golang等等.freeswitch在使用python做业务开发时,有俩种接入方式,一种是ES ...

  5. HearthbuddyHelper已经开源

    https://github.com/V-arc/HearthbuddyHelper 只是为了稳定,没耗时多久写的一个中控,只是在逐渐堆加,并未进行重构. 通过写这个感受到自己对异步和wpf的理解还有 ...

  6. 样式和模板快速入门Style,Template

    http://www.cnblogs.com/jv9/archive/2010/04/14/1711520.html 样式(Style)和模板(Template)的定义 在Silverlight中,样 ...

  7. 关联数组VS索引数组

    关联数组和常规说的数组类似,它包含标量抄数据,可用索引值来单独选择这些数据,和常规数组不同的是, 关联数组的索引值不是非负的整数而是任意的标量袭.这些标量称为百Keys,可以在以后用于检索数组中的数值 ...

  8. 328 day07线程池、Lambda表达式

    day07[线程池.Lambda表达式] 主要内容 等待与唤醒案例 线程池 Lambda表达式 教学目标 -[ ] 能够理解线程通信概念 -[ ] 能够理解等待唤醒机制 -[ ] 能够描述Java中线 ...

  9. 密码学系列之:Argon2加密算法详解

    目录 简介 密钥推导函数key derivation function Password Hashing Competition Argon2算法 Argon2的输入参数 处理流程 简介 Argon2 ...

  10. vue报错 Uncaught TypeError: Cannot read property of null

    有可能是点击a标签,但是a标签有click事件,未阻止默认事件导致报错,开始都看不出来是什么错误